Skip to content

Commit

Permalink
Merge branch 'develop' into issue/9842/discussion-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
rajku-dev authored Feb 7, 2025
2 parents 976a053 + 226dc5a commit 9bc19a1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 84 deletions.
21 changes: 3 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
43 changes: 13 additions & 30 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 Expand Up @@ -278,19 +270,10 @@ const Login = (props: LoginProps) => {
const handlePatientLogin = async (e: React.FormEvent) => {
e.preventDefault();

try {
if (!isOtpSent) {
await sendOtp({ phone_number: phone });
setIsOtpSent(true);
} else {
await verifyOtp({ phone_number: phone, otp });
}
} catch (error: any) {
if (!isOtpSent) {
setOtpError(error.message);
} else {
setOtpValidationError(error.message);
}
if (!isOtpSent) {
sendOtp({ phone_number: phone });
} else {
verifyOtp({ phone_number: phone, otp });
}
};

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
59 changes: 26 additions & 33 deletions src/pages/PublicAppointments/PatientRegistration.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { navigate, useNavigationPrompt } from "raviger";
import { Fragment } from "react";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";
Expand All @@ -24,13 +23,12 @@ import { Textarea } from "@/components/ui/textarea";

import { usePatientContext } from "@/hooks/usePatientUser";

import { GENDER_TYPES } from "@/common/constants";
import { GENDERS, GENDER_TYPES } from "@/common/constants";
import { validateName, validatePincode } from "@/common/validation";

import { usePubSub } from "@/Utils/pubsubContext";
import routes from "@/Utils/request/api";
import mutate from "@/Utils/request/mutate";
import { HTTPError } from "@/Utils/request/types";
import { dateQueryString } from "@/Utils/utils";
import GovtOrganizationSelector from "@/pages/Organization/components/GovtOrganizationSelector";
import { AppointmentPatientRegister } from "@/pages/Patient/Utils";
Expand All @@ -42,19 +40,20 @@ import {
TokenSlot,
} from "@/types/scheduling/schedule";

const initialForm: AppointmentPatientRegister & {
ageInputType: "age" | "date_of_birth";
} = {
name: "",
gender: "1",
ageInputType: "date_of_birth",
year_of_birth: undefined,
date_of_birth: undefined,
phone_number: "",
address: "",
pincode: "",
geo_organization: undefined,
};
// const initialForm: Omit<AppointmentPatientRegister, "gender"> & {
// ageInputType: "age" | "date_of_birth";
// gender: "male" | "female" | "transgender" | "non_binary";
// } = {
// name: "",
// gender: "male",
// ageInputType: "date_of_birth",
// year_of_birth: undefined,
// date_of_birth: undefined,
// phone_number: "",
// address: "",
// pincode: "",
// geo_organization: undefined,
// };

type PatientRegistrationProps = {
facilityId: string;
Expand Down Expand Up @@ -83,7 +82,7 @@ export function PatientRegistration(props: PatientRegistrationProps) {
.string()
.min(1, t("field_required"))
.refine(validateName, t("min_char_length_error", { min_length: 3 })),
gender: z.string().min(1, t("field_required")),
gender: z.enum(GENDERS, { required_error: t("gender_is_required") }),
address: z.string().min(1, t("field_required")),
year_of_birth: z.string().optional(),
date_of_birth: z.date().or(z.string()).optional(),
Expand Down Expand Up @@ -131,7 +130,15 @@ export function PatientRegistration(props: PatientRegistrationProps) {

const form = useForm<PatientFormData>({
resolver: formResolver,
defaultValues: initialForm,
defaultValues: {
name: "",
ageInputType: "date_of_birth",
year_of_birth: undefined,
date_of_birth: undefined,
address: "",
pincode: "",
geo_organization: undefined,
},
});

const { mutate: createAppointment, isPending: isCreatingAppointment } =
Expand Down Expand Up @@ -159,9 +166,6 @@ export function PatientRegistration(props: PatientRegistrationProps) {
},
);
},
onError: (error) => {
toast.error(error?.message || t("failed_to_create_appointment"));
},
});

const { mutate: createPatient } = useMutation({
Expand All @@ -180,16 +184,6 @@ export function PatientRegistration(props: PatientRegistrationProps) {
reason_for_visit: reason ?? "",
});
},
onError: (error: HTTPError) => {
const errorData = error.cause;
const errors = errorData?.errors;
if (Array.isArray(errors) && errors.length > 0) {
const firstError = errors[0];
toast.error(firstError.msg);
} else {
toast.error(error.message);
}
},
});

const onSubmit = form.handleSubmit((data) => {
Expand Down Expand Up @@ -361,7 +355,6 @@ export function PatientRegistration(props: PatientRegistrationProps) {
<FormControl>
<Input
{...form.register("year_of_birth")}
required={form.watch("ageInputType") === "age"}
placeholder={t("type_patient_age")}
/>
</FormControl>
Expand Down Expand Up @@ -396,7 +389,7 @@ export function PatientRegistration(props: PatientRegistrationProps) {
name="pincode"
render={({ field }) => (
<FormItem className="flex flex-col">
<FormLabel>{t("pincode")}</FormLabel>
<FormLabel required>{t("pincode")}</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
Expand Down

0 comments on commit 9bc19a1

Please sign in to comment.