diff --git a/admin/src/modules/@common/models/supported-content-type-fields.js b/admin/src/modules/@common/models/supported-content-type-fields.js index ae17b5f..e24da67 100644 --- a/admin/src/modules/@common/models/supported-content-type-fields.js +++ b/admin/src/modules/@common/models/supported-content-type-fields.js @@ -3,6 +3,7 @@ const SUPPORTED_CONTENT_TYPE_FIELDS = [ "text", "richtext", "email", + "json", ]; export default SUPPORTED_CONTENT_TYPE_FIELDS; diff --git a/admin/src/modules/@common/utils/get-nav.js b/admin/src/modules/@common/utils/get-nav.js index 2171a2a..4c34ef3 100644 --- a/admin/src/modules/@common/utils/get-nav.js +++ b/admin/src/modules/@common/utils/get-nav.js @@ -5,7 +5,7 @@ import Upload from "@strapi/icons/Upload"; import pluginId from "../../../pluginId"; import i18n from "../../../i18n"; -const BASE_PATH = `${process.env.ADMIN_PATH}/plugins/${pluginId}`; +const BASE_PATH = `${process.env.ADMIN_PATH}plugins/${pluginId}`; const t = i18n.t; const gevNav = () => { return [ diff --git a/admin/src/modules/@common/utils/redirect-to-plugin-route.js b/admin/src/modules/@common/utils/redirect-to-plugin-route.js index 4641699..f88b63c 100644 --- a/admin/src/modules/@common/utils/redirect-to-plugin-route.js +++ b/admin/src/modules/@common/utils/redirect-to-plugin-route.js @@ -18,5 +18,5 @@ export default (route) => { return; } - history.push(`${process.env.ADMIN_PATH}/plugins/${pluginId}/${route}`); + history.push(`${process.env.ADMIN_PATH}plugins/${pluginId}/${route}`); }; diff --git a/admin/src/pages/App/index.js b/admin/src/pages/App/index.js index 712b393..f06be72 100644 --- a/admin/src/pages/App/index.js +++ b/admin/src/pages/App/index.js @@ -110,7 +110,7 @@ function App() { <> {!isLoggedIn && ( - + - + - + - + { return parsedKey[dynamicZoneKeySegmentIndex - 1]; }; +/** + * + * @param {*} modelContentTransferSetup + * @param {*} parsedKeyRest + * @returns "yes" | "no" | "json" + */ const shouldSetDownloadedProperty = ( modelContentTransferSetup, parsedKeyRest, @@ -50,11 +56,38 @@ const shouldSetDownloadedProperty = ( const slicedParsedKeyRest = parsedKeyRest.slice(dynamicZoneKeySegmentIndex + 1); const filteredSlicedParsedKeyRest = slicedParsedKeyRest.filter((segment) => isNaN(parseInt(segment))); - return !!get(dzContentTransferSetupComponentModel, filteredSlicedParsedKeyRest); + if(get(dzContentTransferSetupComponentModel, filteredSlicedParsedKeyRest) === true) { + return "yes"; + } + + // json fields + while(filteredSlicedParsedKeyRest.length > 0) { + // last segment out + filteredSlicedParsedKeyRest.pop(); + if(get(dzContentTransferSetupComponentModel, filteredSlicedParsedKeyRest) === true) { + return "json"; + } + } + + return "no"; } else { const filteredParsedKeyRest = parsedKeyRest .filter((partialKey) => isNaN(parseInt(partialKey))); - return !!get(modelContentTransferSetup, filteredParsedKeyRest); + + if(get(modelContentTransferSetup, filteredParsedKeyRest) === true) { + return "yes"; + } + + // json fields + while(filteredParsedKeyRest.length > 0) { + // last segment out + filteredParsedKeyRest.pop(); + if(get(modelContentTransferSetup, filteredParsedKeyRest) === true) { + return "json"; + } + } + + return "no"; } }; diff --git a/server/models/supported-content-type-fields.js b/server/models/supported-content-type-fields.js index 3f00414..ae906c0 100644 --- a/server/models/supported-content-type-fields.js +++ b/server/models/supported-content-type-fields.js @@ -3,6 +3,7 @@ const SUPPORTED_CONTENT_TYPE_FIELDS = [ "text", "richtext", "email", + "json", ]; module.exports = SUPPORTED_CONTENT_TYPE_FIELDS; diff --git a/server/services/localazy-transfer-download-service.js b/server/services/localazy-transfer-download-service.js index 5397deb..a409e2a 100644 --- a/server/services/localazy-transfer-download-service.js +++ b/server/services/localazy-transfer-download-service.js @@ -9,6 +9,7 @@ const { } = require("../utils/iso-locales-utils"); const shouldSetDownloadedProperty = require("../functions/should-set-downloaded-property"); const set = require("lodash/set"); +const get = require("lodash/get"); const isEmpty = require("lodash/isEmpty"); const RequestInitiatorHelper = require('../utils/request-initiator-helper'); const PluginSettingsServiceHelper = require('../services/helpers/plugin-settings-service-helper'); @@ -223,6 +224,7 @@ module.exports = ({ strapi }) => ({ */ const parsedLocalazyContent = {}; const strapiContentTypesModels = await StrapiService.getModels(); + const jsonFields = []; for (const [isoLocalazy, keys] of Object.entries(localazyContent)) { const isoStrapi = isoLocalazyToStrapi(isoLocalazy); if (!isoStrapi) { @@ -244,20 +246,79 @@ module.exports = ({ strapi }) => ({ parsedKey.uid ); - if (typeof modelContentTransferSetup !== "undefined" && shouldSetDownloadedProperty(modelContentTransferSetup, parsedKey.rest)) { - const parsedKeyRestWithoutComponents = parsedKey.rest; - const setKey = [ - isoStrapi, - parsedKey.uid, - parsedKey.id, - ...parsedKeyRestWithoutComponents, - ]; + if (typeof modelContentTransferSetup !== "undefined") { + const shouldSetDownloadedPropertyResult = shouldSetDownloadedProperty(modelContentTransferSetup, parsedKey.rest); + if (shouldSetDownloadedPropertyResult === "no") { + continue; + } + + let parsedKeyRestWithoutComponents = parsedKey.rest; + if (shouldSetDownloadedPropertyResult === "json") { + const setKey = [ + isoStrapi, + parsedKey.uid, + parsedKey.id, + ]; + + // handle dynamic zones and components here + let updatedParsedKeyRestWithoutComponents = parsedKeyRestWithoutComponents; + let segmentsToAdd = []; + for (const segment of parsedKeyRestWithoutComponents) { + const hasEntry = get(modelContentTransferSetup, [...segmentsToAdd, segment]); + const hasEntryNotFinal = hasEntry && typeof hasEntry !== "boolean"; + // const isDZ = Array.isArray(hasEntry) && hasEntry.every((entry) => entry.__component__); + // processed till here in iterations and `segment` is numeric + const isComponent = parseInt(segment) > 0; + + if (hasEntryNotFinal || (!hasEntry && isComponent)) { + segmentsToAdd.push(segment); + setKey.push(segment); + updatedParsedKeyRestWithoutComponents = updatedParsedKeyRestWithoutComponents.slice(1); + } else { + break; + } + } + parsedKeyRestWithoutComponents = updatedParsedKeyRestWithoutComponents; + + let foundJsonFieldIndex = jsonFields.findIndex((jsonField) => { + return jsonField.setKey.join() === setKey.join(); + }); + if (foundJsonFieldIndex === -1) { + jsonFields.push({ + setKey, + jsonKey: parsedKeyRestWithoutComponents[0], + jsonValue: {}, + }); + } - set(parsedLocalazyContent, setKey, value); + foundJsonFieldIndex = foundJsonFieldIndex === -1 ? jsonFields.length - 1 : foundJsonFieldIndex; + const foundJsonField = jsonFields[foundJsonFieldIndex]; + // rest from index 1 + const restSliced = parsedKeyRestWithoutComponents.slice(1); + set(foundJsonField, ['jsonValue', ...restSliced], value); + jsonFields[foundJsonFieldIndex] = foundJsonField; + } + + if (shouldSetDownloadedPropertyResult === "yes") { + const setKey = [ + isoStrapi, + parsedKey.uid, + parsedKey.id, + ...parsedKeyRestWithoutComponents, + ]; + set(parsedLocalazyContent, setKey, value); + } } } } + /** + * Set json fields if applicable + */ + for (const jsonField of jsonFields) { + set(parsedLocalazyContent, [...jsonField.setKey, jsonField.jsonKey], jsonField.jsonValue); + } + /** * Iterate over parsed Localazy content and insert/update content in Strapi */ diff --git a/server/utils/flatten-object.js b/server/utils/flatten-object.js index 777053e..f217a98 100644 --- a/server/utils/flatten-object.js +++ b/server/utils/flatten-object.js @@ -3,6 +3,10 @@ const flattenObject = (object, prefix = "") => { const result = {}; + if (typeof object === "string" || typeof object === "number" || typeof object === "boolean") { + return object; + } + for (const objectKey in object) { if (objectKey === "id") { continue; @@ -22,9 +26,9 @@ const flattenObject = (object, prefix = "") => { const flattenedArray = object[objectKey].reduce( (accumulator, item, index) => { // No id should use the index of the array item - let key = `${objectKey}[${item.id || index}]`; + let key = `${objectKey}[${item?.id || index}]`; // is Dynamic Zone - if (item.id && item.__component) { + if (item?.id && item?.__component) { key = `${objectKey}[${item.id};${item.__component}]`; } diff --git a/server/utils/pick-entries.js b/server/utils/pick-entries.js index 1c3635b..bc3ca29 100644 --- a/server/utils/pick-entries.js +++ b/server/utils/pick-entries.js @@ -22,7 +22,12 @@ const pickEntries = (flatten, pickPaths) => { if (isDynamicZoneKey(key)) { filteredKey = filteredKey.replace(/\[\d+;[\w-]+\./g, `[`); } - if (mappedPickPaths.includes(filteredKey)) { + // if (mappedPickPaths.includes(filteredKey)) { + if ( + mappedPickPaths.includes(filteredKey) || + // include JSON fields + mappedPickPaths.some((pickPath) => filteredKey.startsWith(`${pickPath}.`)) + ) { pickedEntries[key] = flatten[key]; } });