Better handling of file uploads
This commit is contained in:
parent
f561089cad
commit
b4c64d3dc0
3 changed files with 32 additions and 34 deletions
|
@ -66,6 +66,11 @@ export function httpErrorToHuman (error: any): string {
|
||||||
if (data.errors && data.errors[0] && data.errors[0].detail) {
|
if (data.errors && data.errors[0] && data.errors[0].detail) {
|
||||||
return data.errors[0].detail;
|
return data.errors[0].detail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Errors from wings directory, mostly just for file uploads.
|
||||||
|
if (data.error && typeof data.error === 'string') {
|
||||||
|
return data.error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return error.message;
|
return error.message;
|
||||||
|
|
|
@ -8,6 +8,8 @@ import styled from 'styled-components/macro';
|
||||||
import { ModalMask } from '@/components/elements/Modal';
|
import { ModalMask } from '@/components/elements/Modal';
|
||||||
import Fade from '@/components/elements/Fade';
|
import Fade from '@/components/elements/Fade';
|
||||||
import useEventListener from '@/plugins/useEventListener';
|
import useEventListener from '@/plugins/useEventListener';
|
||||||
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
||||||
|
import useFlash from '@/plugins/useFlash';
|
||||||
|
|
||||||
const InnerContainer = styled.div`
|
const InnerContainer = styled.div`
|
||||||
max-width: 600px;
|
max-width: 600px;
|
||||||
|
@ -17,6 +19,8 @@ const InnerContainer = styled.div`
|
||||||
export default () => {
|
export default () => {
|
||||||
const { uuid } = useServer();
|
const { uuid } = useServer();
|
||||||
const [ visible, setVisible ] = useState(false);
|
const [ visible, setVisible ] = useState(false);
|
||||||
|
const [ loading, setLoading ] = useState(false);
|
||||||
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
||||||
|
|
||||||
useEventListener('dragenter', e => {
|
useEventListener('dragenter', e => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
|
@ -41,42 +45,33 @@ export default () => {
|
||||||
|
|
||||||
const onFileDrop = (e: React.DragEvent<HTMLDivElement>) => {
|
const onFileDrop = (e: React.DragEvent<HTMLDivElement>) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
e.stopPropagation();
|
||||||
|
|
||||||
|
setVisible(false);
|
||||||
if (e.dataTransfer === undefined || e.dataTransfer === null) {
|
if (e.dataTransfer === undefined || e.dataTransfer === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const files: FileList = e.dataTransfer.files;
|
const form = new FormData();
|
||||||
console.log(files);
|
Array.from(e.dataTransfer.files).forEach(file => form.append('files', file));
|
||||||
|
|
||||||
const formData = new FormData();
|
|
||||||
|
|
||||||
for (let i = 0; i < files.length; i++) {
|
|
||||||
console.log(files[i]);
|
|
||||||
// @ts-ignore
|
|
||||||
formData.append('files', files[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
setLoading(true);
|
||||||
|
clearFlashes('files');
|
||||||
getFileUploadUrl(uuid)
|
getFileUploadUrl(uuid)
|
||||||
.then(url => {
|
.then(url => axios.post(url, form, {
|
||||||
console.log(url);
|
headers: {
|
||||||
|
'Content-Type': 'multipart/form-data',
|
||||||
axios.post(url, formData, {
|
},
|
||||||
headers: {
|
}))
|
||||||
'Content-Type': 'multipart/form-data',
|
.then(res => {
|
||||||
},
|
console.log(res);
|
||||||
})
|
|
||||||
.then(res => {
|
|
||||||
console.log(res);
|
|
||||||
setVisible(false);
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
console.error(error);
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
});
|
clearAndAddHttpError({ error, key: 'files' });
|
||||||
|
})
|
||||||
|
.then(() => setVisible(false))
|
||||||
|
.then(() => setLoading(false));
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -88,10 +83,7 @@ export default () => {
|
||||||
key={'upload_modal_mask'}
|
key={'upload_modal_mask'}
|
||||||
unmountOnExit
|
unmountOnExit
|
||||||
>
|
>
|
||||||
<ModalMask
|
<ModalMask onClick={() => setVisible(false)} onDrop={onFileDrop} onDragOver={e => e.preventDefault()}>
|
||||||
onClick={() => setVisible(false)}
|
|
||||||
onDrop={onFileDrop}
|
|
||||||
>
|
|
||||||
<div css={tw`w-full flex items-center justify-center`} style={{ pointerEvents: 'none' }}>
|
<div css={tw`w-full flex items-center justify-center`} style={{ pointerEvents: 'none' }}>
|
||||||
<InnerContainer>
|
<InnerContainer>
|
||||||
<p css={tw`text-lg text-neutral-200 text-center`}>
|
<p css={tw`text-lg text-neutral-200 text-center`}>
|
||||||
|
@ -101,6 +93,7 @@ export default () => {
|
||||||
</div>
|
</div>
|
||||||
</ModalMask>
|
</ModalMask>
|
||||||
</Fade>
|
</Fade>
|
||||||
|
<SpinnerOverlay visible={loading} size={'large'}/>
|
||||||
<Button css={tw`mr-2`} onClick={() => setVisible(true)}>
|
<Button css={tw`mr-2`} onClick={() => setVisible(true)}>
|
||||||
Upload
|
Upload
|
||||||
</Button>
|
</Button>
|
||||||
|
|
|
@ -8,13 +8,13 @@ export default (eventName: string, handler: (e: Event | CustomEvent | UIEvent |
|
||||||
}, [ handler ]);
|
}, [ handler ]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const isSupported = document && document.addEventListener;
|
const isSupported = window && window.addEventListener;
|
||||||
if (!isSupported) return;
|
if (!isSupported) return;
|
||||||
|
|
||||||
const eventListener = (event: any) => savedHandler.current(event);
|
const eventListener = (event: any) => savedHandler.current(event);
|
||||||
document.addEventListener(eventName, eventListener, options);
|
window.addEventListener(eventName, eventListener, options);
|
||||||
return () => {
|
return () => {
|
||||||
document.removeEventListener(eventName, eventListener);
|
window.removeEventListener(eventName, eventListener);
|
||||||
};
|
};
|
||||||
}, [ eventName, document ]);
|
}, [ eventName, window ]);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue