Skip to content

Commit

Permalink
rm unnecessary onErrors, modify existing ones
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacobjeevan committed Feb 19, 2025
1 parent 842f978 commit c4c4fb0
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 107 deletions.
2 changes: 2 additions & 0 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -943,11 +943,13 @@
"environment": "Environment",
"error_404": "Error 404",
"error_deleting_shifting": "Error while deleting Shifting record",
"error_failed_to_clone_questionnaire": "Failed to clone questionnaire. Please try again.",
"error_fetching_facility_data": "Error while fetching facility data",
"error_fetching_slots_data": "Error while fetching slots data",
"error_fetching_user_data": "Error while fetching user data",
"error_fetching_user_details": "Error while fetching user details: ",
"error_loading_questionnaire_response": "Error loading questionnaire response",
"error_slug_already_in_use": "This slug is already in use. Please choose a different one.",
"error_updating_encounter": "Error to Updating Encounter",
"error_verifying_otp": "Error while verifying OTP, Please request a new OTP",
"error_while_deleting_record": "Error while deleting record",
Expand Down
6 changes: 0 additions & 6 deletions src/components/Encounter/CreateEncounterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,6 @@ export default function CreateEncounterForm({
onSuccess?.();
navigate(`/facility/${facilityId}/encounter/${data.id}/updates`);
},
onError: (error) => {
const errorData = error.cause as { errors: { msg: string[] } };
errorData.errors.msg.forEach((er) => {
toast.error(er);
});
},
});

function onSubmit(data: z.infer<typeof encounterFormSchema>) {
Expand Down
12 changes: 0 additions & 12 deletions src/components/Patient/LinkDepartmentsSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,6 @@ export default function LinkDepartmentsSheet({
setOpen(false);
onUpdate?.();
},
onError: (error) => {
const errorData = error.cause as { errors: { msg: string }[] };
errorData.errors.forEach((er) => {
toast.error(er.msg);
});
},
});

const { mutate: removeOrganization, isPending: isRemoving } = useMutation({
Expand All @@ -149,12 +143,6 @@ export default function LinkDepartmentsSheet({
toast.success("Organization removed successfully");
onUpdate?.();
},
onError: (error) => {
const errorData = error.cause as { errors: { msg: string }[] };
errorData.errors.forEach((er) => {
toast.error(er.msg);
});
},
});

return (
Expand Down
12 changes: 0 additions & 12 deletions src/components/Patient/PatientDetailsTab/PatientUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ function AddUserSheet({ patientId }: AddUserSheetProps) {
setSelectedUser(undefined);
setSelectedRole("");
},
onError: (error) => {
const errorData = error.cause as { errors: { msg: string }[] };
errorData.errors.forEach((er) => {
toast.error(er.msg);
});
},
});

const handleAddUser = () => {
Expand Down Expand Up @@ -226,12 +220,6 @@ export const PatientUsers = (props: PatientProps) => {
});
toast.success("User removed successfully");
},
onError: (error) => {
const errorData = error.cause as { errors: { msg: string }[] };
errorData.errors.forEach((er) => {
toast.error(er.msg);
});
},
});

const ManageUsers = () => {
Expand Down
10 changes: 6 additions & 4 deletions src/components/Questionnaire/CloneQuestionnaireSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useMutation, useQuery } from "@tanstack/react-query";
import { Building, Check, Loader2 } from "lucide-react";
import { useNavigate } from "raviger";
import { useState } from "react";
import { useTranslation } from "react-i18next";

import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
Expand Down Expand Up @@ -46,6 +47,7 @@ export default function CloneQuestionnaireSheet({
const [error, setError] = useState<string | null>(null);
const [searchQuery, setSearchQuery] = useState("");
const [selectedIds, setSelectedIds] = useState<string[]>([]);
const { t } = useTranslation();

const { data: availableOrganizations, isLoading: isLoadingOrganizations } =
useQuery({
Expand All @@ -67,11 +69,11 @@ export default function CloneQuestionnaireSheet({
navigate(`/questionnaire/${data.slug}`);
setOpen(false);
},
onError: (error: any) => {
if (error.response?.status === 400) {
setError("This slug is already in use. Please choose a different one.");
onError: (error) => {
if (error.status === 400) {
setError(t("error_slug_already_in_use"));
} else {
setError("Failed to clone questionnaire. Please try again.");
setError(t("error_failed_to_clone_questionnaire"));
}
},
});
Expand Down
8 changes: 3 additions & 5 deletions src/components/Questionnaire/QuestionnaireForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,9 @@ export function QuestionnaireForm({
toast.success(t("questionnaire_submitted_successfully"));
onSubmit?.();
},
onError: (error) => {
const errorData = error.cause;
if (errorData?.results) {
handleSubmissionError(errorData.results as ValidationErrorResponse[]);
}
onError: (error: any) => {
const errorData = error.cause.results as ValidationErrorResponse[];
handleSubmissionError(errorData);
toast.error(t("questionnaire_submission_failed"));
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ export default function CreateFacilityOrganizationSheet({
setDescription("");
setOrgType("dept");
},
onError: (error) => {
const errorData = error.cause as { errors: { msg: string }[] };
errorData.errors.forEach((er) => {
toast.error(er.msg);
});
},
});

const handleSubmit = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,6 @@ export default function EditUserRoleSheet({
toast.success(t("user_role_update_success"));
setOpen(false);
},
onError: (error) => {
const errorData = error.cause as { errors: { msg: string[] } };
errorData.errors.msg.forEach((er) => {
toast.error(er);
});
},
});

const { mutate: removeRole } = useMutation({
Expand All @@ -105,12 +99,6 @@ export default function EditUserRoleSheet({
toast.success(t("user_removed_success"));
setOpen(false);
},
onError: (error) => {
const errorData = error.cause as { errors: { msg: string[] } };
errorData.errors.msg.forEach((er) => {
toast.error(er);
});
},
});

const handleUpdateRole = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@ export default function LinkFacilityUserSheet({
setSelectedUser(undefined);
setSelectedRole("");
},
onError: (error) => {
const errorData = error.cause as { errors: { msg: string }[] };
errorData.errors.forEach((er) => {
toast.error(er.msg);
});
},
});

const handleAddUser = () => {
Expand Down
12 changes: 0 additions & 12 deletions src/pages/Organization/components/EditUserRoleSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ export default function EditUserRoleSheet({
toast.success(t("user_role_update_success"));
setOpen(false);
},
onError: (error) => {
const errorData = error.cause as { errors: { msg: string[] } };
errorData.errors.msg.forEach((er) => {
toast.error(er);
});
},
});

const { mutate: removeRole } = useMutation({
Expand All @@ -102,12 +96,6 @@ export default function EditUserRoleSheet({
toast.success(t("user_removed_success"));
setOpen(false);
},
onError: (error) => {
const errorData = error.cause as { errors: { msg: string[] } };
errorData.errors.msg.forEach((er) => {
toast.error(er);
});
},
});

const handleUpdateRole = () => {
Expand Down
6 changes: 0 additions & 6 deletions src/pages/Organization/components/LinkUserSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ export default function LinkUserSheet({
setSelectedUser(undefined);
setSelectedRole("");
},
onError: (error) => {
const errorData = error.cause as { errors: { msg: string }[] };
errorData.errors.forEach((er) => {
toast.error(er.msg);
});
},
});

const handleAddUser = () => {
Expand Down
7 changes: 0 additions & 7 deletions src/pages/Patients/VerifyPatient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { AlertCircle, CalendarIcon } from "lucide-react";
import { Link, useQueryParams } from "raviger";
import { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";

import CareIcon from "@/CAREUI/icons/CareIcon";

Expand Down Expand Up @@ -42,12 +41,6 @@ export default function VerifyPatient(props: { facilityId: string }) {
isError,
} = useMutation({
mutationFn: mutate(routes.patient.search_retrieve),
onError: (error) => {
const errorData = error.cause as { errors: { msg: string[] } };
errorData.errors.msg.forEach((er) => {
toast.error(er);
});
},
});

const { data: encounters } = useQuery<PaginatedResponse<Encounter>>({
Expand Down
3 changes: 0 additions & 3 deletions src/pages/PublicAppointments/PatientSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ export default function PatientSelect({
replace: true,
});
},
onError: (error) => {
toast.error(error?.message || t("failed_to_create_appointment"));
},
});

const patients = patientData?.results;
Expand Down
42 changes: 26 additions & 16 deletions src/pages/PublicAppointments/Schedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ export function ScheduleAppointment(props: AppointmentsProps) {
}),
});

if (facilityError) {
toast.error(t("error_fetching_facility_data"));
}
useEffect(() => {
if (facilityError) {
toast.error(t("error_fetching_facility_data"));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [facilityError]);

const { data: userData, error: userError } = useQuery({
queryKey: ["user", facilityId, staffId],
Expand All @@ -103,9 +106,12 @@ export function ScheduleAppointment(props: AppointmentsProps) {
enabled: !!facilityId && !!staffId,
});

if (userError) {
toast.error(t("error_fetching_user_data"));
}
useEffect(() => {
if (userError) {
toast.error(t("error_fetching_user_data"));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [userError]);

const slotsQuery = useQuery<{ results: TokenSlot[] }>({
queryKey: ["slots", facilityId, staffId, selectedDate],
Expand All @@ -123,17 +129,21 @@ export function ScheduleAppointment(props: AppointmentsProps) {
enabled: !!selectedDate && !!tokenData.token,
});

if (slotsQuery.error) {
if (
slotsQuery.error.cause?.errors &&
Array.isArray(slotsQuery.error.cause.errors) &&
slotsQuery.error.cause.errors[0][0] === "Resource is not schedulable"
) {
toast.error(t("user_not_available_for_appointments"));
} else {
toast.error(t("error_fetching_slots_data"));
useEffect(() => {
if (slotsQuery.error) {
const errorData = slotsQuery.error.cause as { errors: { msg: string }[] };
if (
errorData.errors &&
Array.isArray(errorData.errors) &&
errorData.errors[0].msg === "Resource is not schedulable"
) {
toast.error(t("user_not_available_for_appointments"));
} else {
toast.error(t("error_fetching_slots_data"));
}
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [slotsQuery.error]);

const { mutate: createAppointment, isPending: isCreatingAppointment } =
useMutation({
Expand Down

0 comments on commit c4c4fb0

Please sign in to comment.