Skip to content

Commit f947335

Browse files
authored
Merge pull request #555 from CodeForAfrica/fix/issue550
Refactor page links to use field hooks
2 parents 07d0074 + 8220fe3 commit f947335

File tree

9 files changed

+77
-84
lines changed

9 files changed

+77
-84
lines changed

apps/charterafrica/src/lib/data/common/getPageUrl.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

apps/charterafrica/src/lib/data/common/processPageContributors.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import getPageUrl from "@/charterafrica/lib/data/common/getPageUrl";
21
import { allCountries } from "@/charterafrica/lib/data/json/countries";
32
import {
43
CONTRIBUTORS_COLLECTION,
@@ -13,7 +12,6 @@ const orQueryBuilder = (fields, search) => {
1312
};
1413

1514
export async function getContributors(page, api, context) {
16-
const { breadcrumbs } = page;
1715
const {
1816
locale,
1917
query: { page: pageNumber = 1, limit = 12, search, sort = "fullName" } = {},
@@ -38,17 +36,8 @@ export async function getContributors(page, api, context) {
3836
},
3937
);
4038
const results = docs.map((person) => {
41-
let href = null;
42-
const pageUrl = breadcrumbs[breadcrumbs.length - 1]?.url;
43-
if (pageUrl) {
44-
const { slug } = person;
45-
href = `${pageUrl}/${slug}`;
46-
}
4739
return {
4840
...person,
49-
link: {
50-
href,
51-
},
5241
description: person.description || " ",
5342
name: person.fullName || person.externalId || null,
5443
image: person.avatarUrl ?? null,
@@ -89,20 +78,12 @@ async function processPagePerson(page, api, context) {
8978
},
9079
});
9180

92-
const pageUrl = await getPageUrl(api, "tools");
9381
const tools = toolDocs.map((tool) => {
94-
let href = null;
95-
if (pageUrl) {
96-
href = `${pageUrl}/${tool.slug}`;
97-
}
9882
return {
9983
...tool,
10084
image: tool.avatarUrl ?? null,
10185
description: tool?.description || " ",
10286
name: tool.name || " ",
103-
link: {
104-
href,
105-
},
10687
};
10788
});
10889

apps/charterafrica/src/lib/data/common/processPageOrganisations.js

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import getPageUrl from "@/charterafrica/lib/data/common/getPageUrl";
21
import { allCountries } from "@/charterafrica/lib/data/json/countries";
32
import { ORGANIZATION_COLLECTION } from "@/charterafrica/payload/utils/collections";
43
import queryString from "@/charterafrica/utils/ecosystem/queryString";
@@ -24,24 +23,15 @@ async function processPageSingleOrganisation(page, api, context) {
2423
if (!docs?.length) {
2524
return null;
2625
}
27-
const organisation = docs[0] || {};
2826

29-
const pageUrl = await getPageUrl(api, "tools");
30-
const tools = organisation.tools.map((tool) => {
31-
let href = null;
32-
if (pageUrl) {
33-
href = `${pageUrl}/${tool.slug}`;
34-
}
35-
return {
27+
const organisation = docs[0] || {};
28+
const tools =
29+
organisation.tools?.map((tool) => ({
3630
...tool,
37-
link: {
38-
href,
39-
},
4031
image: tool.avatarUrl ?? null,
4132
description: tool?.description || " ",
4233
name: tool.name || " ",
43-
};
44-
});
34+
})) ?? [];
4535
const block = blocks.findIndex(
4636
({ slug: bSlug }) => bSlug === "our-organisations",
4737
);
@@ -73,7 +63,6 @@ async function processPageSingleOrganisation(page, api, context) {
7363
}
7464

7565
export async function getOrganisations(page, api, context) {
76-
const { breadcrumbs } = page;
7766
const {
7867
locale,
7968
query: { page: pageNumber = 1, limit = 12, search, sort = "name" } = {},
@@ -97,17 +86,8 @@ export async function getOrganisations(page, api, context) {
9786
},
9887
);
9988
const results = docs.map((tool) => {
100-
let href = null;
101-
const pageUrl = breadcrumbs[breadcrumbs.length - 1]?.url;
102-
if (pageUrl) {
103-
const { slug } = tool;
104-
href = `${pageUrl}/${slug}`;
105-
}
10689
return {
10790
...tool,
108-
link: {
109-
href,
110-
},
11191
image: tool.avatarUrl ?? null,
11292
description: tool?.description || " ",
11393
name: tool.name ?? tool?.externalId ?? null,

apps/charterafrica/src/lib/data/common/processPageTools.js

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import getPageUrl from "@/charterafrica/lib/data/common/getPageUrl";
21
import { allCountries } from "@/charterafrica/lib/data/json/countries";
32
import {
43
TOOL_COLLECTION,
@@ -36,13 +35,9 @@ async function processPageSingleTool(page, api, context) {
3635
if (!docs?.length) {
3736
return null;
3837
}
38+
3939
const tool = docs[0];
40-
const contributorPage = await getPageUrl(api, "contributors");
41-
const contributors = tool?.contributors?.map((person) => ({
42-
...person,
43-
link: { href: `${contributorPage}/${person.slug}` },
44-
name: person.name || person?.fullName || person.username || null,
45-
}));
40+
const contributors = tool.toolContributors;
4641
const { docs: orgDocs } = await api.getCollection(ORGANIZATION_COLLECTION, {
4742
locale,
4843
where: {
@@ -53,23 +48,14 @@ async function processPageSingleTool(page, api, context) {
5348
});
5449
const tools = [];
5550
const filterLabels = labelsPerLocale[locale];
56-
const organisationPage = await getPageUrl(api, "organisations");
57-
const organisation = orgDocs?.[0]
58-
? {
59-
...orgDocs?.[0],
60-
link: { href: `${organisationPage}/${orgDocs?.[0].slug}` },
61-
}
62-
: null;
51+
const organisation = orgDocs?.[0] ?? null;
52+
6353
return {
6454
...page,
6555
blocks: [
6656
{
6757
...tool,
6858
slug: "tool",
69-
link: {
70-
href: tool.link,
71-
label: "",
72-
},
7359
contribute: {
7460
href: getRepoLink(tool),
7561
label: filterLabels.contribute,
@@ -111,7 +97,6 @@ async function processPageSingleTool(page, api, context) {
11197
}
11298

11399
export async function getTools(page, api, context) {
114-
const { breadcrumbs } = page;
115100
const {
116101
locale,
117102
query: { page: pageNumber = 1, limit = 12, search, sort = "name" } = {},
@@ -138,21 +123,12 @@ export async function getTools(page, api, context) {
138123
});
139124

140125
const results = docs.map((tool) => {
141-
let href = null;
142-
const pageUrl = breadcrumbs[breadcrumbs.length - 1]?.url;
143-
if (pageUrl) {
144-
const { slug } = tool;
145-
href = `${pageUrl}/${slug}`;
146-
}
147126
return {
148127
...tool,
149128
topicLabel: "Topic",
150129
exploreText: "Explore",
151130
contributorsCount: tool?.contributors?.length ?? null,
152131
description: tool.description ?? " ",
153-
link: {
154-
href,
155-
},
156132
image: tool.avatarUrl ?? null,
157133
};
158134
});

apps/charterafrica/src/payload/collections/Contributors.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ import dateField from "../fields/dateField";
33
import slug from "../fields/slug";
44
import source from "../fields/source";
55
import { CONTRIBUTORS_COLLECTION } from "../utils/collections";
6+
import nestCollectionUnderPage from "../utils/nestCollectionUnderPage";
7+
8+
function useFullNameOrExternalId({ doc }) {
9+
if (doc) {
10+
const name = doc.name ?? doc.fullName ?? doc.externalId ?? null;
11+
return { ...doc, name };
12+
}
13+
return doc;
14+
}
615

716
const Contributors = {
817
slug: CONTRIBUTORS_COLLECTION,
@@ -152,6 +161,12 @@ const Contributors = {
152161
},
153162
},
154163
],
164+
hooks: {
165+
afterRead: [
166+
nestCollectionUnderPage("contributors"),
167+
useFullNameOrExternalId,
168+
],
169+
},
155170
};
156171

157172
export default Contributors;

apps/charterafrica/src/payload/collections/Organisations.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import slug from "../fields/slug";
44
import source from "../fields/source";
55
import supporter from "../fields/supporter";
66
import { ORGANIZATION_COLLECTION, TOOL_COLLECTION } from "../utils/collections";
7+
import nestCollectionUnderPage from "../utils/nestCollectionUnderPage";
78

89
const Organisations = {
910
slug: ORGANIZATION_COLLECTION,
@@ -266,6 +267,9 @@ const Organisations = {
266267
},
267268
},
268269
],
270+
hooks: {
271+
afterRead: [nestCollectionUnderPage("organisations")],
272+
},
269273
};
270274

271275
export default Organisations;

apps/charterafrica/src/payload/collections/Tools.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import slug from "../fields/slug";
88
import source from "../fields/source";
99
import supporter from "../fields/supporter";
1010
import { TOOL_COLLECTION, CONTRIBUTORS_COLLECTION } from "../utils/collections";
11+
import nestCollectionUnderPage from "../utils/nestCollectionUnderPage";
1112

1213
const Tools = {
1314
slug: TOOL_COLLECTION,
@@ -307,8 +308,12 @@ const Tools = {
307308
dateField({
308309
name: "deletedAt",
309310
}),
311+
310312
source(),
311313
],
314+
hooks: {
315+
afterRead: [nestCollectionUnderPage("tools")],
316+
},
312317
};
313318

314319
export default Tools;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import formatPagePath from "./formatPagePath";
2+
3+
async function findAndFormatPagePath(payload, slug) {
4+
const collection = "pages";
5+
const options = {
6+
collection,
7+
where: {
8+
slug: {
9+
equals: slug,
10+
},
11+
},
12+
limit: 0,
13+
};
14+
const { docs } = await payload.find(options);
15+
16+
if (docs?.length) {
17+
return formatPagePath(collection, docs[0]);
18+
}
19+
return undefined;
20+
}
21+
22+
export default findAndFormatPagePath;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import findAndFormatPagePath from "./findAndFormatPagePath";
2+
3+
function nestCollectionUnderPage(pageSlug) {
4+
// TODO(kilemensi): Think of a way of nesting title and breadcrumbs as well
5+
// i.e. full SEO
6+
return async function nestCollectionItemUnderParentPage({
7+
doc,
8+
req: { payload },
9+
}) {
10+
let href = null;
11+
try {
12+
const pagePath = await findAndFormatPagePath(payload, pageSlug);
13+
if (pagePath) {
14+
href = `${pagePath}/${doc.slug}`;
15+
}
16+
} catch (error) {
17+
// TODO(kilemensi): Add Sentry to payload & report errors
18+
}
19+
return { ...doc, link: { href } };
20+
};
21+
}
22+
23+
export default nestCollectionUnderPage;

0 commit comments

Comments
 (0)