Skip to content

Commit

Permalink
chore: added timeout to form state reset
Browse files Browse the repository at this point in the history
  • Loading branch information
jeafreezy committed Nov 10, 2024
1 parent 7ba5b10 commit bebebd7
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions frontend/src/app/providers/models-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import React, {
useContext,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import { useNavigate } from "react-router-dom";
Expand Down Expand Up @@ -206,11 +207,26 @@ export const ModelsProvider: React.FC<{
) => {
setFormData((prev) => ({ ...prev, [field]: value }));
};

const timeOutRef = useRef<number | null>(null);

useEffect(() => {
// Cleanup the timeout on component unmount
return () => {
if (timeOutRef.current) {
clearTimeout(timeOutRef.current);
}
};
}, []);

const trainingRequestMutation = useCreateModelTrainingRequest({
mutationConfig: {
onSuccess: () => {
showSuccessToast(TOAST_NOTIFICATIONS.trainingRequestSubmittedSuccess);
setFormData(initialFormState);
// delay for a few seconds before resetting the state
timeOutRef.current = setTimeout(() => {
setFormData(initialFormState);
}, 3000);
},
onError: (error) => {
showErrorToast(error);
Expand Down Expand Up @@ -241,6 +257,9 @@ export const ModelsProvider: React.FC<{
mutationConfig: {
onSuccess: (data) => {
showSuccessToast(TOAST_NOTIFICATIONS.modelCreationSuccess);
navigate(
`${APPLICATION_ROUTES.CREATE_NEW_MODEL_CONFIRMATION}?id=${data.id}`,
);
// Submit the model for training request
trainingRequestMutation.mutate({
model: data.id,
Expand All @@ -251,9 +270,6 @@ export const ModelsProvider: React.FC<{
zoom_level: formData.zoomLevels,
});

navigate(
`${APPLICATION_ROUTES.CREATE_NEW_MODEL_CONFIRMATION}?id=${data.id}`,
);
},
onError: (error) => {
showErrorToast(error);
Expand Down

0 comments on commit bebebd7

Please sign in to comment.