mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
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:
parent
791630f08b
commit
dec38e2daf
@ -2,39 +2,37 @@ import PreloadStore from "discourse/lib/preload-store";
|
|||||||
import RestAdapter from "discourse/adapters/rest";
|
import RestAdapter from "discourse/adapters/rest";
|
||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
|
|
||||||
export function finderFor(filter, params) {
|
export default RestAdapter.extend({
|
||||||
return function () {
|
find(store, type, { filter, params }) {
|
||||||
let url = `/${filter}.json`;
|
return PreloadStore.getAndRemove("topic_list", () => {
|
||||||
|
let url = `/${filter}.json`;
|
||||||
|
|
||||||
if (params) {
|
if (params) {
|
||||||
const urlSearchParams = new URLSearchParams();
|
const urlSearchParams = new URLSearchParams();
|
||||||
|
|
||||||
for (const [key, value] of Object.entries(params)) {
|
for (const [key, value] of Object.entries(params)) {
|
||||||
if (typeof value !== "undefined") {
|
if (typeof value === "undefined") {
|
||||||
urlSearchParams.set(key, value);
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
for (const arrayValue of value) {
|
||||||
|
urlSearchParams.append(`${key}[]`, arrayValue);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
urlSearchParams.set(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const queryString = urlSearchParams.toString();
|
||||||
|
|
||||||
|
if (queryString) {
|
||||||
|
url += `?${queryString}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const queryString = urlSearchParams.toString();
|
return ajax(url);
|
||||||
|
}).then((result) => {
|
||||||
if (queryString) {
|
|
||||||
url += `?${queryString}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
|
||||||
result.filter = filter;
|
result.filter = filter;
|
||||||
result.params = params;
|
result.params = params;
|
||||||
return result;
|
return result;
|
||||||
|
@ -7,6 +7,7 @@ import { ajax } from "discourse/lib/ajax";
|
|||||||
import { getOwner } from "discourse-common/lib/get-owner";
|
import { getOwner } from "discourse-common/lib/get-owner";
|
||||||
import { isEmpty } from "@ember/utils";
|
import { isEmpty } from "@ember/utils";
|
||||||
import { notEmpty } from "@ember/object/computed";
|
import { notEmpty } from "@ember/object/computed";
|
||||||
|
import deprecated from "discourse-common/lib/deprecated";
|
||||||
|
|
||||||
function extractByKey(collection, klass) {
|
function extractByKey(collection, klass) {
|
||||||
const retval = {};
|
const retval = {};
|
||||||
@ -195,6 +196,15 @@ TopicList.reopenClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
find(filter, params) {
|
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");
|
const store = getOwner(this).lookup("service:store");
|
||||||
return store.findFiltered("topicList", { filter, params });
|
return store.findFiltered("topicList", { filter, params });
|
||||||
},
|
},
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
import { module, test } from "qunit";
|
import { module, test } from "qunit";
|
||||||
import { setupTest } from "ember-qunit";
|
import { setupTest } from "ember-qunit";
|
||||||
import { getOwner } from "discourse-common/lib/get-owner";
|
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) {
|
module("Unit | Service | store", function (hooks) {
|
||||||
setupTest(hooks);
|
setupTest(hooks);
|
||||||
@ -236,4 +239,28 @@ module("Unit | Service | store", function (hooks) {
|
|||||||
const users = await store.findAll("user");
|
const users = await store.findAll("user");
|
||||||
assert.strictEqual(users.objectAt(0).username, "souna");
|
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");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user