From 3064b40508e7f6b23eccf88188da0e6ca558c176 Mon Sep 17 00:00:00 2001 From: Kevin Koech Date: Mon, 11 Sep 2023 16:36:40 +0300 Subject: [PATCH] Test new standard schema --- .../src/lib/ecosystem/airtable/index.js | 5 +- .../src/lib/ecosystem/airtable/processData.js | 74 +++++++-------- apps/charterafrica/src/lib/ecosystem/index.js | 1 - .../fields/ecosystem/socialMediaFields.js | 85 +++++++++++++++++ .../src/payload/globals/Ecosystem.js | 92 +------------------ 5 files changed, 124 insertions(+), 133 deletions(-) create mode 100644 apps/charterafrica/src/payload/fields/ecosystem/socialMediaFields.js diff --git a/apps/charterafrica/src/lib/ecosystem/airtable/index.js b/apps/charterafrica/src/lib/ecosystem/airtable/index.js index e93a6130f..293649cf6 100644 --- a/apps/charterafrica/src/lib/ecosystem/airtable/index.js +++ b/apps/charterafrica/src/lib/ecosystem/airtable/index.js @@ -68,15 +68,13 @@ async function data(config) { organisationTableId, contributorTableId, partnersTableId, - socialMediaTableId, }, } = config; const toolsData = await table(baseId, toolTableId); const contributorsData = await table(baseId, contributorTableId); const organisationsData = await table(baseId, organisationTableId); - const socialMediaData = await table(baseId, socialMediaTableId); const partnersData = await table(baseId, partnersTableId); - const tableData = { socialMediaData, partnersData }; + const tableData = { partnersData }; const tools = toolsData .map((item) => processTool(item, config, tableData)) .filter(Boolean); @@ -90,7 +88,6 @@ async function data(config) { tools, organisations, contributors, - socialMedia: socialMediaData, partners: partnersData, }; } diff --git a/apps/charterafrica/src/lib/ecosystem/airtable/processData.js b/apps/charterafrica/src/lib/ecosystem/airtable/processData.js index d947741de..12ca65d42 100644 --- a/apps/charterafrica/src/lib/ecosystem/airtable/processData.js +++ b/apps/charterafrica/src/lib/ecosystem/airtable/processData.js @@ -29,7 +29,7 @@ function getRepoLink(source = "github", slug = "") { function mapSupporterIdsToObjects(supporterIds, config, { partnersData }) { const { - schema: { partnerTableColumns }, + schema: { partnerTableColumns = {} }, } = config; const { name, url, logo } = partnerTableColumns; const mapped = supporterIds.map((id) => { @@ -47,27 +47,32 @@ function mapSupporterIdsToObjects(supporterIds, config, { partnersData }) { return mapped.filter(Boolean); } -function mapSocialMediaIdsToObjects(socialMedia, config, tableData) { - const { - schema: { socialMediaTableColumns }, - } = config; - const { name, url } = socialMediaTableColumns; - const { socialMediaData = [] } = tableData; - const mapped = socialMedia.map((id) => { - const { fields } = socialMediaData.find((item) => item.id === id) || {}; - - if (!fields) { +function mapSocialMediaColumnsToObjects(columns, data) { + const getData = (name) => { + const link = getValue(data, columns[name]); + if (!link) { return null; } return { - name: getValue(fields, name), - link: getValue(fields, url), + name, + link, }; - }); - return mapped.filter(Boolean); + }; + return [ + getData("twitter"), + getData("facebook"), + getData("youtube"), + getData("slack"), + getData("tiktok"), + getData("linkedIn"), + getData("instagram"), + getData("telegram"), + getData("discord"), + getData("whatsapp"), + ].filter(Boolean); } -export function processTool(item, config, { partnersData, socialMediaData }) { +export function processTool(item, config, { partnersData }) { const { schema: { toolTableColumns }, localized, @@ -107,17 +112,16 @@ export function processTool(item, config, { partnersData, socialMediaData }) { const partners = mapSupporterIdsToObjects( getValue(data, toolTableColumns.partners) || [], config, - { partnersData, socialMediaData }, + { partnersData }, ); const supporters = mapSupporterIdsToObjects( getValue(data, toolTableColumns.supporters) || [], config, - { partnersData, socialMediaData }, + { partnersData }, ); - const socialMedia = mapSocialMediaIdsToObjects( - getValue(data, toolTableColumns.socialMedia) || [], - config, - { partnersData, socialMediaData }, + const socialMedia = mapSocialMediaColumnsToObjects( + toolTableColumns.socialMediaColumns, + data, ); const source = getSourceType(getValue(data, toolTableColumns.source.url)); return { @@ -139,11 +143,7 @@ export function processTool(item, config, { partnersData, socialMediaData }) { }; } -export function processContributor( - item, - config, - { partnersData, socialMediaData }, -) { +export function processContributor(item, config) { const { schema: { contributorTableColumns }, localized, @@ -157,10 +157,9 @@ export function processContributor( } const locales = localized ? ["en", "fr", "pt"] : ["en"]; - const socialMedia = mapSocialMediaIdsToObjects( - getValue(data, contributorTableColumns.socialMedia) || [], - config, - { partnersData, socialMediaData }, + const socialMedia = mapSocialMediaColumnsToObjects( + contributorTableColumns.socialMediaColumns, + data, ); const foundDescription = locales.reduce((acc, curr) => { const val = getValue(data, contributorTableColumns.description[curr]); @@ -185,11 +184,7 @@ export function processContributor( }; } -export function processOrganisation( - item, - config, - { partnersData, socialMediaData }, -) { +export function processOrganisation(item, config, { partnersData }) { const { schema: { organisationTableColumns }, localized, @@ -230,10 +225,9 @@ export function processOrganisation( config, { partnersData }, ); - const socialMedia = mapSocialMediaIdsToObjects( - getValue(data, organisationTableColumns.socialMedia) || [], - config, - { socialMediaData }, + const socialMedia = mapSocialMediaColumnsToObjects( + organisationTableColumns.socialMediaColumns, + data, ); const source = getSourceType( getValue(data, organisationTableColumns.source.url), diff --git a/apps/charterafrica/src/lib/ecosystem/index.js b/apps/charterafrica/src/lib/ecosystem/index.js index ba1a0088b..fd325134e 100644 --- a/apps/charterafrica/src/lib/ecosystem/index.js +++ b/apps/charterafrica/src/lib/ecosystem/index.js @@ -18,7 +18,6 @@ function checkConfig(config) { "contributorTableId", "organisationTableId", "partnersTableId", - "socialMediaTableId", ]; const isPresent = (tableName) => config?.schema?.[tableName]; return requiredTables.every(isPresent); diff --git a/apps/charterafrica/src/payload/fields/ecosystem/socialMediaFields.js b/apps/charterafrica/src/payload/fields/ecosystem/socialMediaFields.js new file mode 100644 index 000000000..8231da851 --- /dev/null +++ b/apps/charterafrica/src/payload/fields/ecosystem/socialMediaFields.js @@ -0,0 +1,85 @@ +import airtableColumnSelect from "./airtableColumnSelect"; + +function socialMediaColumns({ tableField, schema }) { + return { + label: "Social Media", + type: "group", + name: "socialMediaColumns", + fields: [ + airtableColumnSelect({ + schema, + tableField, + overrides: { + name: "twitter", + label: "Twitter", + }, + }), + airtableColumnSelect({ + schema, + tableField, + overrides: { + name: "tiktok", + label: "Tiktok", + }, + }), + airtableColumnSelect({ + schema, + tableField, + overrides: { + name: "facebook", + label: "Facebook", + }, + }), + airtableColumnSelect({ + schema, + tableField, + overrides: { + name: "linkedIn", + label: "Linkedin", + }, + }), + airtableColumnSelect({ + schema, + tableField, + overrides: { + name: "instagram", + label: "Instagram", + }, + }), + airtableColumnSelect({ + schema, + tableField, + overrides: { + name: "youtube", + label: "Youtube", + }, + }), + airtableColumnSelect({ + schema, + tableField, + overrides: { + name: "telegram", + label: "Telegram", + }, + }), + airtableColumnSelect({ + schema, + tableField, + overrides: { + name: "discord", + label: "Discord", + }, + }), + airtableColumnSelect({ + schema, + tableField, + overrides: { + name: "whatsapp", + label: "Whatsapp", + }, + }), + ], + }; +} + +export default socialMediaColumns; diff --git a/apps/charterafrica/src/payload/globals/Ecosystem.js b/apps/charterafrica/src/payload/globals/Ecosystem.js index 29ffe5fe8..7e64e63b2 100644 --- a/apps/charterafrica/src/payload/globals/Ecosystem.js +++ b/apps/charterafrica/src/payload/globals/Ecosystem.js @@ -3,6 +3,7 @@ import airtableBaseSelect, { } from "../fields/ecosystem/airtableBaseSelect"; import airtableColumnSelect from "../fields/ecosystem/airtableColumnSelect"; import airtableTableSelect from "../fields/ecosystem/airtableTableSelect"; +import socialMediaColumns from "../fields/ecosystem/socialMediaFields"; import sourceField from "../fields/ecosystem/sourceField"; import { ECOSYSTEM_GLOBAL } from "../utils/collections"; @@ -246,18 +247,6 @@ const Ecosystem = { }, }, }), - airtableColumnSelect({ - schema, - tableField: "toolTableId", - overrides: { - name: "socialMedia", - label: { - en: "Social Media", - fr: "Réseaux sociaux", - pt: "Mídia social", - }, - }, - }), sourceField({ tableField: "toolTableId", schema }), airtableColumnSelect({ schema, @@ -307,6 +296,7 @@ const Ecosystem = { }, }, }), + socialMediaColumns({ schema, tableField: "toolTableId" }), ], }, ], @@ -431,17 +421,9 @@ const Ecosystem = { hideGutter: true, }, }, - airtableColumnSelect({ + socialMediaColumns({ schema, tableField: "contributorTableId", - overrides: { - name: "socialMedia", - label: { - en: "Social Media", - fr: "Réseaux sociaux", - pt: "Mídia social", - }, - }, }), sourceField({ tableField: "contributorTableId", schema }), ], @@ -579,17 +561,9 @@ const Ecosystem = { }, }, }), - airtableColumnSelect({ + socialMediaColumns({ schema, tableField: "organisationTableId", - overrides: { - name: "socialMedia", - label: { - en: "Social Media", - fr: "Réseaux sociaux", - pt: "Mídia social", - }, - }, }), airtableColumnSelect({ schema, @@ -702,64 +676,6 @@ const Ecosystem = { }, ], }, - { - type: "collapsible", - label: { - en: "Social Media", - fr: "Réseaux sociaux", - pt: "Mídia social", - }, - admin: { - initCollapsed: true, - }, - fields: [ - airtableTableSelect({ - schema, - name: "socialMediaTableId", - label: { - en: "Table Name", - fr: "Nom du tableau", - pt: "Nome da tabela", - }, - }), - { - type: "group", - name: "socialMediaTableColumns", - label: { - en: "Columns", - fr: "Colonnes", - pt: "Colunas", - }, - admin: { - hideGutter: true, - }, - fields: [ - airtableColumnSelect({ - schema, - tableField: "socialMediaTableId", - overrides: { - name: "name", - label: { - en: "Name", - fr: "Nom", - pt: "Nome", - }, - }, - }), - airtableColumnSelect({ - schema, - tableField: "socialMediaTableId", - overrides: { - name: "url", - label: { - en: "URL", - }, - }, - }), - ], - }, - ], - }, ], }, ],