import client from 'part:@sanity/base/client'

function uploadAsset(assetType, asset) {
  return client.observable.assets.upload(assetType, asset.file)
    .map(event => {
      if (event.type === 'response') {
        // rewrite to a 'complete' event
        return {
          type: 'complete',
          id: event.body.document._id,
          asset: event.body.document
        }
      }
      return event
    })
}

export const uploadImage = imageAsset => uploadAsset('image', imageAsset)
export const uploadFile = fileAsset => uploadAsset('file', fileAsset)

export function materializeReference(id) {
  return client.observable
    .fetch('*[_id == $id] {_id, _type, url}', {id: id})
    .map(results => results[0])
}

import client from 'part:@sanity/base/client'

function uploadAsset(assetType, asset) {
  return client.observable.assets.upload(assetType, asset.file)
    .map(event => {
      if (event.type === 'response') {
        // rewrite to a 'complete' event
        return {
          type: 'complete',
          id: event.body.document._id,
          asset: event.body.document
        }
      }
      return event
    })
}

export const uploadImage = imageAsset => uploadAsset('image', imageAsset)
export const uploadFile = fileAsset => uploadAsset('file', fileAsset)

export function materializeReference(id) {
  return client.observable
    .getDocument(id)
}

Use .getDocument to fetch image record to avoid sync issues
[form-builder] Use .getDocument to fetch image record to avoid sync issues
