Skip to content

A platform for community discussion. Free, open, simple.

DEV: Make the fields every saved category has required (#45209)

Sérgio Saquetimadf7244270b8
diff --git a/frontend/discourse/app/models/category.ts b/frontend/discourse/app/models/category.ts
index cc0fc368fcf..6e0453e1677 100644
--- a/frontend/discourse/app/models/category.ts
+++ b/frontend/discourse/app/models/category.ts
@@ -126,16 +126,20 @@ type CategorySearchResult = {
categoriesCount: number;
};
-/** Server payload fields and values assigned by core; plugins can augment this interface. */
+/**
+ * Server payload fields and values assigned by core; plugins can augment this interface.
+ * The required fields are missing only on a new category before its first `save()`.
+ */
export interface CategoryFields {
- id?: number;
- name?: string;
- slug?: string | null;
- color?: string;
- text_color?: string;
- style_type?: CategoryStyle;
- icon?: string | null;
- emoji?: string | null;
+ id: number;
+ name: string;
+ /** Null after the edit form clears it; empty when the generated slug was taken. */
+ slug: string | null;
+ color: string;
+ text_color: string;
+ style_type: CategoryStyle;
+ icon: string | null;
+ emoji: string | null;
description?: string | null;
description_text?: string | null;
description_excerpt?: string | null;
@@ -143,7 +147,7 @@ export interface CategoryFields {
topic_count?: number;
post_count?: number;
position?: number | null;
- read_restricted?: boolean;
+ read_restricted: boolean;
permission?: number | null;
parent_category_id?: number | null;
notification_level?: number | null;
@@ -765,9 +769,9 @@ export default class Category extends RestModel {
@service declare currentUser: User | null;
- @tracked color?: string;
- @tracked emoji?: string | null;
- @tracked icon?: string | null;
+ @tracked color!: string;
+ @tracked emoji!: string | null;
+ @tracked icon!: string | null;
@tracked
localizations: CategoryEditableLocalization[] | undefined =
this.category_localizations;
diff --git a/frontend/discourse/type-tests/models/category-test.ts b/frontend/discourse/type-tests/models/category-test.ts
index 22367b168c2..338974e527b 100644
--- a/frontend/discourse/type-tests/models/category-test.ts
+++ b/frontend/discourse/type-tests/models/category-test.ts
@@ -88,14 +88,14 @@ expectTypeOf(typeMetadata["type_specific_extra"]).toEqualTypeOf<unknown>();
// Every data field core sends or assigns, with its presence and nullability.
interface ExpectedCategoryFields {
- id?: number;
- name?: string;
- slug?: string | null;
- color?: string;
- text_color?: string;
- style_type?: ExpectedStyle;
- icon?: string | null;
- emoji?: string | null;
+ id: number;
+ name: string;
+ slug: string | null;
+ color: string;
+ text_color: string;
+ style_type: ExpectedStyle;
+ icon: string | null;
+ emoji: string | null;
description?: string | null;
description_text?: string | null;
description_excerpt?: string | null;
@@ -103,7 +103,7 @@ interface ExpectedCategoryFields {
topic_count?: number;
post_count?: number;
position?: number | null;
- read_restricted?: boolean;
+ read_restricted: boolean;
permission?: number | null;
parent_category_id?: number | null;
notification_level?: number | null;
@@ -284,3 +284,9 @@ Category.create({ style_type: "circle" });
declare const store: StoreService;
expectTypeOf<ModelFor<"category">>().toEqualTypeOf<Category>();
expectTypeOf(store.createRecord("category")).toEqualTypeOf<Category>();
+
+// New categories are built without an id until they are saved.
+expectTypeOf(Category.create({ name: "Draft" })).toEqualTypeOf<Category>();
+expectTypeOf(
+ store.createRecord("category", { name: "Draft" })
+).toEqualTypeOf<Category>();
diff --git a/frontend/discourse/type-tests/models/site-test.ts b/frontend/discourse/type-tests/models/site-test.ts
index 9c480371bf5..acb5c2e1b31 100644
--- a/frontend/discourse/type-tests/models/site-test.ts
+++ b/frontend/discourse/type-tests/models/site-test.ts
@@ -296,9 +296,7 @@ expectTypeOf(site.desktopView).toEqualTypeOf<boolean>();
expectTypeOf(site.mobileView).toEqualTypeOf<boolean>();
expectTypeOf(site.isMobileDevice).toEqualTypeOf<boolean>();
expectTypeOf(site.categoriesByCount).toEqualTypeOf<Category[]>();
-expectTypeOf(site.categoriesById).toEqualTypeOf<
- Map<number | undefined, Category>
->();
+expectTypeOf(site.categoriesById).toEqualTypeOf<Map<number, Category>>();
expectTypeOf(site.categoriesByParentId).toEqualTypeOf<
Map<number | null | undefined, Category[]>
>();
@@ -327,11 +325,28 @@ expectTypeOf(site.topicFlagTypeById(1)).toEqualTypeOf<
expectTypeOf(site.topicFlagTypeById("1")).toEqualTypeOf<
PostActionType | undefined
>();
+// The smallest payload a saved category arrives with, from the badge serializer.
+const badgeCategory = {
+ id: 1,
+ name: "Bugs",
+ slug: "bugs",
+ color: "0088CC",
+ text_color: "FFFFFF",
+ style_type: "square",
+ icon: null,
+ emoji: null,
+ read_restricted: false,
+} as const;
+expectTypeOf(site.updateCategory(badgeCategory)).toEqualTypeOf<Category>();
expectTypeOf(
- site.updateCategory({ id: 1, permission: null })
+ site.updateCategory({ ...badgeCategory, permission: null })
).toEqualTypeOf<Category>();
expectTypeOf(
- site.updateCategory({ id: 1, has_children: null, plugin_payload: true })
+ site.updateCategory({
+ ...badgeCategory,
+ has_children: null,
+ plugin_payload: true,
+ })
).toEqualTypeOf<Category>();
expectTypeOf(site.removeCategory(1)).toEqualTypeOf<void>();
expectTypeOf(site.removeCategory("1")).toEqualTypeOf<void>();
@@ -355,7 +370,7 @@ site.isReadOnly = false;
// @ts-expect-error the read-only setter keeps the boolean backing field's type
site.isReadOnly = "false";
// @ts-expect-error category updates use numeric server ids
-site.updateCategory({ id: "1" });
+site.updateCategory({ ...badgeCategory, id: "1" });
// @ts-expect-error category removal accepts an id, not a category payload
site.removeCategory({ id: 1 });
@@ -373,7 +388,7 @@ expectTypeOf(Site.currentProp("isReadOnly", true)).toEqualTypeOf<
boolean | undefined
>();
expectTypeOf(Site.currentProp("categoriesById")).toEqualTypeOf<
- Map<number | undefined, Category>
+ Map<number, Category>
>();
expectTypeOf(Site.currentProp("type_test_plugin_flag")).toEqualTypeOf<
boolean | undefined