Skip to content

Commit

Permalink
Implemented fetching error state in RecordForm
Browse files Browse the repository at this point in the history
- added new error component
- added new state to record form
  • Loading branch information
skorphil committed Mar 12, 2024
1 parent 63b1c2f commit 4a617d3
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 5 deletions.
57 changes: 57 additions & 0 deletions components/RecordForm/FetchingErrorState.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
VStack,
Image,
Heading,
Text,
Center,
Accordion,
AccordionButton,
AccordionItem,
AccordionIcon,
Box,
AccordionPanel,
} from "@chakra-ui/react";

export function FetchingErrorState({ errorMessage }) {
return (
<VStack justifyContent="space-between" h="100%">
<Center flexGrow={1}>
<VStack>
<Image
src="/server-down.svg"
alt="Server error illustration"
boxSize="240px"
/>
<Heading as="h1" size="md">
Cannot check previos record
</Heading>
<Text>Try to reload the page</Text>
</VStack>
</Center>

{errorMessage && (
<Accordion w="100%" allowToggle>
<AccordionItem borderBottomStyle="none">
<h2>
<AccordionButton>
<Box as="span" flex="1" textAlign="left">
System error message
</Box>
<AccordionIcon />
</AccordionButton>
</h2>
<AccordionPanel
fontFamily="mono"
fontSize="xs"
maxH="240px"
overflow="auto"
pb={4}
>
{errorMessage}
</AccordionPanel>
</AccordionItem>
</Accordion>
)}
</VStack>
);
}
13 changes: 12 additions & 1 deletion components/RecordForm/RecordForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,23 @@ import { handleInstitution } from "handlers";
import { useRouter } from "next/navigation";
import { appendRecord } from "serverActions/appendRecord";
import { getLatestRecord } from "serverActions/getLatestRecord";
import { FetchingErrorState } from "./FetchingErrorState";

export function RecordForm() {
const [isInstitutionOpen, setIsInstitutionOpen] = useState(false);
const [selectedInstitutionIndex, setSelectedInstitutionIndex] = useState(0);
const [errorState, setErrorState] = useState(false);
const router = useRouter();
const arrayName = "institutions";

const { control, ...form } = useForm({
defaultValues: async () => getLatestRecord(),
defaultValues: async () => {
try {
await getLatestRecord();
} catch (error) {
setErrorState(error.stack);
}
},
});
const institutionsFieldArray = useFieldArray({
control,
Expand Down Expand Up @@ -90,6 +98,8 @@ export function RecordForm() {
)}
{form.formState.isLoading ? (
<Progress size="xs" isIndeterminate />
) : errorState ? (
<FetchingErrorState errorMessage={errorState} />
) : (
<form className={classes.RecordForm}>
<InstitutionsList
Expand All @@ -98,6 +108,7 @@ export function RecordForm() {
/>
</form>
)}

{/* <DevTool control={formMethods.control} /> */}
</FormProvider>
);
Expand Down
4 changes: 4 additions & 0 deletions components/RecordForm/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ stateDiagram-v2
[*] --> loadingState
loadingState --> preFilled : fetched previousRecord
loadingState --> empty : fetched previousRecord == null
loadingState --> errorFetching
preFilled --> openedInstitution : BUTTON_PRESS
empty --> openedInstitution : BUTTON_PRESS
openedInstitution --> empty : BUTTON_PRESS
openedInstitution --> preFilled : BUTTON_PRESS
empty --> edited
preFilled --> edited
edited --> errorSubmitting
```

## See also
Expand Down
Loading

0 comments on commit 4a617d3

Please sign in to comment.