Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset form to initial state upon submitting #245

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading