-
Notifications
You must be signed in to change notification settings - Fork 673
Expand file tree
/
Copy pathbuildPaths.ts
More file actions
177 lines (148 loc) · 6.11 KB
/
Copy pathbuildPaths.ts
File metadata and controls
177 lines (148 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import type { CmsDynamicZoneTemplate, CmsEntryValues, CmsModelField } from "~/types/index.js";
import { immutableGet } from "@webiny/stdlib";
import { getBaseFieldType } from "~/utils/getBaseFieldType.js";
import type { GenericRecord } from "@webiny/api/types.js";
type INarrowedCmsModelField = Pick<CmsModelField, "fieldId" | "list" | "type" | "settings">;
interface IResolveBaseRefField<TValues extends CmsEntryValues = CmsEntryValues> {
collection: string[];
field: INarrowedCmsModelField;
parentPaths: string[];
input: TValues | TValues[];
isMultipleValues: boolean;
}
const resolveBaseRef = <TValues extends CmsEntryValues = CmsEntryValues>(
params: IResolveBaseRefField<TValues>
): string[] => {
const { field, parentPaths, input, collection, isMultipleValues } = params;
const parentPathsValue = parentPaths.length > 0 ? `${parentPaths.join(".")}.` : "";
if (field.list) {
const inputValue = immutableGet(input, `${field.fieldId}`, []);
if (!Array.isArray(inputValue)) {
return collection;
}
for (const key in inputValue) {
const path = `${parentPathsValue}${field.fieldId}.${key}`;
collection.push(path);
}
return collection;
}
if (isMultipleValues) {
for (const key in input) {
const path = `${parentPathsValue}${key}.${field.fieldId}`;
collection.push(path);
}
return collection;
}
collection.push(`${parentPathsValue}${field.fieldId}`);
return collection;
};
interface IBuildReferenceFieldPathsParams<TValues extends CmsEntryValues = CmsEntryValues> {
fields: INarrowedCmsModelField[];
parentPaths: string[];
input: TValues | TValues[];
}
export const buildReferenceFieldPaths = <TValues extends CmsEntryValues = CmsEntryValues>(
params: IBuildReferenceFieldPathsParams<TValues>
): string[] => {
const { fields, parentPaths: initialParentPaths, input } = params;
const parentPaths = [...initialParentPaths];
const isMultipleValues = Array.isArray(input);
return fields
.filter(field => ["object", "ref", "dynamicZone"].includes(getBaseFieldType(field)))
.reduce<string[]>((collection, field) => {
/**
* First we check the ref field
*/
const baseType = getBaseFieldType(field);
if (baseType === "ref") {
return resolveBaseRef({
collection,
field,
parentPaths,
input,
isMultipleValues
});
}
if (baseType === "dynamicZone") {
const templates: CmsDynamicZoneTemplate[] = field.settings?.templates || [];
if (field.list) {
const values = immutableGet(input, field.fieldId, []);
if (!Array.isArray(values)) {
return collection;
}
values.forEach((value, index) => {
const template = templates.find(tpl => tpl.id === value["_templateId"]);
if (!template) {
return;
}
const result = buildReferenceFieldPaths({
fields: template.fields,
input: value,
parentPaths: parentPaths.concat([field.fieldId, String(index)])
});
collection.push(...result);
});
return collection;
}
const value = immutableGet<GenericRecord>(input, field.fieldId, {});
if (!value) {
return collection;
}
const template = templates.find(tpl => {
return tpl.id === value["_templateId"];
});
if (!template) {
return collection;
}
const result = buildReferenceFieldPaths({
fields: template.fields,
input: value ?? {},
parentPaths: parentPaths.concat([field.fieldId])
});
collection.push(...result);
return collection;
}
/**
* Then we move onto the object field
*/
const parentPathsValue = parentPaths.length > 0 ? `${parentPaths.join(".")}.` : "";
/**
* This is if received input is array. We need to map key with fieldId at this point.
*/
if (isMultipleValues) {
for (const key in input) {
const path = `${parentPathsValue}${key}.${field.fieldId}`;
collection.push(path);
}
return collection;
}
const objFieldPath = `${field.fieldId}`;
const objFieldInputValue = immutableGet(input, objFieldPath, []);
/**
* If field is multiple values one, we need to go through the input and use the existing keys.
*/
if (field.list) {
if (Array.isArray(objFieldInputValue) === false) {
return collection;
}
for (const key in objFieldInputValue) {
const result = buildReferenceFieldPaths({
fields: field.settings?.fields || [],
input: objFieldInputValue[key],
parentPaths: parentPaths.concat([field.fieldId, key])
});
collection.push(...result);
}
return collection;
}
/**
* Single value reference field.
*/
const results = buildReferenceFieldPaths({
fields: field.settings?.fields || [],
input: objFieldInputValue,
parentPaths: parentPaths.concat([field.fieldId])
});
return collection.concat(results);
}, []);
};