import React, { useEffect, useState } from 'react'; import { getPatient, savePatient, getPatientIdentifierSources, exportPatientIdentifiersFromSource } from "./PatientService"; import { age, navigate } from '@openmrs/esm-framework'; import * as Yup from "yup"; import { Formik, Form } from "formik"; import { Button, ButtonSkeleton, Column, Grid, TextInput, TextInputSkeleton } from 'carbon-components-react'; import { Patient, PatientCreateViewModel } from '../models/patient'; import GlobitsTextInput from '../common/form/GlobitsTextInput'; import GlobitsSelectInput from '../common/form/GlobitsSelectInput'; import GlobitsDateInput from '../common/form/GlobitsDateInput'; import styles from './patient.scss'; interface PatientEditProps { } export default function PatientCreate() { const [isLoading, setIsLoading] = useState(false); const [patientIdenResourceId, setPatientIdenResourceId] = useState(""); const [identifiers, setIdentifiers] = useState([]); const backToPatientList = () => navigate({ to: `/openmrs/spa/dashboard/patient` }); const [patient, setPatient] = useState({ // 1. Basic Info familyName: "", givenName: "", middleName: "", openMRSId: "", openMRSIdentifierNumber: "", oldIdentifierNumber: "", gender: "M", birthdate: null, age: null, //2. Contact Details postCode: "", longitude: "", estateNearestCentre: "", sectionHomestead: "", addressLine1: "", addressLine2: "", country: "", region: "", latitude: "", location: "", sublocation: "", city: "", district: "", division: "", phoneNumber: "", email: "", }); const validationSchema = Yup.object({ familyName: Yup.string().required("This field is required!"), givenName: Yup.string().required("This field is required!"), }); useEffect(() => { getResource(); }, []); useEffect(() => { // console.log(patientIdenResourceId) if (patientIdenResourceId) generateIdentifiers(patientIdenResourceId) }, [patientIdenResourceId]); async function generateIdentifiers(id: string) { let newObj = { generateIdentifiers: true, sourceUuid: id, numberToGenerate: 1 } console.log(newObj) let response = await exportPatientIdentifiersFromSource(newObj); setIdentifiers(response.data.identifiers) } async function getResource() { let response = await getPatientIdentifierSources(); setPatientIdenResourceId(response.data.results[0].uuid); } function hanledFormSubmit(patient: Patient) { setIsLoading(true) let newPatient: PatientCreateViewModel = { identifiers: [ { "identifier": identifiers[0], "identifierType": "05a29f94-c0ed-11e2-94be-8c13b969e334", "location": "c0937d4f-1691-11df-97a5-7038c432aabf", "preferred": true } ], person: { gender: patient.gender, age: patient.age, birthdate: patient.birthdate, birthdateEstimated: false, dead: false, names: [ { givenName: patient.givenName, familyName: patient.familyName, middleName: patient.middleName } ], addresses: [ { address1: patient.addressLine1, cityVillage: patient.city, country: patient.country, postalCode: patient.postCode, latitude: patient.latitude, longitude: patient.longitude, } ] } } // console.log(newPatient) savePatient(newPatient).then((data) => { if (data) { alert("Create Patient Success!") } else { alert("Something wrong!") } }); setIsLoading(false); } // async function createOpenMrsPatient(patient: Patient) { // setIsLoading(false); // } return ( hanledFormSubmit(values)} > {({ isSubmitting }) => (

1. Basic Info

All fields are required unless marked optional {/* */} {/* */}

2. Contact Details

All fields are required unless marked optional
{isLoading ? ( ) : ( )}
) }
); };