Skip to content

Commit

Permalink
Merge pull request #2 from abhay-raizada/fixes
Browse files Browse the repository at this point in the history
Fix State Initialization and Bugs
  • Loading branch information
abh3po authored Jan 6, 2025
2 parents 58f1458 + 85ef631 commit 066df1e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
7 changes: 5 additions & 2 deletions components/PrescriptionCreator/AddressForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ interface AddressForm {

interface AddressFormProps {
nestedFormCallback: (tag: string, form: Object) => void;
initForm: {[key: string]: any};
}
const ADDRESS_KEY = 'Address';

export const AddressForm: React.FC<AddressFormProps> = ({
nestedFormCallback,
initForm,
}) => {
const [form, setForm] = useState<AddressForm>({});
const [form, setForm] = useState<AddressForm>(initForm[ADDRESS_KEY] || {});

const handleTextChange = (tag: keyof AddressForm, text: string) => {
let newForm = {...form};
newForm[tag] = text;
setForm(newForm);
nestedFormCallback('Address', newForm);
nestedFormCallback(ADDRESS_KEY, newForm);
};

return (
Expand Down
10 changes: 7 additions & 3 deletions components/PrescriptionCreator/MedicineForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@ interface MedicineForm {
directions?: string;
}

const MEDICINE_KEY = 'MedicationPrescribed';

interface MedicineFormProps {
nestedFormCallback: (tag: string, form: Object) => void;
initForm: {[key: string]: any};
}

export const MedicineForm: React.FC<MedicineFormProps> = ({
nestedFormCallback,
initForm,
}) => {
const [form, setForm] = useState<MedicineForm>({});
const [form, setForm] = useState<MedicineForm>(initForm[MEDICINE_KEY] || {});

const handleTextChange = (tag: keyof MedicineForm, text: string) => {
let newForm = {...form};
newForm[tag] = text;
setForm(newForm);
nestedFormCallback('MedicationPrescribed', newForm);
nestedFormCallback(MEDICINE_KEY, newForm);
};

return (
Expand Down Expand Up @@ -85,7 +89,7 @@ export const MedicineForm: React.FC<MedicineFormProps> = ({
<TextInput
style={styles.input}
placeholder="Enter directions"
value={form.refills}
value={form.directions}
placeholderTextColor="white"
onChangeText={(text: string) => handleTextChange('directions', text)}
/>
Expand Down
12 changes: 9 additions & 3 deletions components/PrescriptionCreator/PatientForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,25 @@ interface PatientForm {

interface PatientFormProps {
nestedFormCallback: (tag: string, form: Object) => void;
initForm: {[key: string]: any};
}

const PATIENT_KEY = 'patient';

export const PatientForm: React.FC<PatientFormProps> = ({
nestedFormCallback,
initForm,
}) => {
const [form, setForm] = useState<PatientForm>({});
const [form, setForm] = useState<PatientForm>(
initForm[PATIENT_KEY]?.['human_patient'] || {},
);
const [openDate, setOpenDate] = useState<boolean>(false);

const handleTextChange = (tag: 'name' | 'date_of_birth', text: string) => {
let newForm = {...form};
newForm[tag] = text;
setForm(newForm);
nestedFormCallback('patient', {human_patient: newForm});
nestedFormCallback(PATIENT_KEY, {human_patient: newForm});
};

return (
Expand Down Expand Up @@ -62,7 +68,7 @@ export const PatientForm: React.FC<PatientFormProps> = ({
modal
mode={'date'}
open={openDate}
date={new Date(form.date_of_birth || '01-01-1999')}
date={new Date(form.date_of_birth || '1990-01-01')}
onCancel={() => setOpenDate(false)}
onConfirm={(date: Date) => {
handleTextChange('date_of_birth', date.toDateString());
Expand Down
17 changes: 13 additions & 4 deletions components/PrescriptionCreator/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,22 @@ export const PrescriptionCreator = () => {
<View style={{display: 'flex', flexDirection: 'column', width: '100%'}}>
<Section title="Patient" collapsible={true}>
{' '}
<PatientForm nestedFormCallback={nestedFormCallback} />
<PatientForm
initForm={finalJSON}
nestedFormCallback={nestedFormCallback}
/>
</Section>
<Section title="Address" collapsible={true}>
<AddressForm nestedFormCallback={nestedFormCallback} />
<AddressForm
initForm={finalJSON}
nestedFormCallback={nestedFormCallback}
/>
</Section>
<Section title="Medicine" collapsible={true}>
<MedicineForm nestedFormCallback={nestedFormCallback} />
<Section title="Medication" collapsible={true}>
<MedicineForm
initForm={finalJSON}
nestedFormCallback={nestedFormCallback}
/>
</Section>
<View style={{margin: 15}}>
<Button onPress={handleButtonPress} title="Create Rx" />
Expand Down

0 comments on commit 066df1e

Please sign in to comment.