|
| 1 | +import { AxiosError } from 'axios'; |
| 2 | +import { ReactQueryKeys } from 'hooks/constants'; |
| 3 | +import { useMutation, useQuery } from 'react-query'; |
| 4 | +import { useHistory } from 'react-router-dom'; |
| 5 | + |
| 6 | +import patientsAPI from '../../api/patientsAPI'; |
| 7 | +import { HospitalsResponse, RegisterPatientPayload } from '../../models/apiTypes'; |
| 8 | +import { RegisterPatientFormType } from '../../pages/RegisterPatient/types'; |
| 9 | +import urls from '../../routing/urls'; |
| 10 | + |
| 11 | +export const useGetHospitals = () => { |
| 12 | + return useQuery<HospitalsResponse, AxiosError, HospitalsResponse>( |
| 13 | + ReactQueryKeys.HospitalsQuery, |
| 14 | + async () => { |
| 15 | + const { request } = patientsAPI.single.getHospitals(); |
| 16 | + const data = await request(); |
| 17 | + |
| 18 | + return data; |
| 19 | + }, |
| 20 | + { |
| 21 | + onError: (errors) => { |
| 22 | + console.log(errors); |
| 23 | + }, |
| 24 | + |
| 25 | + retry: false, |
| 26 | + } |
| 27 | + ); |
| 28 | +}; |
| 29 | + |
| 30 | +export const useRegisterPatient = () => { |
| 31 | + const history = useHistory(); |
| 32 | + |
| 33 | + return useMutation<RegisterPatientPayload, AxiosError, RegisterPatientFormType>( |
| 34 | + (params) => { |
| 35 | + const { request } = patientsAPI.single.registerPatient({ |
| 36 | + full_name: params.name, |
| 37 | + address: params.address, |
| 38 | + age: params.age, |
| 39 | + year_of_birth: params.yearOfBirth, |
| 40 | + phone_1: params.phone1, |
| 41 | + phone_2: params.phone2, |
| 42 | + hospital_id: params.center.value, |
| 43 | + national_id: params.nationalId, |
| 44 | + gender: params.gender, |
| 45 | + }); |
| 46 | + return request(); |
| 47 | + }, |
| 48 | + { |
| 49 | + onSuccess: () => { |
| 50 | + history.replace(urls.patients()); |
| 51 | + }, |
| 52 | + onError: (errors) => { |
| 53 | + console.log(errors); |
| 54 | + }, |
| 55 | + } |
| 56 | + ); |
| 57 | +}; |
0 commit comments