on master help sign in
grants: make pushOrDropQuery function
commit2cd7c70d9ab704fedbc819c26bced0c2b76126f3 authorMatilde Park <matilde.park@urbit.org> dateThu 12 May 2022, 09:25 (4 years ago) parent36db253
3 files changed+34−28
package-lock.json+11 package.json+1 pages/grants.js+22−28
package-lock.json+11
@@ -22,6 +22,7 @@
2222 "gray-matter": "^4.0.3",
2323 "html-entities": "^2.3.2",
2424 "lodash.debounce": "^4.0.8",
25+ "lodash.omit": "^4.5.0",
2526 "luxon": "^2.0.2",
2627 "next": "^12.1.5",
2728 "path-browserify": "^1.0.1",
@@ -2078,6 +2079,11 @@
20782079 "resolved": "https://registry.npmjs.org/lodash.isequal/-/lodash.isequal-4.5.0.tgz",
20792080 "integrity": "sha1-QVxEePK8wwEgwizhDtMib30+GOA="
20802081 },
2082+ "node_modules/lodash.omit": {
2083+ "version": "4.5.0",
2084+ "resolved": "https://registry.npmjs.org/lodash.omit/-/lodash.omit-4.5.0.tgz",
2085+ "integrity": "sha1-brGa5aHuHdnfC5aeZs4Lf6MLXmA="
2086+ },
20812087 "node_modules/longest-streak": {
20822088 "version": "3.0.1",
20832089 "resolved": "https://registry.npmjs.org/longest-streak/-/longest-streak-3.0.1.tgz",
@@ -6665,6 +6671,11 @@
66656671 "resolved": "https://registry.npmjs.org/lodash.isequal/-/lodash.isequal-4.5.0.tgz",
66666672 "integrity": "sha1-QVxEePK8wwEgwizhDtMib30+GOA="
66676673 },
6674+ "lodash.omit": {
6675+ "version": "4.5.0",
6676+ "resolved": "https://registry.npmjs.org/lodash.omit/-/lodash.omit-4.5.0.tgz",
6677+ "integrity": "sha1-brGa5aHuHdnfC5aeZs4Lf6MLXmA="
6678+ },
66686679 "longest-streak": {
66696680 "version": "3.0.1",
66706681 "resolved": "https://registry.npmjs.org/longest-streak/-/longest-streak-3.0.1.tgz",
package.json+1
@@ -32,6 +32,7 @@
3232 "gray-matter": "^4.0.3",
3333 "html-entities": "^2.3.2",
3434 "lodash.debounce": "^4.0.8",
35+ "lodash.omit": "^4.5.0",
3536 "luxon": "^2.0.2",
3637 "next": "^12.1.5",
3738 "path-browserify": "^1.0.1",
pages/grants.js+22−28
@@ -9,6 +9,7 @@ import SingleColumn from "../components/SingleColumn";
99import Section from "../components/Section";
1010import GrantPreview from "../components/GrantPreview";
1111import JoinGroup from "../components/JoinGroup";
12+import omit from "lodash.omit";
1213import { getAllPosts, getGrantsCategories, getGrantsTypes } from "../lib/lib";
1314
1415function isArray(arr) {
@@ -41,6 +42,25 @@ export default function Grants({ posts, categories, types, search }) {
4142 );
4243 }
4344
45+ function pushOrDropQuery(queryName, currentQuery, item) {
46+ let to, toWithQuery;
47+ if (isArray(currentQuery)) {
48+ to = isOrIsIn(item, currentQuery)
49+ ? currentQuery.filter((e) => e !== item)
50+ : [...currentQuery, item];
51+ } else {
52+ to = isOrIsIn(item, currentQuery)
53+ ? null
54+ : [currentQuery, item].filter((e) => e !== undefined);
55+ }
56+ if (!to) {
57+ toWithQuery = omit(router.query, queryName);
58+ } else {
59+ toWithQuery = { ...router.query, [queryName]: to };
60+ }
61+ return toWithQuery;
62+ }
63+
4464 const annotatedPosts = posts.map((post) => {
4565 if (post.extra.canceled) {
4666 return { ...post, status: "canceled" };
@@ -304,21 +324,7 @@ export default function Grants({ posts, categories, types, search }) {
304324 const active = isOrIsIn(each, category)
305325 ? "bg-green-400 text-white"
306326 : "bg-wall-100 text-wall-500";
307− let to, categoryQuery;
308− if (isArray(category)) {
309− to = isOrIsIn(each, category)
310− ? category.filter((e) => e !== each)
311− : [...category, each];
312− } else {
313− to = isOrIsIn(each, category)
314− ? null
315− : [category, each].filter((e) => e !== undefined);
316− }
317− if (!to) {
318− categoryQuery = (({ category, ...obj }) => obj)(router.query);
319− } else {
320− categoryQuery = { ...router.query, category: to };
321− }
327+ const categoryQuery = pushOrDropQuery("category", category, each);
322328 return (
323329 <button
324330 className={`${active} badge-sm mr-1 my-1`}
@@ -348,19 +354,7 @@ export default function Grants({ posts, categories, types, search }) {
348354 ? "Exclude In Progress"
349355 : "Include In Progress";
350356 }
351− let to, statusQuery;
352− if (isArray(status)) {
353− to = isOrIsIn(each, status)
354− ? status.filter((e) => e !== each)
355− : [...status, each];
356− } else {
357− to = isOrIsIn(each, status) ? null : [status, each];
358− }
359− if (!to) {
360− statusQuery = (({ status, ...obj }) => obj)(router.query);
361− } else {
362− statusQuery = { ...router.query, status: to };
363− }
357+ const statusQuery = pushOrDropQuery("status", status, each);
364358 return (
365359 <button
366360 className="mr-4 badge-sm bg-black text-white"