Skip to content

Commit

Permalink
♻️ refactor: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
david-vaclavek committed Sep 7, 2023
1 parent 297906a commit b94ac3c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 68 deletions.
13 changes: 0 additions & 13 deletions server/controllers/localazy-transfer-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,26 +396,13 @@ module.exports = {

if (typeof modelContentTransferSetup !== "undefined" && shouldSetDownloadedProperty(modelContentTransferSetup, parsedKey.rest)) {
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;
// }

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

const setKey = [
isoStrapi,
parsedKey.uid,
parsedKey.id,
...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
18 changes: 8 additions & 10 deletions server/services/strapi-i18n-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { isoLocalazyToStrapi } = require("../utils/iso-locales-utils.js");
const intlDisplayName = require("../utils/intl-display-name.js");
const omitDeep = require("../utils/omit-deep.js");
const { forEach, map, find } = require("lodash")
const { forEach, find } = require("lodash")

module.exports = ({ strapi }) => ({
async getLocales(ctx = {}) {
Expand Down Expand Up @@ -85,9 +85,7 @@ module.exports = ({ strapi }) => ({
const newEntryLocale = newEntry.locale;
const filteredNewEntry = omitDeep(newEntry, [
"locale",
// "id", // keep id to fill-up for the missing fields in the localized entry
"createdAt",
// "publishedAt",
"updatedAt",
]);
filteredNewEntry.locale = newEntryLocale;
Expand All @@ -113,23 +111,23 @@ module.exports = ({ strapi }) => ({
},
async updateLocalizationForAnExistingEntry(uid, updateEntryId, data) {
try {

const StrapiService = strapi.plugin("localazy").service("strapiService");
const strapiContentTypesModels = await StrapiService.getModels();
const populate = await StrapiService.getPopulateObject(uid);

// Bugfix by <emanuele.c@dacoco.io>:
// Prevents IDs to be appended when updating a localized entry. When IDs are present
// Strapi attempts to update a not yet existent ID into the DB
if(strapiContentTypesModels) {
if (strapiContentTypesModels) {
const filtered = find(strapiContentTypesModels, (model) => model.uid === uid)
if(filtered?.attributes) {
for(let attribute in filtered.attributes) {
if(filtered.attributes[attribute]?.type === 'dynamiczone') {
if(!populate[attribute] || populate[attribute] !== 'deep') {
if (filtered?.attributes) {
for (let attribute in filtered.attributes) {
if (filtered.attributes[attribute]?.type === 'dynamiczone') {
if (!populate[attribute] || populate[attribute] !== 'deep') {
populate[attribute] = 'deep'
}
forEach(data[attribute], (prop) => { if(prop?.id) delete prop.id})
forEach(data[attribute], (prop) => { if (prop?.id) delete prop.id })
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/services/strapi-localazy-i18n-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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");
const { merge } = require("lodash");

module.exports = ({ strapi }) => ({
async createEntry(
Expand Down
51 changes: 7 additions & 44 deletions server/utils/parsed-localazy-entry-to-create-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const parsedLocalazyEntryToCreateEntry = (
const toCreateEntry = (
entry,
model,
baseEntry, // this does not need to be passed, can be referenced from the closure
// eslint-disable-next-line no-unused-vars
key = "",
prefix = "",
Expand All @@ -42,13 +41,13 @@ const parsedLocalazyEntryToCreateEntry = (
if (baseEntryRepeateableGroup !== undefined) {
const localizedEntryRepeatableItemPosition = baseEntryRepeateableGroup.findIndex((repeatableItem) => !!repeatableItem && repeatableItem.id === baseEntryRepeatableItemId);
if (localizedEntryRepeatableItemPosition > -1) {
toCreateEntry(value, model, baseEntry, localizedEntryRepeatableItemPosition, `${prefix}.${localizedEntryRepeatableItemPosition}`, component);
toCreateEntry(value, model, localizedEntryRepeatableItemPosition, `${prefix}.${localizedEntryRepeatableItemPosition}`, component);
}
}
}

if (component && !isRepeatableComponent) {
toCreateEntry(value, model, baseEntry, `${prefix}`, `${prefix}`);
toCreateEntry(value, model, `${prefix}`, `${prefix}`);
}

if (!component && isRepeatableComponent) {
Expand All @@ -67,7 +66,6 @@ const parsedLocalazyEntryToCreateEntry = (
toCreateEntry(
value,
dzEntryComponentModel,
baseEntry,
localizedEntryRepeatableItemPosition,
`${prefix}.${localizedEntryRepeatableItemPosition}`,
dzEntryComponent,
Expand All @@ -80,7 +78,6 @@ const parsedLocalazyEntryToCreateEntry = (
}
});
} else if (isDZ) {
// TODO: implement DZ functionality
Object.entries(entry).forEach(([dzEntryIdWithComponent, value]) => {
let [dzEntryId, dzEntryComponent] = dzEntryIdWithComponent.split(";");
dzEntryId = parseInt(dzEntryId);
Expand All @@ -90,7 +87,6 @@ const parsedLocalazyEntryToCreateEntry = (
toCreateEntry(
value,
dzEntryComponentModel,
baseEntry,
dzEntryId,
`${prefix}.${dzEntryId}`,
dzEntryComponent,
Expand Down Expand Up @@ -119,7 +115,8 @@ const parsedLocalazyEntryToCreateEntry = (
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?
// ? TODO: what if it's not found?
const baseEntryDZIndex = get(baseEntry, dzParamKey).findIndex((v) => (v.__component === component) && (v.id === dzEntryId));
newPrefixBase = `${dzParamKey}.${baseEntryDZIndex}`;
newPrefix = `${newPrefixBase}.${objectKey}`;
}
Expand All @@ -130,47 +127,13 @@ const parsedLocalazyEntryToCreateEntry = (
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 @@ -179,7 +142,6 @@ const parsedLocalazyEntryToCreateEntry = (
toCreateEntry(
value,
null, // model is evaluated later as it's dynamic (DZ)
baseEntry,
objectKey,
newPrefix,
"", // component is computed later as it's dynamic (DZ)
Expand All @@ -192,7 +154,8 @@ const parsedLocalazyEntryToCreateEntry = (
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?
// ? TODO: what if it's not found?
const baseEntryDZIndex = get(baseEntry, dzParamKey).findIndex((v) => (v.__component === component) && (v.id === dzEntryId));
newPrefixBase = `${dzParamKey}.${baseEntryDZIndex}`;
newPrefix = `${newPrefixBase}.${objectKey}`;
}
Expand All @@ -218,7 +181,7 @@ const parsedLocalazyEntryToCreateEntry = (
};

const model = findModel(models, uid);
toCreateEntry(parsedLocalazyEntry, model, baseEntry);
toCreateEntry(parsedLocalazyEntry, model);
if (locale) {
createEntry.locale = locale;
}
Expand Down

0 comments on commit b94ac3c

Please sign in to comment.