A platform for community discussion. Free, open, simple.
FIX: Load the composer category before rendering
Régis Hanol74ee39b1b209
diff --git a/frontend/discourse/app/lib/multi-cache.js b/frontend/discourse/app/lib/multi-cache.jsindex 37ed6cfa7fa..2fd20ef02e8 100644--- a/frontend/discourse/app/lib/multi-cache.js+++ b/frontend/discourse/app/lib/multi-cache.js@@ -21,8 +21,6 @@ export class MultiCache {}async fetch(ids) {- this.fetchTimes = [this.fetchTimes[this.fetchTimes.length - 1], new Date()];-const notFound = [];ids = uniqueItemsFromArray(ids);@@ -33,6 +31,7 @@ export class MultiCache {}if (notFound.length !== 0) {+ this.fetchTimes = [this.fetchTimes.at(-1), new Date()];const request = this.cb(notFound);for (const id of notFound) {diff --git a/frontend/discourse/app/models/composer.ts b/frontend/discourse/app/models/composer.tsindex 5e04598ec61..48aa3e7b044 100644--- a/frontend/discourse/app/models/composer.ts+++ b/frontend/discourse/app/models/composer.ts@@ -473,7 +473,7 @@ export default class Composer extends RestModel {};if (this.site.lazy_load_categories) {- Category.asyncFindById(categoryId!).then(applyTemplate);+ Category.asyncFindById(categoryId!).then(applyTemplate, () => {});} else {applyTemplate();}@@ -1830,6 +1830,15 @@ export default class Composer extends RestModel {this.set(_add_draft_fields[f], opts[f]);});+ promise = promise.then(async () => {+ const { categoryId } = this;+ if (!this.canCategorize || !categoryId) {+ return;+ }++ await Category.asyncFindById(categoryId).catch(() => {});+ });+return promise.finally(() => {this.set("loading", false);});diff --git a/frontend/discourse/tests/unit/models/composer-test.js b/frontend/discourse/tests/unit/models/composer-test.jsindex 1ee40bbb6ae..f54185748f1 100644--- a/frontend/discourse/tests/unit/models/composer-test.js+++ b/frontend/discourse/tests/unit/models/composer-test.js@@ -2,6 +2,7 @@ import { getOwner } from "@ember/owner";import { setupTest } from "ember-qunit";import { module, test } from "qunit";import { USER_OPTION_COMPOSITION_MODES } from "discourse/lib/constants";+import Category from "discourse/models/category";import {CREATE_TOPIC,EDIT,@@ -671,6 +672,33 @@ module("Unit | Model | composer", function (hooks) {);});+ test("open loads the category when categories are lazy loaded", async function (assert) {+ getOwner(this).lookup("service:site").set("lazy_load_categories", true);+ const composer = createComposer.call(this);++ await composer.open({+ action: CREATE_TOPIC,+ categoryId: 12345,+ draftKey: "new_topic",+ });++ assert.true(Category.hasAsyncFoundAll([12345]));+ });++ test("open does not fail when the category cannot be loaded", async function (assert) {+ getOwner(this).lookup("service:site").set("lazy_load_categories", true);+ pretender.get("/categories/find", () => response(500, {}));+ const composer = createComposer.call(this);++ await composer.open({+ action: CREATE_TOPIC,+ categoryId: 12345,+ draftKey: "new_topic",+ });++ assert.false(composer.loading);+ });+test("editPost rolls back state on save failure", async function (assert) {const store = getOwner(this).lookup("service:store");const post = store.createRecord("post", {diff --git a/frontend/discourse/tests/unit/utils/multi-cache-test.js b/frontend/discourse/tests/unit/utils/multi-cache-test.jsindex 7df04193ea2..4c7ab3b9a50 100644--- a/frontend/discourse/tests/unit/utils/multi-cache-test.js+++ b/frontend/discourse/tests/unit/utils/multi-cache-test.js@@ -82,6 +82,7 @@ module("Unit | Utils | multi-cache", function (hooks) {const response2 = cache.fetch([10]);assert.strictEqual(requests.length, 1);+ assert.false(Boolean(cache.hadTooManyCalls()));const [request] = requests;assert.deepEqual(request.ids, [10]);