Skip to content

Commit

Permalink
Merge pull request #555 from CodeForAfrica/fix/issue550
Browse files Browse the repository at this point in the history
Refactor page links to use field hooks
  • Loading branch information
kilemensi authored Aug 31, 2023
2 parents 07d0074 + 8220fe3 commit f947335
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 84 deletions.
13 changes: 0 additions & 13 deletions apps/charterafrica/src/lib/data/common/getPageUrl.js

This file was deleted.

19 changes: 0 additions & 19 deletions apps/charterafrica/src/lib/data/common/processPageContributors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import getPageUrl from "@/charterafrica/lib/data/common/getPageUrl";
import { allCountries } from "@/charterafrica/lib/data/json/countries";
import {
CONTRIBUTORS_COLLECTION,
Expand All @@ -13,7 +12,6 @@ const orQueryBuilder = (fields, search) => {
};

export async function getContributors(page, api, context) {
const { breadcrumbs } = page;
const {
locale,
query: { page: pageNumber = 1, limit = 12, search, sort = "fullName" } = {},
Expand All @@ -38,17 +36,8 @@ export async function getContributors(page, api, context) {
},
);
const results = docs.map((person) => {
let href = null;
const pageUrl = breadcrumbs[breadcrumbs.length - 1]?.url;
if (pageUrl) {
const { slug } = person;
href = `${pageUrl}/${slug}`;
}
return {
...person,
link: {
href,
},
description: person.description || " ",
name: person.fullName || person.externalId || null,
image: person.avatarUrl ?? null,
Expand Down Expand Up @@ -89,20 +78,12 @@ async function processPagePerson(page, api, context) {
},
});

const pageUrl = await getPageUrl(api, "tools");
const tools = toolDocs.map((tool) => {
let href = null;
if (pageUrl) {
href = `${pageUrl}/${tool.slug}`;
}
return {
...tool,
image: tool.avatarUrl ?? null,
description: tool?.description || " ",
name: tool.name || " ",
link: {
href,
},
};
});

Expand Down
28 changes: 4 additions & 24 deletions apps/charterafrica/src/lib/data/common/processPageOrganisations.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import getPageUrl from "@/charterafrica/lib/data/common/getPageUrl";
import { allCountries } from "@/charterafrica/lib/data/json/countries";
import { ORGANIZATION_COLLECTION } from "@/charterafrica/payload/utils/collections";
import queryString from "@/charterafrica/utils/ecosystem/queryString";
Expand All @@ -24,24 +23,15 @@ async function processPageSingleOrganisation(page, api, context) {
if (!docs?.length) {
return null;
}
const organisation = docs[0] || {};

const pageUrl = await getPageUrl(api, "tools");
const tools = organisation.tools.map((tool) => {
let href = null;
if (pageUrl) {
href = `${pageUrl}/${tool.slug}`;
}
return {
const organisation = docs[0] || {};
const tools =
organisation.tools?.map((tool) => ({
...tool,
link: {
href,
},
image: tool.avatarUrl ?? null,
description: tool?.description || " ",
name: tool.name || " ",
};
});
})) ?? [];
const block = blocks.findIndex(
({ slug: bSlug }) => bSlug === "our-organisations",
);
Expand Down Expand Up @@ -73,7 +63,6 @@ async function processPageSingleOrganisation(page, api, context) {
}

export async function getOrganisations(page, api, context) {
const { breadcrumbs } = page;
const {
locale,
query: { page: pageNumber = 1, limit = 12, search, sort = "name" } = {},
Expand All @@ -97,17 +86,8 @@ export async function getOrganisations(page, api, context) {
},
);
const results = docs.map((tool) => {
let href = null;
const pageUrl = breadcrumbs[breadcrumbs.length - 1]?.url;
if (pageUrl) {
const { slug } = tool;
href = `${pageUrl}/${slug}`;
}
return {
...tool,
link: {
href,
},
image: tool.avatarUrl ?? null,
description: tool?.description || " ",
name: tool.name ?? tool?.externalId ?? null,
Expand Down
32 changes: 4 additions & 28 deletions apps/charterafrica/src/lib/data/common/processPageTools.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import getPageUrl from "@/charterafrica/lib/data/common/getPageUrl";
import { allCountries } from "@/charterafrica/lib/data/json/countries";
import {
TOOL_COLLECTION,
Expand Down Expand Up @@ -36,13 +35,9 @@ async function processPageSingleTool(page, api, context) {
if (!docs?.length) {
return null;
}

const tool = docs[0];
const contributorPage = await getPageUrl(api, "contributors");
const contributors = tool?.contributors?.map((person) => ({
...person,
link: { href: `${contributorPage}/${person.slug}` },
name: person.name || person?.fullName || person.username || null,
}));
const contributors = tool.toolContributors;
const { docs: orgDocs } = await api.getCollection(ORGANIZATION_COLLECTION, {
locale,
where: {
Expand All @@ -53,23 +48,14 @@ async function processPageSingleTool(page, api, context) {
});
const tools = [];
const filterLabels = labelsPerLocale[locale];
const organisationPage = await getPageUrl(api, "organisations");
const organisation = orgDocs?.[0]
? {
...orgDocs?.[0],
link: { href: `${organisationPage}/${orgDocs?.[0].slug}` },
}
: null;
const organisation = orgDocs?.[0] ?? null;

return {
...page,
blocks: [
{
...tool,
slug: "tool",
link: {
href: tool.link,
label: "",
},
contribute: {
href: getRepoLink(tool),
label: filterLabels.contribute,
Expand Down Expand Up @@ -111,7 +97,6 @@ async function processPageSingleTool(page, api, context) {
}

export async function getTools(page, api, context) {
const { breadcrumbs } = page;
const {
locale,
query: { page: pageNumber = 1, limit = 12, search, sort = "name" } = {},
Expand All @@ -138,21 +123,12 @@ export async function getTools(page, api, context) {
});

const results = docs.map((tool) => {
let href = null;
const pageUrl = breadcrumbs[breadcrumbs.length - 1]?.url;
if (pageUrl) {
const { slug } = tool;
href = `${pageUrl}/${slug}`;
}
return {
...tool,
topicLabel: "Topic",
exploreText: "Explore",
contributorsCount: tool?.contributors?.length ?? null,
description: tool.description ?? " ",
link: {
href,
},
image: tool.avatarUrl ?? null,
};
});
Expand Down
15 changes: 15 additions & 0 deletions apps/charterafrica/src/payload/collections/Contributors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import dateField from "../fields/dateField";
import slug from "../fields/slug";
import source from "../fields/source";
import { CONTRIBUTORS_COLLECTION } from "../utils/collections";
import nestCollectionUnderPage from "../utils/nestCollectionUnderPage";

function useFullNameOrExternalId({ doc }) {
if (doc) {
const name = doc.name ?? doc.fullName ?? doc.externalId ?? null;
return { ...doc, name };
}
return doc;
}

const Contributors = {
slug: CONTRIBUTORS_COLLECTION,
Expand Down Expand Up @@ -152,6 +161,12 @@ const Contributors = {
},
},
],
hooks: {
afterRead: [
nestCollectionUnderPage("contributors"),
useFullNameOrExternalId,
],
},
};

export default Contributors;
4 changes: 4 additions & 0 deletions apps/charterafrica/src/payload/collections/Organisations.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import slug from "../fields/slug";
import source from "../fields/source";
import supporter from "../fields/supporter";
import { ORGANIZATION_COLLECTION, TOOL_COLLECTION } from "../utils/collections";
import nestCollectionUnderPage from "../utils/nestCollectionUnderPage";

const Organisations = {
slug: ORGANIZATION_COLLECTION,
Expand Down Expand Up @@ -266,6 +267,9 @@ const Organisations = {
},
},
],
hooks: {
afterRead: [nestCollectionUnderPage("organisations")],
},
};

export default Organisations;
5 changes: 5 additions & 0 deletions apps/charterafrica/src/payload/collections/Tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import slug from "../fields/slug";
import source from "../fields/source";
import supporter from "../fields/supporter";
import { TOOL_COLLECTION, CONTRIBUTORS_COLLECTION } from "../utils/collections";
import nestCollectionUnderPage from "../utils/nestCollectionUnderPage";

const Tools = {
slug: TOOL_COLLECTION,
Expand Down Expand Up @@ -307,8 +308,12 @@ const Tools = {
dateField({
name: "deletedAt",
}),

source(),
],
hooks: {
afterRead: [nestCollectionUnderPage("tools")],
},
};

export default Tools;
22 changes: 22 additions & 0 deletions apps/charterafrica/src/payload/utils/findAndFormatPagePath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import formatPagePath from "./formatPagePath";

async function findAndFormatPagePath(payload, slug) {
const collection = "pages";
const options = {
collection,
where: {
slug: {
equals: slug,
},
},
limit: 0,
};
const { docs } = await payload.find(options);

if (docs?.length) {
return formatPagePath(collection, docs[0]);
}
return undefined;
}

export default findAndFormatPagePath;
23 changes: 23 additions & 0 deletions apps/charterafrica/src/payload/utils/nestCollectionUnderPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import findAndFormatPagePath from "./findAndFormatPagePath";

function nestCollectionUnderPage(pageSlug) {
// TODO(kilemensi): Think of a way of nesting title and breadcrumbs as well
// i.e. full SEO
return async function nestCollectionItemUnderParentPage({
doc,
req: { payload },
}) {
let href = null;
try {
const pagePath = await findAndFormatPagePath(payload, pageSlug);
if (pagePath) {
href = `${pagePath}/${doc.slug}`;
}
} catch (error) {
// TODO(kilemensi): Add Sentry to payload & report errors
}
return { ...doc, link: { href } };
};
}

export default nestCollectionUnderPage;

0 comments on commit f947335

Please sign in to comment.