-
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 #581 from CodeForAfrica/feature/cfa-partners-cms
Migrate partners to Payload
- Loading branch information
Showing
9 changed files
with
236 additions
and
73 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const Partners = { | ||
slug: "our-partners", | ||
labels: { | ||
singular: { | ||
en: "Partners", | ||
fr: "Partenaires", | ||
pt: "Parceiros", | ||
}, | ||
plural: { | ||
en: "Partners", | ||
fr: "Partenaires", | ||
pt: "Parceiros", | ||
}, | ||
}, | ||
fields: [ | ||
{ | ||
name: "title", | ||
label: { | ||
en: "Title", | ||
fr: "Titre", | ||
pt: "Título", | ||
}, | ||
type: "text", | ||
localized: true, | ||
required: true, | ||
}, | ||
{ | ||
name: "partners", | ||
label: { | ||
en: "Partners", | ||
fr: "Les partenaires", | ||
pt: "Parceiros", | ||
}, | ||
type: "relationship", | ||
relationTo: "partners", | ||
hasMany: true, | ||
admin: { | ||
isSortable: true, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
export default Partners; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import image from "../fields/image"; | ||
import richText from "../fields/richText"; | ||
import slug from "../fields/slug"; | ||
import socialLinks from "../fields/socialLinks"; | ||
import nestCollectionUnderPage from "../utils/nestCollectionUnderPage"; | ||
|
||
const Partners = { | ||
slug: "partners", | ||
labels: { | ||
singular: { | ||
en: "Partner", | ||
fr: "Partenaire", | ||
pt: "Parceiro", | ||
}, | ||
plural: { | ||
en: "Partners", | ||
fr: "Partenaires", | ||
pt: "Parceiros", | ||
}, | ||
}, | ||
admin: { | ||
useAsTitle: "name", | ||
defaultColumns: ["name", "logo"], | ||
}, | ||
access: { | ||
read: () => true, | ||
}, | ||
|
||
fields: [ | ||
{ | ||
name: "name", | ||
label: { | ||
en: "Name", | ||
fr: "Nom", | ||
pt: "Nome", | ||
}, | ||
type: "text", | ||
required: true, | ||
localized: true, | ||
}, | ||
slug({ fieldToUse: "name" }), | ||
image({ | ||
overrides: { | ||
name: "logo", | ||
required: true, | ||
}, | ||
}), | ||
richText({ | ||
name: "description", | ||
label: { | ||
en: "Description", | ||
fr: "La description", | ||
pt: "Descrição", | ||
}, | ||
localized: true, | ||
required: true, | ||
}), | ||
socialLinks({ | ||
name: "connect", | ||
label: "Social Media Links", | ||
required: false, | ||
}), | ||
], | ||
hooks: { | ||
afterRead: [nestCollectionUnderPage("partners")], | ||
}, | ||
}; | ||
export default Partners; |
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,69 @@ | ||
import { deepmerge } from "@mui/utils"; | ||
import { array } from "payload/dist/fields/validations"; | ||
|
||
import url from "./url"; | ||
|
||
const socialMediaOptions = [ | ||
"Facebook", | ||
"Twitter", | ||
"Instagram", | ||
"Linkedin", | ||
"Github", | ||
"Slack", | ||
]; | ||
|
||
function socialLinks(overrides) { | ||
const defaults = { | ||
name: "links", | ||
type: "array", | ||
label: "Links", | ||
labels: { | ||
singular: "Link", | ||
plural: "Links", | ||
}, | ||
minRows: 1, | ||
admin: { | ||
className: "array-field-nested", | ||
components: { | ||
RowLabel: ({ data, index }) => { | ||
let label = ""; | ||
if (data.platform) { | ||
label = data.platform; | ||
} | ||
if (data.url) { | ||
label = label ? `${label} (${data.url})` : data.url; | ||
} | ||
if (!label) { | ||
label = `Link ${String(index).padStart(2, "0")}`; | ||
} | ||
return label; | ||
}, | ||
}, | ||
initCollapsed: true, | ||
}, | ||
fields: [ | ||
{ | ||
name: "platform", | ||
type: "select", | ||
label: "Platform", | ||
options: socialMediaOptions, | ||
required: true, | ||
validate: (val, options) => { | ||
const { data } = options || {}; | ||
if (data?.links?.filter((l) => l.platform === val)?.length > 1) { | ||
return "Please select a unique platform"; | ||
} | ||
return array(val, options); | ||
}, | ||
}, | ||
url({ | ||
overrides: { | ||
required: true, | ||
}, | ||
}), | ||
], | ||
}; | ||
return deepmerge(defaults, overrides); | ||
} | ||
|
||
export default socialLinks; |
66 changes: 1 addition & 65 deletions
66
apps/codeforafrica/src/payload/globals/Settings/EngagementTab.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
22 changes: 22 additions & 0 deletions
22
apps/codeforafrica/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; |
21 changes: 21 additions & 0 deletions
21
apps/codeforafrica/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,21 @@ | ||
import findAndFormatPagePath from "./findAndFormatPagePath"; | ||
|
||
function nestCollectionUnderPage(pageSlug) { | ||
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) { | ||
// Handle Errors | ||
} | ||
return { ...doc, link: { href } }; | ||
}; | ||
} | ||
|
||
export default nestCollectionUnderPage; |