Skip to content

Commit

Permalink
Merge pull request #81 from skorphil/69-add-form-submit-success
Browse files Browse the repository at this point in the history
implemented successful submit toast
  • Loading branch information
skorphil committed Mar 12, 2024
2 parents 7db4240 + 8df3ef6 commit ccd26c3
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 10 deletions.
3 changes: 2 additions & 1 deletion components/InstitutionTab/InstitutionTab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export const InstitutionTab = forwardRef(
const institutionFields =
institutionsFieldArray.fields[getInstitutionIndex(institutionName)];
const institutionDefaultValues =
defaultValues.institutions[getInstitutionIndex(institutionName)];
defaultValues.institutions?.[getInstitutionIndex(institutionName)] ??
null;
const institutionCurrentValues = useWatch({
control,
name: institutionName,
Expand Down
23 changes: 19 additions & 4 deletions components/RecordForm/RecordForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Root element of the form
import { useForm, FormProvider, useFieldArray } from "react-hook-form";
import classes from "./RecordForm.module.css";
import { Button, Progress, useToast } from "@chakra-ui/react";
import { useState, useEffect } from "react";
import { useState } from "react";

import { InstitutionsList } from "components/InstitutionsList";
import { FormHeader } from "~/FormHeader";
Expand All @@ -22,7 +22,7 @@ export function RecordForm() {
const [isInstitutionOpen, setIsInstitutionOpen] = useState(false);
const [selectedInstitutionIndex, setSelectedInstitutionIndex] = useState(0);
const [errorState, setErrorState] = useState(false);

const toast = useToast({ position: "top" });
const router = useRouter();
const arrayName = "institutions";

Expand Down Expand Up @@ -88,8 +88,23 @@ export function RecordForm() {
Cancel
</Button>
<Button
onClick={formMethods.handleSubmit((data) => {
appendRecord(data);
onClick={formMethods.handleSubmit(async (data) => {
try {
await appendRecord(data);
router.push("/");
toast({
title: "Record saved",
status: "success",
duration: 4000,
});
} catch (error) {
toast({
title: "Error saving record",
description: error.message,
status: "error",
duration: 4000,
});
}
})}
>
Save
Expand Down
1 change: 0 additions & 1 deletion public/next.svg

This file was deleted.

1 change: 0 additions & 1 deletion public/vercel.svg

This file was deleted.

2 changes: 1 addition & 1 deletion serverActions/appendRecord.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ export async function appendRecord(formData) {
await record.save();
console.log("Document saved to db");
} catch (error) {
throw new Error("Error saving document:", error);
throw new Error("Error saving document:", error.message);
}
}
4 changes: 2 additions & 2 deletions serverActions/getLatestRecord.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ export async function getLatestRecord() {
.sort({ createdAt: -1 })
.limit(1)
.exec();
const institutionsList = latestRecord[0].institutions.toObject({
const institutionsList = latestRecord[0]?.institutions.toObject({
transform: function (doc, ret) {
delete ret._id;
return ret;
},
});

const initialValues = {
institutions: institutionsList.map((institution) => ({
institutions: institutionsList?.map((institution) => ({
...institution,
isDeleted: false,
})),
Expand Down

0 comments on commit ccd26c3

Please sign in to comment.