prime
prime copied to clipboard
Bug: Adding document with a slice doesn't work
I tried to add a slice to the document and when I am saving the slice and refresh the document the slice is gone and not saved.
This problem occurs when slices are not marked as 'Allow multiple slices'. Isolated the problem to this code fragment in prime-core/src/utils/DocumentTransformer.ts
. For some reason, even though 'Allow multiple slices' is disabled, the value of the field is an array with one value, so the else case is triggered; and value.__inputname
is undefined, causing the save to fail.
if (field.type === 'slice') {
if (options.multiple && Array.isArray(value)) {
value = (await Promise.all(
value.map(async item => {
const sfields = await this.getFields({ id: item.__inputname } as any);
if (sfields) {
return await this.transform(sfields, item, schema, io, Types.SLICE);
}
return {};
})
)).filter(item => Object.keys(item).length > 0);
} else {
const sfields = await this.getFields({ id: value.__inputname } as any);
value = await this.transform(sfields, value, schema, io, Types.SLICE);
if (Object.keys(value).length === 0) {
continue;
}
}
}
Same as #244 ?
Was looking at this the other night. Transform is a recursive method that iterates over all the fields and resolves their value. Both groups and slices are handled in a very similar manner and both contain an option which allows or restricts repeated / multiples. However, both handle the side effects of toggling this option quite differently. The following steps can be followed to reproduce issue described in part here:
- Create new slice
- Add slice to a content type
- Set allow multiple
- Add multiple slices to a document
- Update the slice to *not allow multiple
- When viewing the document or document list the following error is thrown
"The loader.load() function must be called with a value,but got: undefined."
When following the same steps for groups we reach a different outcome; when the repeated option is toggled with data already entered only the first value (of the array) is rendered. When the repeated option is toggled back we see the previously entered data, no loss no hard fail etc.
The following PR #361 361 aims to handle slices and groups in a similar manner.