Skip to content

Commit

Permalink
Test new standard schema
Browse files Browse the repository at this point in the history
  • Loading branch information
koechkevin committed Sep 11, 2023
1 parent e1dba35 commit 3064b40
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 133 deletions.
5 changes: 1 addition & 4 deletions apps/charterafrica/src/lib/ecosystem/airtable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -90,7 +88,6 @@ async function data(config) {
tools,
organisations,
contributors,
socialMedia: socialMediaData,
partners: partnersData,
};
}
Expand Down
74 changes: 34 additions & 40 deletions apps/charterafrica/src/lib/ecosystem/airtable/processData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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]);
Expand All @@ -185,11 +184,7 @@ export function processContributor(
};
}

export function processOrganisation(
item,
config,
{ partnersData, socialMediaData },
) {
export function processOrganisation(item, config, { partnersData }) {
const {
schema: { organisationTableColumns },
localized,
Expand Down Expand Up @@ -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),
Expand Down
1 change: 0 additions & 1 deletion apps/charterafrica/src/lib/ecosystem/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ function checkConfig(config) {
"contributorTableId",
"organisationTableId",
"partnersTableId",
"socialMediaTableId",
];
const isPresent = (tableName) => config?.schema?.[tableName];
return requiredTables.every(isPresent);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
92 changes: 4 additions & 88 deletions apps/charterafrica/src/payload/globals/Ecosystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -307,6 +296,7 @@ const Ecosystem = {
},
},
}),
socialMediaColumns({ schema, tableField: "toolTableId" }),
],
},
],
Expand Down Expand Up @@ -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 }),
],
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
},
},
}),
],
},
],
},
],
},
],
Expand Down

0 comments on commit 3064b40

Please sign in to comment.