Skip to content

Commit

Permalink
Reset form to initial state upon submitting (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
hieungo89 authored Nov 15, 2023
1 parent 9734180 commit b8d982f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
31 changes: 23 additions & 8 deletions src/components/goal/Goal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,24 @@ const Goals = ({ goal }: GoalProps) => {
onSuccess: () => utils.iep.getSubgoals.invalidate(),
});

const handleSubGoalSubmit = (event: React.FormEvent<HTMLFormElement>) => {
const handleSubGoalSubmit = async (
event: React.FormEvent<HTMLFormElement>
) => {
event.preventDefault();
const data = new FormData(event.currentTarget);

subgoal.mutate({
goal_id: goal.goal_id,
description: data.get("description") as string,
instructions: data.get("instructions") as string,
target_max_attempts: Number(data.get("target_max_attempts")) || null,
});
try {
await subgoal.mutateAsync({
goal_id: goal.goal_id,
description: data.get("description") as string,
instructions: data.get("instructions") as string,
target_max_attempts: Number(data.get("target_max_attempts")) || null,
});

(event.target as HTMLFormElement).reset();
} catch (err) {
console.log("error: ", err);
}
};

if (isLoading) {
Expand Down Expand Up @@ -55,11 +63,18 @@ const Goals = ({ goal }: GoalProps) => {
placeholder="Subgoal description"
required
/>
<input type="text" name="instructions" placeholder="Instructions" />
<input
type="text"
name="instructions"
placeholder="Instructions"
required
/>
<input
type="number"
name="target_max_attempts"
placeholder="# of attempts"
min="1"
required
/>

<button type="submit" className={$goal.createButton}>
Expand Down
18 changes: 12 additions & 6 deletions src/components/iep/Iep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ const Iep = ({ iep_id }: IepProps) => {
onSuccess: () => utils.iep.getGoals.invalidate(),
});

const handleGoalSubmit = (event: React.FormEvent<HTMLFormElement>) => {
const handleGoalSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
const data = new FormData(event.currentTarget);

goalMutation.mutate({
iep_id: iep_id,
description: data.get("description") as string,
category: data.get("category") as string,
});
try {
await goalMutation.mutateAsync({
iep_id: iep_id,
description: data.get("description") as string,
category: data.get("category") as string,
});

(event.target as HTMLFormElement).reset();
} catch (err) {
console.log("error: ", err);
}
};

if (isLoading) {
Expand Down

0 comments on commit b8d982f

Please sign in to comment.