|
| 1 | +/** @jsxImportSource @emotion/react */ |
| 2 | +import React, { useState } from 'react'; |
| 3 | + |
| 4 | +import { Button, Icon } from '@orfium/ictinus'; |
| 5 | +import { ButtonContainer, PageSubtitle, PageTitle, PageWrapper } from 'common.style'; |
| 6 | +import ConfirmationModal from 'components/ConfirmationModal'; |
| 7 | +import arrayMutators from 'final-form-arrays'; |
| 8 | +import { Form } from 'react-final-form'; |
| 9 | +import { useHistory } from 'react-router'; |
| 10 | +import { useRouteMatch } from 'react-router-dom'; |
| 11 | +import urls from 'routing/urls'; |
| 12 | + |
| 13 | +import { |
| 14 | + useGetHospital, |
| 15 | + useGetHospitals, |
| 16 | + useGetPatient, |
| 17 | + useGetSurgeons, |
| 18 | + useRegisterEpisode, |
| 19 | +} from '../../hooks/api/patientHooks'; |
| 20 | +import RegisterEpisodeForm from './components/RegisterEpisodeForm'; |
| 21 | +import { RegisterEpisodeFormType } from './types'; |
| 22 | +import { formValidation } from './utils'; |
| 23 | + |
| 24 | +const RegisterEpisode: React.FC = () => { |
| 25 | + const match = useRouteMatch<{ hospitalID?: string; patientID?: string }>(); |
| 26 | + const { hospitalID, patientID } = match.params; |
| 27 | + |
| 28 | + const { data: hospitals, isLoading: isHospitalsLoading } = useGetHospitals({ |
| 29 | + offset: 0, |
| 30 | + limit: 100, |
| 31 | + }); |
| 32 | + const { data: patient, isLoading: isPatientLoading } = useGetPatient(patientID ?? ''); |
| 33 | + const { data: hospital, isLoading: isHospitalLoading } = useGetHospital(hospitalID ?? ''); |
| 34 | + const { data: surgeons, isLoading: isSurgeonsLoading } = useGetSurgeons({ |
| 35 | + offset: 0, |
| 36 | + limit: 100, |
| 37 | + }); |
| 38 | + |
| 39 | + const hospitalPatientID = patient?.hospital_mappings.find( |
| 40 | + (value) => value.hospital_id === hospital?.id |
| 41 | + )?.patient_hospital_id; |
| 42 | + |
| 43 | + const isLoading = |
| 44 | + isHospitalLoading || isHospitalsLoading || isSurgeonsLoading || isPatientLoading; |
| 45 | + |
| 46 | + const { mutate, isLoading: isSubmitLoading } = useRegisterEpisode(hospitalID, patientID); |
| 47 | + |
| 48 | + const handleSubmit = (form: RegisterEpisodeFormType) => { |
| 49 | + mutate(form); |
| 50 | + }; |
| 51 | + |
| 52 | + const [isFormDirty, setIsFormDirty] = useState(false); |
| 53 | + const [showWarningModal, setShowWarningModal] = useState(false); |
| 54 | + |
| 55 | + const history = useHistory(); |
| 56 | + |
| 57 | + return ( |
| 58 | + <> |
| 59 | + <PageWrapper> |
| 60 | + <PageTitle> |
| 61 | + <Icon |
| 62 | + name="fatArrowLeft" |
| 63 | + size={24} |
| 64 | + color={'lightGray-700'} |
| 65 | + onClick={() => { |
| 66 | + if (isFormDirty) { |
| 67 | + setShowWarningModal(true); |
| 68 | + } else { |
| 69 | + history.push(urls.patients()); |
| 70 | + } |
| 71 | + }} |
| 72 | + /> |
| 73 | + Register an Episode |
| 74 | + </PageTitle> |
| 75 | + <PageSubtitle> |
| 76 | + Please verify that the hospital of the surgery is correct. If you wish, you can choose |
| 77 | + another hospital. |
| 78 | + </PageSubtitle> |
| 79 | + <Form |
| 80 | + initialValues={{ |
| 81 | + hospital: { value: hospital?.id, label: hospital?.name }, |
| 82 | + patientHospitalId: hospitalPatientID, |
| 83 | + surgeons: [{}, {}], |
| 84 | + }} |
| 85 | + validate={formValidation} |
| 86 | + mutators={{ |
| 87 | + ...arrayMutators, |
| 88 | + }} |
| 89 | + onSubmit={handleSubmit} |
| 90 | + > |
| 91 | + {({ handleSubmit, values, dirty, valid, submitting }) => { |
| 92 | + if (dirty) { |
| 93 | + setIsFormDirty(true); |
| 94 | + } |
| 95 | + |
| 96 | + return ( |
| 97 | + <form |
| 98 | + onSubmit={handleSubmit} |
| 99 | + css={{ |
| 100 | + display: 'flex', |
| 101 | + flexDirection: 'column', |
| 102 | + height: 'calc(100vh)', |
| 103 | + overflow: 'hidden', |
| 104 | + }} |
| 105 | + > |
| 106 | + {patient && surgeons && hospitals && hospital && ( |
| 107 | + <RegisterEpisodeForm |
| 108 | + values={values} |
| 109 | + surgeons={surgeons?.results ?? []} |
| 110 | + patient={patient} |
| 111 | + selectedHospital={hospital} |
| 112 | + hospitals={hospitals?.results ?? []} |
| 113 | + /> |
| 114 | + )} |
| 115 | + <ButtonContainer> |
| 116 | + <Button |
| 117 | + color={'blue-500'} |
| 118 | + buttonType="button" |
| 119 | + onClick={handleSubmit} |
| 120 | + disabled={isLoading || !valid || submitting || isSubmitLoading} |
| 121 | + block |
| 122 | + size="md" |
| 123 | + > |
| 124 | + Register an Episode |
| 125 | + </Button> |
| 126 | + </ButtonContainer> |
| 127 | + </form> |
| 128 | + ); |
| 129 | + }} |
| 130 | + </Form> |
| 131 | + </PageWrapper> |
| 132 | + {showWarningModal && ( |
| 133 | + <ConfirmationModal |
| 134 | + onClose={() => { |
| 135 | + setShowWarningModal(false); |
| 136 | + }} |
| 137 | + title={'Cancel new registration?'} |
| 138 | + subtitle={ |
| 139 | + 'Are you sure you want to cancel registering an episode? All information you’ve entered will be lost!' |
| 140 | + } |
| 141 | + buttonText={'Yes, cancel new addition'} |
| 142 | + onClick={() => history.push(`${urls.patients()}/${hospitalID}/${patientID}`)} |
| 143 | + /> |
| 144 | + )} |
| 145 | + </> |
| 146 | + ); |
| 147 | +}; |
| 148 | + |
| 149 | +export default RegisterEpisode; |
0 commit comments