@localazy/strapi-plugin 0.1.13-rc.3 → 0.1.13-rc.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@localazy/strapi-plugin",
3
- "version": "0.1.13-rc.3",
3
+ "version": "0.1.13-rc.4",
4
4
  "description": "The official Strapi Plugin by Localazy.",
5
5
  "strapi": {
6
6
  "name": "localazy",
@@ -12,8 +12,16 @@ const isDynamicZoneKey = (key, transferSetupModel) => {
12
12
  }
13
13
 
14
14
  const dynamicPropertyEntryIdWithSquareBrackets = matches[1];
15
- const dynamicPropertyNamePart = key.split(".").find((s, index) => index > 1 && s.includes(dynamicPropertyEntryIdWithSquareBrackets));
16
- const dynamicPropertyName = dynamicPropertyNamePart.replace(dynamicPropertyEntryIdWithSquareBrackets, "");
15
+ const dynamicPropertyNamePart = key
16
+ .split(".")
17
+ .find(
18
+ (s, index) =>
19
+ index > 1 && s.includes(dynamicPropertyEntryIdWithSquareBrackets)
20
+ );
21
+ const dynamicPropertyName = dynamicPropertyNamePart.replace(
22
+ dynamicPropertyEntryIdWithSquareBrackets,
23
+ ""
24
+ );
17
25
 
18
26
  return Array.isArray(transferSetupModel[dynamicPropertyName]);
19
27
  };
@@ -25,19 +33,31 @@ const getDynamicZoneEntryId = (key, transferSetupModel) => {
25
33
 
26
34
  const matches = key.match(/\[\d+\]+/g);
27
35
  // remove square brackets
28
- const id = matches[1].replace(/[\[\]']+/g, '');
36
+ const id = matches[1].replace(/[\[\]']+/g, "");
29
37
  return id;
30
- }
38
+ };
39
+
40
+ const getUniqueDynamicZoneEntryId = (key, transferSetupModel) => {
41
+ if (!isDynamicZoneKey(key, transferSetupModel)) {
42
+ return undefined;
43
+ }
44
+
45
+ const keySplittedByDot = key.split(".");
46
+
47
+ return keySplittedByDot[2];
48
+ };
31
49
 
32
50
  const doesPickPathsIncludeTheComponent = (pickPaths, dzParameterKey) => {
33
51
  return pickPaths.some((pp) => pp.includes(dzParameterKey));
34
- }
52
+ };
35
53
 
36
54
  const pickEntries = (flatten, pickPaths, transferSetupModel) => {
37
55
  // * dynamic zones properties must! always be in 1st level (not in component; Strapi restrictions)
38
56
 
39
57
  const dzEntryIdComponentMap = {};
40
- const flattenComponentProps = Object.fromEntries(Object.entries(flatten).filter(([key]) => key.includes('__component')));
58
+ const flattenComponentProps = Object.fromEntries(
59
+ Object.entries(flatten).filter(([key]) => key.includes("__component"))
60
+ );
41
61
 
42
62
  const pickedDeep = pickDeep(transferSetupModel, ["__component__"]);
43
63
  const flattenedPickedDeep = flattenObject(pickedDeep);
@@ -52,25 +72,41 @@ const pickEntries = (flatten, pickPaths, transferSetupModel) => {
52
72
  return;
53
73
  }
54
74
 
55
- const entryId = getDynamicZoneEntryId(key, transferSetupModel);
56
- dzEntryIdComponentMap[entryId] = {
75
+ const keySplittedByDot = key.replace(".__component", "").split(".");
76
+ const componentName = keySplittedByDot[keySplittedByDot.length - 1].replace(
77
+ /\[.*\]/g,
78
+ ""
79
+ );
80
+ const uniqueEntryId = getUniqueDynamicZoneEntryId(key, transferSetupModel);
81
+
82
+ const keyOfEntry = Object.keys(flattenedPickedDeep)
83
+ .filter((fpdk) => fpdk.replace(/\[.*\]/g, "") == componentName)
84
+ .find((fpdk) => flattenedPickedDeep[fpdk] === flattenComponentProps[key]);
85
+ dzEntryIdComponentMap[uniqueEntryId] = {
57
86
  component: flattenComponentProps[key],
58
- key: Object.keys(flattenedPickedDeep).find((fpdk) => flattenedPickedDeep[fpdk] === flattenComponentProps[key]),
87
+ key: keyOfEntry,
59
88
  };
60
89
  });
61
90
 
62
91
  const pickedEntries = {};
63
- const mappedPickPaths = pickPaths.map((pickPath) => pickPath.replace(/\[\d+\]/g, ""));
92
+ const mappedPickPaths = pickPaths.map((pickPath) =>
93
+ pickPath.replace(/\[\d+\]/g, "")
94
+ );
64
95
 
65
96
  // filter out "__component"
66
- const filteredFlatten = Object.fromEntries(Object.entries(flatten).filter(([key]) => !key.includes('__component')));
97
+ const filteredFlatten = Object.fromEntries(
98
+ Object.entries(flatten).filter(([key]) => !key.includes("__component"))
99
+ );
67
100
  Object.keys(filteredFlatten).forEach((key) => {
68
101
  // decide whether it is a dynamic zone
69
102
  // handle dynamic zones, in case of similarly-named fields across more components
70
103
  const isDynamicZone = isDynamicZoneKey(key, transferSetupModel);
71
104
  if (isDynamicZone) {
72
- const dzEntryId = getDynamicZoneEntryId(key, transferSetupModel);
73
- const dzParameterKey = dzEntryIdComponentMap[dzEntryId].key;
105
+ const uniqueDzEntryId = getUniqueDynamicZoneEntryId(
106
+ key,
107
+ transferSetupModel
108
+ );
109
+ const dzParameterKey = dzEntryIdComponentMap[uniqueDzEntryId].key;
74
110
 
75
111
  if (!doesPickPathsIncludeTheComponent(pickPaths, dzParameterKey)) {
76
112
  return;
@@ -81,11 +117,19 @@ const pickEntries = (flatten, pickPaths, transferSetupModel) => {
81
117
  if (mappedPickPaths.includes(filteredKey)) {
82
118
  if (isDynamicZone) {
83
119
  const dzEntryId = getDynamicZoneEntryId(key, transferSetupModel);
84
- const dzParameterKey = dzEntryIdComponentMap[dzEntryId].key;
85
-
86
- const dzParameterName = dzParameterKey.substring(0, dzParameterKey.indexOf('['));
120
+ const uniqueDzEntryId = getUniqueDynamicZoneEntryId(
121
+ key,
122
+ transferSetupModel
123
+ );
124
+ const dzParameterKey = dzEntryIdComponentMap[uniqueDzEntryId].key;
125
+
126
+ const dzParameterName = dzParameterKey.substring(
127
+ 0,
128
+ dzParameterKey.indexOf("[")
129
+ );
87
130
  const toBeReplaced = `${dzParameterName}[${dzEntryId}]`;
88
- const dzParameterComponent = dzEntryIdComponentMap[dzEntryId].component;
131
+ const dzParameterComponent =
132
+ dzEntryIdComponentMap[uniqueDzEntryId].component;
89
133
  const componentAddition = `${dzParameterName}[${dzEntryId};${dzParameterComponent}]`;
90
134
  const replacedKey = key.replace(toBeReplaced, componentAddition);
91
135
  pickedEntries[replacedKey] = flatten[key];