From 31074ac42e0147a31864c64da3a435b20c1df286 Mon Sep 17 00:00:00 2001 From: Sean Macfarlane Date: Thu, 11 Mar 2021 18:46:11 -0500 Subject: [PATCH] fixed image upload --- app/containers/ProfileDetails/index.js | 63 +++++++++++++++++++------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/app/containers/ProfileDetails/index.js b/app/containers/ProfileDetails/index.js index 65f8b2e..d9ebe6c 100644 --- a/app/containers/ProfileDetails/index.js +++ b/app/containers/ProfileDetails/index.js @@ -7,7 +7,6 @@ import { ProfileDetails as ProfileDetailsPage, Loading } from '@tip-wlan/wlan-cl import { ROUTES, AUTH_TOKEN } from 'constants/index'; import UserContext from 'contexts/UserContext'; import { GET_ALL_PROFILES, GET_API_URL } from 'graphql/queries'; -import { FILE_UPLOAD } from 'graphql/mutations'; import { fetchMoreProfiles } from 'graphql/functions'; import { getItem } from 'utils/localStorage'; @@ -133,8 +132,6 @@ const ProfileDetails = () => { const [updateProfile] = useMutation(UPDATE_PROFILE); const [deleteProfile] = useMutation(DELETE_PROFILE); - const [fileUpload] = useMutation(FILE_UPLOAD); - const handleDeleteProfile = () => { deleteProfile({ variables: { id } }) .then(() => { @@ -180,20 +177,45 @@ const ProfileDetails = () => { ); }; - const handleFileUpload = (fileName, file) => - fileUpload({ variables: { fileName, file } }) - .then(() => { - notification.success({ - message: 'Success', - description: 'File successfully uploaded.', - }); + const handleFileUpload = async (fileName, file) => { + const token = getItem(AUTH_TOKEN); + + if (apiUrl?.getApiUrl) { + fetch(`${apiUrl?.getApiUrl}filestore/${fileName}`, { + method: 'POST', + headers: { + Authorization: token ? `Bearer ${token.access_token}` : '', + 'Content-Type': 'application/octet-stream', + }, + body: file, }) - .catch(() => - notification.error({ - message: 'Error', - description: 'File could not be uploaded.', + .then(response => response.json()) + .then(resp => { + if (resp?.success) { + notification.success({ + message: 'Success', + description: 'File successfully uploaded.', + }); + } else { + notification.error({ + message: 'Error', + description: 'File could not be uploaded.', + }); + } }) - ); + .catch(() => { + notification.error({ + message: 'Error', + description: 'File could not be uploaded.', + }); + }); + } else { + notification.error({ + message: 'Error', + description: 'File could not be uploaded.', + }); + } + }; const handleDownloadFile = async name => { const token = getItem(AUTH_TOKEN); @@ -201,10 +223,19 @@ const ProfileDetails = () => { return fetch(`${apiUrl?.getApiUrl}filestore/${encodeURIComponent(name)}`, { method: 'GET', headers: { + 'Content-Type': 'application/octet-stream', Authorization: token ? `Bearer ${token.access_token}` : '', }, }) - .then(res => res.text()) + .then(response => response.blob()) + .then(blob => { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onloadend = () => resolve(reader.result); + reader.onerror = reject; + reader.readAsDataURL(blob); + }); + }) .catch(() => { notification.error({ message: 'Error',