DEV: Add support for array params in topic-list finder (#21578)

It wasn't possible (at least in any reasonable way) to pass params like `tags`. Also removes the export and inlines the function as that was used only to test the function and the test is gone.
This commit is contained in:
Jarek Radosz 2023-05-16 19:49:38 +02:00 committed by GitHub
parent 791630f08b
commit dec38e2daf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 29 deletions

View File

@ -2,15 +2,24 @@ import PreloadStore from "discourse/lib/preload-store";
import RestAdapter from "discourse/adapters/rest";
import { ajax } from "discourse/lib/ajax";
export function finderFor(filter, params) {
return function () {
export default RestAdapter.extend({
find(store, type, { filter, params }) {
return PreloadStore.getAndRemove("topic_list", () => {
let url = `/${filter}.json`;
if (params) {
const urlSearchParams = new URLSearchParams();
for (const [key, value] of Object.entries(params)) {
if (typeof value !== "undefined") {
if (typeof value === "undefined") {
continue;
}
if (Array.isArray(value)) {
for (const arrayValue of value) {
urlSearchParams.append(`${key}[]`, arrayValue);
}
} else {
urlSearchParams.set(key, value);
}
}
@ -23,18 +32,7 @@ export function finderFor(filter, params) {
}
return ajax(url);
};
}
export default RestAdapter.extend({
find(store, type, findArgs) {
const filter = findArgs.filter;
const params = findArgs.params;
return PreloadStore.getAndRemove(
"topic_list",
finderFor(filter, params)
).then(function (result) {
}).then((result) => {
result.filter = filter;
result.params = params;
return result;

View File

@ -7,6 +7,7 @@ import { ajax } from "discourse/lib/ajax";
import { getOwner } from "discourse-common/lib/get-owner";
import { isEmpty } from "@ember/utils";
import { notEmpty } from "@ember/object/computed";
import deprecated from "discourse-common/lib/deprecated";
function extractByKey(collection, klass) {
const retval = {};
@ -195,6 +196,15 @@ TopicList.reopenClass({
},
find(filter, params) {
deprecated(
`TopicList.find is deprecated. Use \`findFiltered("topicList")\` on the \`store\` service instead.`,
{
id: "topic-list-find",
since: "3.1.0.beta5",
dropFrom: "3.2.0.beta1",
}
);
const store = getOwner(this).lookup("service:store");
return store.findFiltered("topicList", { filter, params });
},

View File

@ -1,7 +1,10 @@
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
import pretender, {
fixturesByUrl,
response,
} from "discourse/tests/helpers/create-pretender";
module("Unit | Service | store", function (hooks) {
setupTest(hooks);
@ -236,4 +239,28 @@ module("Unit | Service | store", function (hooks) {
const users = await store.findAll("user");
assert.strictEqual(users.objectAt(0).username, "souna");
});
test("findFiltered", async function (assert) {
pretender.get("/topics/created-by/trout.json", ({ queryParams }) => {
assert.deepEqual(queryParams, {
order: "latest",
tags: ["dev", "bug"],
});
return response(fixturesByUrl["/c/bug/1/l/latest.json"]);
});
const store = getOwner(this).lookup("service:store");
const result = await store.findFiltered("topicList", {
filter: "topics/created-by/trout",
params: {
order: "latest",
tags: ["dev", "bug"],
},
});
assert.true(result.loaded);
assert.true("topic_list" in result);
assert.true(Array.isArray(result.topics));
assert.strictEqual(result.filter, "topics/created-by/trout");
});
});