-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #555 from CodeForAfrica/fix/issue550
Refactor page links to use field hooks
- Loading branch information
Showing
9 changed files
with
77 additions
and
84 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
apps/charterafrica/src/payload/utils/findAndFormatPagePath.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
apps/charterafrica/src/payload/utils/nestCollectionUnderPage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |