-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fe): add problem uploading ui with excel (#1468)
* feat: add file upload dialog * feat: configure apollo-upload-client
- Loading branch information
1 parent
e52cc56
commit 17aaf13
Showing
7 changed files
with
233 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
frontend-client/app/admin/problem/_components/UploadDialog.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import { gql } from '@generated' | ||
import { Button } from '@/components/ui/button' | ||
import { | ||
Dialog, | ||
DialogClose, | ||
DialogContent, | ||
DialogTrigger | ||
} from '@/components/ui/dialog' | ||
import { useMutation } from '@apollo/client' | ||
import { UploadIcon, UploadCloudIcon } from 'lucide-react' | ||
import { useRef, useState } from 'react' | ||
import { createPortal } from 'react-dom' | ||
import { RiFileExcel2Fill } from 'react-icons/ri' | ||
import { useDrop } from 'react-use' | ||
import { toast } from 'sonner' | ||
|
||
interface Props { | ||
refetch: () => Promise<unknown> | ||
} | ||
|
||
const UPLOAD_PROBLEMS = gql(` | ||
mutation uploadProblems ($groupId: Int!, $input: UploadFileInput!) { | ||
uploadProblems(groupId: $groupId, input: $input) { | ||
id | ||
} | ||
} | ||
`) | ||
|
||
export default function UploadDialog({ refetch }: Props) { | ||
const [file, setFile] = useState<File | null>(null) | ||
const fileRef = useRef<HTMLInputElement | null>(null) | ||
|
||
const state = useDrop({ | ||
onFiles: (files) => { | ||
if (files.length > 1) { | ||
toast.error('Only one file is allowed') | ||
} | ||
const file = files[0] | ||
if (file.name.endsWith('.xlsx')) { | ||
setFile(files[0]) | ||
} else { | ||
toast.error('Only .xlsx files are allowed') | ||
} | ||
} | ||
}) | ||
|
||
const openFileBrowser = () => { | ||
fileRef.current?.click() | ||
} | ||
|
||
const setFileFromInput = () => { | ||
if (fileRef.current?.files) { | ||
setFile(fileRef.current.files[0]) | ||
} | ||
} | ||
|
||
const resetFile = () => { | ||
setFile(null) | ||
if (fileRef.current) fileRef.current.value = '' | ||
} | ||
|
||
const [uploadProblems, { loading }] = useMutation(UPLOAD_PROBLEMS) | ||
const uploadFile = async () => { | ||
try { | ||
await uploadProblems({ | ||
variables: { | ||
groupId: 1, | ||
input: { | ||
file | ||
} | ||
} | ||
}) | ||
} catch (error) { | ||
toast.error('Failed to upload file') | ||
return | ||
} | ||
toast.success('File uploaded successfully') | ||
document.getElementById('closeDialog')?.click() | ||
resetFile() | ||
await refetch() | ||
} | ||
|
||
return ( | ||
<Dialog> | ||
<DialogTrigger asChild> | ||
<Button variant="outline"> | ||
<UploadIcon className="mr-2 h-4 w-4" /> | ||
Upload | ||
</Button> | ||
</DialogTrigger> | ||
<DialogContent className="flex h-[24rem] w-[32rem] flex-col"> | ||
<h1 className="my-2 text-2xl font-bold">Upload Problem</h1> | ||
<p className="text-sm"> | ||
Please upload Excel file containing problem data. If you are looking | ||
for the required schema, you can download{' '} | ||
<a href="/sample.xlsx" download className="text-primary underline"> | ||
the sample file here. | ||
</a> | ||
</p> | ||
<input | ||
hidden | ||
type="file" | ||
ref={fileRef} | ||
accept=".xlsx" | ||
onChange={setFileFromInput} | ||
/> | ||
<DialogClose /> | ||
{file ? ( | ||
<section className="relative flex h-full w-full flex-col items-center justify-center gap-4"> | ||
<div className="flex min-w-60 items-center justify-center gap-2 border-4 border-dotted p-8 text-sm"> | ||
<RiFileExcel2Fill size={20} className="text-[#1D6F42]" /> | ||
{file.name} | ||
</div> | ||
<div className="flex gap-4"> | ||
<Button | ||
variant="ghost" | ||
size="sm" | ||
onClick={resetFile} | ||
disabled={loading} | ||
> | ||
Reset | ||
</Button> | ||
<Button | ||
variant="default" | ||
size="sm" | ||
onClick={uploadFile} | ||
disabled={loading} | ||
> | ||
Upload | ||
</Button> | ||
</div> | ||
{loading && ( | ||
<div className="absolute left-0 top-0 h-full w-full bg-white/20"> | ||
<div className="" /> {/* spinner */} | ||
</div> | ||
)} | ||
</section> | ||
) : ( | ||
<section className="flex h-full w-full flex-col items-center justify-center rounded-lg bg-slate-100"> | ||
<UploadCloudIcon className="h-16 w-16 text-slate-800" /> | ||
<p className="text-sm font-semibold">Drag and Drop</p> | ||
<p className="text-sm">or</p> | ||
<Button | ||
variant="outline" | ||
className="mt-2 text-sm" | ||
size="sm" | ||
onClick={openFileBrowser} | ||
> | ||
Browse | ||
</Button> | ||
</section> | ||
)} | ||
{state.over && | ||
typeof window === 'object' && | ||
createPortal( | ||
<div className="fixed left-0 top-0 z-50 grid h-dvh w-dvw place-items-center bg-slate-500/50 text-5xl font-bold backdrop-blur"> | ||
Drop file here | ||
</div>, | ||
document.body | ||
)} | ||
</DialogContent> | ||
</Dialog> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.