Skip to content

Commit 0f84d6a

Browse files
authored
Display spinner when uploading file (#15)
1 parent 3065c36 commit 0f84d6a

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/routes/UploadModel.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ vi.mock("react-router", async (importOriginal) => {
1313
return {
1414
...actual,
1515
useNavigate: () => mockUseNavigate,
16+
useParams: () => ({ propId: "cm1", visitId: 1 }),
1617
};
1718
});
1819

@@ -42,7 +43,7 @@ describe("Upload Model", () => {
4243

4344
await userEvent.click(screen.getByRole("button", { name: "Submit" }));
4445

45-
await waitFor(() => expect(mockUseNavigate).toHaveBeenCalledWith(-1));
46+
await waitFor(() => expect(mockUseNavigate).toHaveBeenCalledWith("/proposals/cm1/sessions/1"));
4647
});
4748

4849
it("should display toast if upload fails and server returns error details", async () => {

src/routes/UploadModel.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Button, Divider, Heading, useToast, VStack, Text, Code } from "@chakra-ui/react";
2-
import { FormEvent, useCallback } from "react";
2+
import { FormEvent, useCallback, useState } from "react";
33
import { useNavigate, useParams } from "react-router";
44

55
import "styles/upload.css";
@@ -8,21 +8,25 @@ import { client } from "utils/api/client";
88
export const UploadModelPage = () => {
99
const { propId, visitId } = useParams();
1010
const toast = useToast();
11+
const [loading, setLoading] = useState(false);
1112

1213
const navigate = useNavigate();
1314

1415
const uploadFile = useCallback(
1516
async (e: FormEvent<HTMLFormElement>) => {
1617
e.preventDefault();
1718
const data = new FormData(e.currentTarget);
19+
20+
setLoading(true);
1821
const resp = await client.post(
1922
`proposals/${propId}/sessions/${visitId}/processingModel`,
2023
data
2124
);
25+
setLoading(false);
2226

2327
if (resp.status === 200) {
2428
toast({ status: "success", title: "Model successfully uploaded!" });
25-
navigate(-1);
29+
navigate(`/proposals/${propId}/sessions/${visitId}`);
2630
} else {
2731
toast({
2832
status: "error",
@@ -44,7 +48,7 @@ export const UploadModelPage = () => {
4448
</Text>
4549
<form onSubmit={uploadFile} encType='multipart/form-data'>
4650
<input name='file' data-testid='file-input' type='file' accept='.h5' />
47-
<Button w='8em' type='submit'>
51+
<Button w='8em' type='submit' loadingText='Uploading' isLoading={loading}>
4852
Submit
4953
</Button>
5054
</form>

0 commit comments

Comments
 (0)