@@ -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"