Skip to content

Commit

Permalink
Transform references from slug to ids
Browse files Browse the repository at this point in the history
Signed-off-by: Cédric Boirard <[email protected]>
  • Loading branch information
triozer committed Dec 13, 2024
1 parent c9211a6 commit 405e0df
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions examples/cms-starter/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function computeFieldConfig(existingFields: ManagedCollectionField[], dataSource
name,
type: field.multiple ? "multiCollectionReference" : "collectionReference",
collectionId: COLLECTIONS_SYNC_MAP.get(field.reference)?.[0].id ?? "",
userEditable: true,
userEditable: false,
}
} else {
const fieldType = FIELD_MAPPING[field.type][0] ?? "string"
Expand Down Expand Up @@ -193,9 +193,11 @@ function getFieldValue(field: FieldConfig, value: unknown) {
}
case "reference": {
if (field.field?.type === "multiCollectionReference") {
return String(value).split(",")
return String(value)
.split(",")
.map(id => generateHashId(id))
} else if (field.field?.type === "string" || field.field?.type === "collectionReference") {
return Array.isArray(value) ? value[0] : value
return Array.isArray(value) ? generateHashId(value[0]) : generateHashId(String(value))
}
return null
}
Expand Down Expand Up @@ -235,10 +237,6 @@ if (framer.mode === "syncManagedCollection" && savedFieldsConfig && syncDataSour
.filter(field => !field.reference || field.reference.destination !== null),
syncSlugFieldId
)

framer.closePlugin("Synced items", {
variant: "success",
})
}

const allDataSources = await listDataSourcesIds()
Expand Down Expand Up @@ -754,22 +752,23 @@ async function syncCollection(collection: DataSource, fields: FieldConfig[], slu
assert(slugField, "Slug field not found")

for (const item of collection.items) {
const slug = item[slugField.source.name]
if (typeof slug !== "string") {
const slugValue = item[slugField.source.name]
if (typeof slugValue !== "string") {
framer.notify(`Skipping item ${item.id} because it doesn't have a slug`, {
variant: "warning",
})
continue
}

const slug = slugify(slugValue)
const itemId = generateHashId(slug)
unsyncedItems.delete(itemId)

const fieldData: Record<string, unknown> = {}
for (const [fieldName, value] of Object.entries(item)) {
const field = fields.find(field => field.source.name === fieldName)

// Field is in the data but not in the to be synced fields
// Field is in the data but should not be synced
if (!field?.field) {
console.warn(`Skipping field ${fieldName} because it may have been ignored`)
continue
Expand All @@ -780,7 +779,7 @@ async function syncCollection(collection: DataSource, fields: FieldConfig[], slu

items.push({
id: itemId,
slug: slugify(slug),
slug: slug,
draft: false,
fieldData,
})
Expand Down

0 comments on commit 405e0df

Please sign in to comment.