#Custom field cell image upload on drop

4 messages · Page 1 of 1 (latest)

cunning sonnet
#

Hi, i'm trying to make a custom field cell with a dropzone that would upload a new media and add it to the item images.

I didn't find a way to combine local API and client behavior (drop) , so I'm fetching the api, not sure if that's the way.

I'm stuck at the item update trough PATCH. What would be the syntax to add a new relation to an images array field ?

const fd = new FormData();
fd.append('file', file, file.name);
fd.append('data', JSON.stringify({}));
const createRes = await fetch(`${API_ROOT}/api/media`, {
  method: 'POST',
  body: fd,
  credentials: 'include',
});
const media = await createRes.json();
await fetch(`${API_ROOT}/api/mycoll/${rowData.id}`, {
  method: 'PATCH',
  headers: { 'Content-Type': 'application/json' },
  credentials: 'include',
  body: JSON.stringify({
    images: {
      relationTo: 'media',
      value: media.doc
    },
  }),
});
{
  name: 'images',
  type: 'array',
  label: 'Images',
  fields: [
    {
      name: 'image',
      type: 'upload',
      relationTo: 'media',
      label: 'Image',
      required: true
    }
  ]
}
verbal mossBOT
summer tide
#

Does images need to be an array in your PATCH body? Although, that may replace the array.

You could implement a custom endpoint and do this all in a single request.

cunning sonnet
#

Thanks for the help !

I succeeded trough a custom endpoint as your advice, as this gives me req/payload access.