Skip to content

Commit

Permalink
Fixes login errors being shown twice due to nested useMutation (#10479
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rithviknishad authored Feb 7, 2025
1 parent 267622a commit 1bd8305
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/Utils/FiltersCache.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const invalidate = (key?: string) => {
/**
* Removes all filters cache in the platform.
*/
const invaldiateAll = () => {
const invalidateAll = () => {
for (const key in localStorage) {
if (key.startsWith("filters--")) {
invalidate(key);
Expand All @@ -70,7 +70,7 @@ export default {
get,
set,
invalidate,
invaldiateAll,
invalidateAll,
utils: {
clean,
},
Expand Down
26 changes: 9 additions & 17 deletions src/components/Auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@ import FiltersCache from "@/Utils/FiltersCache";
import ViewCache from "@/Utils/ViewCache";
import routes from "@/Utils/request/api";
import mutate from "@/Utils/request/mutate";
import { HTTPError } from "@/Utils/request/types";
import { TokenData } from "@/types/auth/otpToken";

interface LoginFormData {
username: string;
password: string;
}

interface OtpLoginData {
phone_number: string;
otp: string;
Expand Down Expand Up @@ -103,17 +99,6 @@ const Login = (props: LoginProps) => {
localStorage.setItem(LocalStorageKeys.loginPreference, loginMode);
}, [loginMode]);

// Staff Login Mutation
const staffLoginMutation = useMutation({
mutationFn: async (data: LoginFormData) => {
FiltersCache.invaldiateAll();
return await signIn(data);
},
onError: (error) => {
setCaptcha(error.status == 429);
},
});

// Send OTP Mutation
const { mutate: sendOtp, isPending: sendOtpPending } = useMutation({
mutationFn: mutate(routes.otp.sendOtp),
Expand Down Expand Up @@ -233,7 +218,14 @@ const Login = (props: LoginProps) => {
const validated = validateData();
if (!validated) return;

staffLoginMutation.mutate(validated);
FiltersCache.invalidateAll();
try {
await signIn(validated);
} catch (error) {
if (error instanceof HTTPError) {
setCaptcha(error.status == 429);
}
}
};

const validateForgetData = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Appointments/components/AppointmentTokenCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const AppointmentTokenCard = ({ id, appointment, facility }: Props) => {
<Label>{t("name")}</Label>
<p className="font-semibold">{patient.name}</p>
<p className="text-sm text-gray-600 font-medium">
{formatPatientAge(patient as any, true)},{" "}
{formatPatientAge(patient, true)},{" "}
{t(`GENDER__${patient.gender}`)}
</p>
</div>
Expand Down

0 comments on commit 1bd8305

Please sign in to comment.