import client from '../../app-shell/client'

export const fetchFileList = () => async dispatch => {
  try {
    const data = await client.fetchFileList()

    dispatch({
      type: 'FILE-LIST',
      list: data.files
    })
  } catch (error) {
    console.error('file', error)
    dispatch({
      type: 'CLEAR-FILE-LIST'
    })
  }
}

/*
 ref. https://github.com/jaydenseric/graphql-multipart-request-spec#client
 - TODO support multiple file upload
 */
export const uploadFile = file => async dispatch => {
  try {
    const data = await client.uploadFile(file)

    dispatch(fetchFileList())
  } catch (e) {
    console.error(e)
  }
}
