Skip to content

Commit

Permalink
Fix backend bugs: edit location actually saves changes
Browse files Browse the repository at this point in the history
  • Loading branch information
raissaji committed Nov 16, 2024
1 parent d2f8961 commit 4a55e20
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions frontend/src/components/LocationModal/LocationModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,16 @@ const LocationModal: React.FC<LocationModalProps> = ({
setIsOpen(true);
};

const methods = useForm();

const closeModal = () => {
methods.clearErrors();
reset();
setIsOpen(false);
};

useEffect(() => {
if (!isOpen) {
reset();
}
}, [isOpen, reset]);

const onSubmit: SubmitHandler<FormData> = async (data) => {
const url = existingLocation
? `/api/locations/${existingLocation.id}`
: '/api/locations';

const method = existingLocation ? axios.put : axios.post;

const newLocation = await method(url, data).then((res) => res.data);
Expand All @@ -98,9 +91,10 @@ const LocationModal: React.FC<LocationModalProps> = ({
onAddLocation(newLocation.data);
showToast('Location has been added.', ToastStatus.SUCCESS);
} else if (existingLocation && onEditLocation) {
onEditLocation(newLocation);
onEditLocation(newLocation.data);
showToast('Location has been updated.', ToastStatus.SUCCESS);
}

closeModal();
};

Expand Down

0 comments on commit 4a55e20

Please sign in to comment.