prime icon indicating copy to clipboard operation
prime copied to clipboard

Bug: Adding document with a slice doesn't work

Open bobsingor opened this issue 5 years ago • 3 comments

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.

bobsingor avatar Jun 24 '19 21:06 bobsingor

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;
          }
        }
      }

irisjae avatar Sep 12 '19 04:09 irisjae

Same as #244 ?

irisjae avatar Sep 12 '19 05:09 irisjae

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:

  1. Create new slice
  2. Add slice to a content type
  3. Set allow multiple
  4. Add multiple slices to a document
  5. Update the slice to *not allow multiple
  6. 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.

swim avatar Oct 08 '19 22:10 swim