Skip to content

Commit

Permalink
🚧 feat: fix download issues with Dynamic Zones
Browse files Browse the repository at this point in the history
  • Loading branch information
david-vaclavek committed Sep 5, 2023
1 parent 421b202 commit 22aa00f
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 54 deletions.
19 changes: 12 additions & 7 deletions server/controllers/localazy-transfer-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,17 @@ module.exports = {
);

if (typeof modelContentTransferSetup !== "undefined" && shouldSetDownloadedProperty(modelContentTransferSetup, parsedKey.rest)) {
const parsedKeyRestWithoutComponents = parsedKey.rest.map((segment) => {
const semicolonIndex = segment.indexOf(";");
const parsedKeyRestWithoutComponents = parsedKey.rest;
// TODO: component needs to be included in the key for dynamic zones (otherwise we loose data - only the last item of each unique entry id is set)
// .map((segment) => {
// const semicolonIndex = segment.indexOf(";");

if (semicolonIndex === -1) {
return segment;
}
// if (semicolonIndex === -1) {
// return segment;
// }

return segment.substring(0, semicolonIndex);
});
// return segment.substring(0, semicolonIndex);
// });

const setKey = [
isoStrapi,
Expand All @@ -412,6 +414,8 @@ module.exports = {
...parsedKeyRestWithoutComponents,
];

// ? TODO: Should be set as object instead of array?
// ? TODO: Should such thing be done only for dynamic zones?
set(parsedLocalazyContent, setKey, value);
}
}
Expand Down Expand Up @@ -486,6 +490,7 @@ module.exports = {
} catch (e) {
success = false;
strapi.log.error(e.message);
strapi.log.error(JSON.stringify(e.details?.errors || {}));
messageReport.push(
`Cannot create an entry in ${isoStrapi} for ${uid}[${baseEntry.id}]: ${e.message}`
);
Expand Down
7 changes: 5 additions & 2 deletions server/services/strapi-i18n-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ module.exports = ({ strapi }) => ({
baseEntry.id,
newEntryLocale
);
const mergedEntry = merge(cloneDeep(baseEntry), insertedEntry);

return mergedEntry;
return insertedEntry

// const mergedEntry = merge(cloneDeep(baseEntry), insertedEntry);

// return mergedEntry;
} catch (e) {
strapi.log.error(e);
throw e;
Expand Down
53 changes: 43 additions & 10 deletions server/services/strapi-localazy-i18n-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const parsedLocalazyEntryToCreateEntry = require("../utils/parsed-localazy-entry-to-create-entry");
const parsedLocalazyEntryToUpdateEntry = require("../utils/parsed-localazy-entry-to-update-entry");
const omitDeep = require("../utils/omit-deep");
const { set, merge } = require("lodash");

module.exports = ({ strapi }) => ({
async createEntry(
Expand All @@ -15,35 +17,66 @@ module.exports = ({ strapi }) => ({
// * The entry will be created and then updated as the structures differ
// * It's one extra database call, but the complexity of recursive code to maintain does worth it


// Strapi i18n Service
const StrapiI18nService = strapi
.plugin("localazy")
.service("strapiI18nService");

const { createEntry } = parsedLocalazyEntryToCreateEntry(
const { createEntry, dynamicZoneComponentKeys } = parsedLocalazyEntryToCreateEntry(
strapiContentTypesModels,
translatedModel,
baseEntry,
uid,
isoStrapi,
);

// ? TODO do not omit the __component from Dynamic Zones
const filteredBaseEntry = omitDeep(baseEntry, [
// "__component", // do not omit the __component
"locale",
"localizations",
"createdAt",
"createdBy",
"updatedAt",
"updatedBy",
"publishedAt",
]);

let mergedCreateEntry = {};
merge(mergedCreateEntry, filteredBaseEntry, createEntry);
mergedCreateEntry = omitDeep(mergedCreateEntry, [
// "__component",
"locale",
"localizations",
"createdAt",
"createdBy",
"updatedAt",
"updatedBy",
"publishedAt",
]);
// set dynamic zone compoonent keys again
// dynamicZoneComponentKeys.forEach((v) => {
// set(mergedCreateEntry, v.key, v.component);
// });

mergedCreateEntry.locale = isoStrapi;
const createdEntry =
await StrapiI18nService.createLocalizationForAnExistingEntry(
ctx,
uid,
baseEntry,
createEntry,
mergedCreateEntry,
);

await this.updateEntry(
uid,
createdEntry.id,
strapiContentTypesModels,
translatedModel,
baseEntry,
isoStrapi,
);
// await this.updateEntry(
// uid,
// createdEntry.id,
// strapiContentTypesModels,
// translatedModel,
// baseEntry,
// isoStrapi,
// );

return createdEntry;
},
Expand Down
133 changes: 98 additions & 35 deletions server/utils/parsed-localazy-entry-to-create-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ const parsedLocalazyEntryToCreateEntry = (
const toCreateEntry = (
entry,
model,
baseEntry,
baseEntry, // this does not need to be passed, can be referenced from the closure
// eslint-disable-next-line no-unused-vars
key = "",
prefix = "",
component = "",
isRepeatableComponent = false,
isDZ = false,
isInsideDZ = false,
) => {
if (Array.isArray(entry)) {
// is array
Expand Down Expand Up @@ -78,6 +79,28 @@ const parsedLocalazyEntryToCreateEntry = (
}
}
});
} else if (isDZ) {
// TODO: implement DZ functionality
Object.entries(entry).forEach(([dzEntryIdWithComponent, value]) => {
let [dzEntryId, dzEntryComponent] = dzEntryIdWithComponent.split(";");
dzEntryId = parseInt(dzEntryId);
const baseEntryDZEntry = get(baseEntry, prefix).find((v) => (v.id === dzEntryId) && (v.__component === dzEntryComponent));
if (baseEntryDZEntry !== undefined) {
const dzEntryComponentModel = findModel(models, dzEntryComponent);
toCreateEntry(
value,
dzEntryComponentModel,
baseEntry,
dzEntryId,
`${prefix}.${dzEntryId}`,
dzEntryComponent,
true,
false, // we're already inside, so it's falsy
true,
);
}
// ? don't do anything if baseEntryDZEntry is undefined - it means that the entry was deleted
});
} else {
// is object
Object.entries(entry).forEach(([objectKey, value]) => {
Expand All @@ -86,38 +109,70 @@ const parsedLocalazyEntryToCreateEntry = (
// logicaly don't anything, skip...
} else if (isComponent(attribute)) {
// is component
const component = attribute.component;
const componentModel = findModel(models, component);
if (isRepeatable(attribute)) {
// is repeatable - array
const newPrefix = prefix
? `${prefix}.${objectKey}`
: `${objectKey}`;
toCreateEntry(
value,
componentModel,
baseEntry,
objectKey,
newPrefix,
component,
true
);
} else {
// is no repeatable - object
const newPrefix = prefix
? `${prefix}.${objectKey}`
: `${objectKey}`;
set(createEntry, `${newPrefix}.__component`, component);
toCreateEntry(
value,
componentModel,
baseEntry,
objectKey,
newPrefix,
component,
false
);
console.log("func key", key);
console.log("func comp", component);
const innerComponent = attribute.component;
const componentModel = findModel(models, innerComponent);

let newPrefixBase = prefix;
let newPrefix = newPrefixBase
? `${newPrefixBase}.${objectKey}`
: `${objectKey}`;
if (isInsideDZ) {
let [dzParamKey, dzEntryId] = newPrefixBase.split(".");
dzEntryId = parseInt(dzEntryId);
const baseEntryDZIndex = get(baseEntry, dzParamKey).findIndex((v) => (v.__component === component) && (v.id === dzEntryId)); // ? TODO: what if it's not found?
newPrefixBase = `${dzParamKey}.${baseEntryDZIndex}`;
newPrefix = `${newPrefixBase}.${objectKey}`;
}
const isRepeatableComponent = isRepeatable(attribute);
if (!isRepeatableComponent) {
set(createEntry, `${newPrefix}.__component`, innerComponent);
}
toCreateEntry(
value,
componentModel,
baseEntry,
objectKey,
newPrefix,
innerComponent,
isRepeatableComponent,
false,
isInsideDZ,
);
// if (isRepeatable(attribute)) {
// // TODO: need to count with possible inner-DZ components
// // is repeatable - array
// // const newPrefix = prefix
// // ? `${prefix}.${objectKey}`
// // : `${objectKey}`;
// toCreateEntry(
// value,
// componentModel,
// baseEntry,
// objectKey,
// newPrefix,
// innerComponent,
// true,
// false,
// isInsideDZ,
// );
// } else {
// // TODO: need to count with possible inner-DZ components
// // is no repeatable - object
// set(createEntry, `${newPrefix}.__component`, innerComponent);
// toCreateEntry(
// value,
// componentModel,
// baseEntry,
// objectKey,
// newPrefix,
// innerComponent,
// false,
// false,
// isInsideDZ,
// );
// }
} else if (isDynamicZone(attribute)) {
// behaves sort of like repeatable component
const newPrefix = prefix
Expand All @@ -134,14 +189,22 @@ const parsedLocalazyEntryToCreateEntry = (
true,
);
} else {
const newPrefix = prefix ? `${prefix}.${objectKey}` : `${objectKey}`;
let newPrefixBase = prefix;
let newPrefix = newPrefixBase ? `${newPrefixBase}.${objectKey}` : `${objectKey}`;
if (isInsideDZ) {
let [dzParamKey, dzEntryId] = newPrefixBase.split(".");
dzEntryId = parseInt(dzEntryId);
const baseEntryDZIndex = get(baseEntry, dzParamKey).findIndex((v) => (v.__component === component) && (v.id === dzEntryId)); // ? TODO: what if it's not found?
newPrefixBase = `${dzParamKey}.${baseEntryDZIndex}`;
newPrefix = `${newPrefixBase}.${objectKey}`;
}
set(createEntry, newPrefix, value);

if (component) {
const componentKeyToSet = `${prefix}.__component`;
const componentKeyToSet = `${newPrefixBase}.__component`;
set(createEntry, componentKeyToSet, component);

if (isDZ) {
if (isInsideDZ) {
const isKeyAdded = (typeof dynamicZoneComponentKeys.find((v) => v.key === componentKeyToSet) !== "undefined");
if (!isKeyAdded)
dynamicZoneComponentKeys.push({
Expand Down

0 comments on commit 22aa00f

Please sign in to comment.