fix: clean up upload components
This commit is contained in:
parent
2376fd8968
commit
03379943de
3 changed files with 10 additions and 26 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { Button, Collapse, Group, Progress, Title, useMantineTheme } from '@mantine/core';
|
import { Button, Collapse, Group, Progress, Title } from '@mantine/core';
|
||||||
import { randomId, useClipboard } from '@mantine/hooks';
|
import { randomId, useClipboard } from '@mantine/hooks';
|
||||||
import { useNotifications } from '@mantine/notifications';
|
import { useNotifications } from '@mantine/notifications';
|
||||||
import Dropzone from 'components/dropzone/Dropzone';
|
import Dropzone from 'components/dropzone/Dropzone';
|
||||||
|
@ -20,8 +20,8 @@ export default function Upload() {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
window.addEventListener('paste', (e: ClipboardEvent) => {
|
window.addEventListener('paste', (e: ClipboardEvent) => {
|
||||||
const item = Array.from(e.clipboardData.items).find(x => /^image/.test(x.type));
|
const item = Array.from(e.clipboardData.items).find(x => /^image/.test(x.type));
|
||||||
const blob = item.getAsFile();
|
const file = item.getAsFile();
|
||||||
setFiles([...files, new File([blob], blob.name, { type: blob.type })]);
|
setFiles([...files, file]);
|
||||||
notif.showNotification({
|
notif.showNotification({
|
||||||
title: 'Image imported from clipboard',
|
title: 'Image imported from clipboard',
|
||||||
message: '',
|
message: '',
|
||||||
|
@ -94,7 +94,7 @@ export default function Upload() {
|
||||||
</Collapse>
|
</Collapse>
|
||||||
|
|
||||||
<Group position='right'>
|
<Group position='right'>
|
||||||
<Button leftIcon={<UploadIcon />} mt={12} onClick={handleUpload}>Upload</Button>
|
<Button leftIcon={<UploadIcon />} mt={12} onClick={handleUpload} disabled={files.length === 0 ? true : false}>Upload</Button>
|
||||||
</Group>
|
</Group>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,31 +1,20 @@
|
||||||
import { Button, Collapse, Group, Progress, Select, Title, useMantineTheme } from '@mantine/core';
|
import { Button, Group, LoadingOverlay, Select, Title } from '@mantine/core';
|
||||||
import { randomId, useClipboard } from '@mantine/hooks';
|
|
||||||
import { useNotifications } from '@mantine/notifications';
|
import { useNotifications } from '@mantine/notifications';
|
||||||
import CodeInput from 'components/CodeInput';
|
import CodeInput from 'components/CodeInput';
|
||||||
import Dropzone from 'components/dropzone/Dropzone';
|
|
||||||
import FileDropzone from 'components/dropzone/DropzoneFile';
|
|
||||||
import { TypeIcon, UploadIcon } from 'components/icons';
|
import { TypeIcon, UploadIcon } from 'components/icons';
|
||||||
import Link from 'components/Link';
|
import Link from 'components/Link';
|
||||||
import exts from 'lib/exts';
|
import exts from 'lib/exts';
|
||||||
import { useStoreSelector } from 'lib/redux/store';
|
import { useStoreSelector } from 'lib/redux/store';
|
||||||
import { useEffect, useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
|
||||||
export default function Upload() {
|
export default function Upload() {
|
||||||
const notif = useNotifications();
|
const notif = useNotifications();
|
||||||
const clipboard = useClipboard();
|
|
||||||
const user = useStoreSelector(state => state.user);
|
const user = useStoreSelector(state => state.user);
|
||||||
|
|
||||||
|
|
||||||
const [progress, setProgress] = useState(0);
|
|
||||||
const [loading, setLoading] = useState(false);
|
|
||||||
const [value, setValue] = useState('');
|
const [value, setValue] = useState('');
|
||||||
const [lang, setLang] = useState('txt');
|
const [lang, setLang] = useState('txt');
|
||||||
console.log(lang);
|
|
||||||
|
|
||||||
const handleUpload = async () => {
|
const handleUpload = async () => {
|
||||||
setProgress(0);
|
|
||||||
setLoading(true);
|
|
||||||
|
|
||||||
const file = new File([value], 'text.' + lang);
|
const file = new File([value], 'text.' + lang);
|
||||||
|
|
||||||
const id = notif.showNotification({
|
const id = notif.showNotification({
|
||||||
|
@ -36,16 +25,9 @@ export default function Upload() {
|
||||||
});
|
});
|
||||||
|
|
||||||
const req = new XMLHttpRequest();
|
const req = new XMLHttpRequest();
|
||||||
req.upload.addEventListener('progress', e => {
|
|
||||||
if (e.lengthComputable) {
|
|
||||||
setProgress(Math.round(e.loaded / e.total * 100));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
req.addEventListener('load', e => {
|
req.addEventListener('load', e => {
|
||||||
// @ts-ignore not sure why it thinks response doesnt exist, but it does.
|
// @ts-ignore not sure why it thinks response doesnt exist, but it does.
|
||||||
const json = JSON.parse(e.target.response);
|
const json = JSON.parse(e.target.response);
|
||||||
setLoading(false);
|
|
||||||
|
|
||||||
if (!json.error) {
|
if (!json.error) {
|
||||||
notif.updateNotification(id, {
|
notif.updateNotification(id, {
|
||||||
|
@ -69,7 +51,7 @@ export default function Upload() {
|
||||||
|
|
||||||
<CodeInput
|
<CodeInput
|
||||||
value={value}
|
value={value}
|
||||||
onChange={e => setValue(e.target.value)}
|
onChange={e => setValue(e.target.value.trim())}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Group position='right' mt='md'>
|
<Group position='right' mt='md'>
|
||||||
|
@ -80,7 +62,7 @@ export default function Upload() {
|
||||||
data={Object.keys(exts).map(x => ({ value: x, label: exts[x] }))}
|
data={Object.keys(exts).map(x => ({ value: x, label: exts[x] }))}
|
||||||
icon={<TypeIcon />}
|
icon={<TypeIcon />}
|
||||||
/>
|
/>
|
||||||
<Button leftIcon={<UploadIcon />} onClick={handleUpload}>Upload</Button>
|
<Button leftIcon={<UploadIcon />} onClick={handleUpload} disabled={value.trim().length === 0 ? true : false}>Upload</Button>
|
||||||
</Group>
|
</Group>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
|
@ -40,6 +40,8 @@
|
||||||
"zip-env.d.ts",
|
"zip-env.d.ts",
|
||||||
"**/*.ts",
|
"**/*.ts",
|
||||||
"**/*.tsx",
|
"**/*.tsx",
|
||||||
|
"**/**/*.ts",
|
||||||
|
"**/**/*.tsx",
|
||||||
"prisma/seed.ts"
|
"prisma/seed.ts"
|
||||||
],
|
],
|
||||||
"exclude": [
|
"exclude": [
|
||||||
|
|
Loading…
Reference in a new issue