Skip to content

Commit

Permalink
Form submission when changing sections working
Browse files Browse the repository at this point in the history
  • Loading branch information
samau3 committed Aug 28, 2024
1 parent 2d8face commit 81664c9
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions client/src/pages/patients/patients.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default function Patients() {
patientData: {
firstName: isNotEmpty('First Name is required'),
lastName: isNotEmpty('Last Name is required'),
language: isNotEmpty('Language is required'),
gender: isNotEmpty('Gender is required'),
dateOfBirth: matches(
/^\d{4}-\d{2}-\d{2}$/,
Expand Down Expand Up @@ -139,12 +140,13 @@ export default function Patients() {
}
} else if (res.status === StatusCodes.CONFLICT) {
patientData.id = patientId;
patientData.codeStatus = codeStatus;

const res = await updatePatient({
patientData,
contactData,
medicalData,
healthcareChoices,
codeStatus,
});
if (res.status === StatusCodes.OK) {
notifications.show({
Expand Down Expand Up @@ -178,13 +180,13 @@ export default function Patients() {
async function handleAccordionChange(value) {
console.log(value, openedSection);
// console.log(form.getValues()[openedSection]);
let errors = 0;
let errorFieldCount = 0;
for (const field in form.getValues()[openedSection]) {
form.validateField(`${openedSection}.${field}`);
form.isValid(`${openedSection}.${field}`) ? null : errors++;
form.isValid(`${openedSection}.${field}`) ? null : errorFieldCount++;
}
console.log(errors);
if (errors === 0) {
console.log(errorFieldCount);
if (errorFieldCount === 0) {
setOpenedSection(value);

if (openedSection === 'patientData') {
Expand All @@ -198,7 +200,9 @@ export default function Patients() {
color: 'green',
});
} else if (res.status === StatusCodes.CONFLICT) {
const res = await updatePatient(form.getValues().patientData);
const res = await updatePatient({
patientData: form.getValues().patientData,
});
if (res.status === StatusCodes.OK) {
notifications.show({
title: 'Success',
Expand All @@ -220,7 +224,10 @@ export default function Patients() {
}
} else {
try {
const res = await updatePatient(form.getValues()[openedSection]);
const res = await updatePatient({
[openedSection]: form.getValues()[openedSection],
});
console.log(await res.json());
if (res.status === StatusCodes.OK) {
notifications.show({
title: 'Success',
Expand Down Expand Up @@ -260,7 +267,7 @@ export default function Patients() {
const errorKeys = Object.keys(errors).map((key) => key.split('.')[0]);
const errorSets = new Set(errorKeys);

const errorSectionMapping = {
const errorSectionMap = {
patientData: 'Patient Data',
contactData: 'Emergency Contact',
medicalData: 'Medical Information',
Expand All @@ -270,7 +277,7 @@ export default function Patients() {

let errorSections = [];
errorSets.forEach((key) => {
errorSections.push(errorSectionMapping[key]);
errorSections.push(errorSectionMap[key]);
});

notifications.show({
Expand Down Expand Up @@ -326,7 +333,6 @@ export default function Patients() {
]}
key={form.key('patientData.gender')}
{...form.getInputProps('patientData.gender')}
clearable
/>
<Select
label="Language"
Expand All @@ -342,7 +348,6 @@ export default function Patients() {
]}
key={form.key('patientData.language')}
{...form.getInputProps('patientData.language')}
clearable
/>
<TextInput
label="Date of Birth"
Expand Down Expand Up @@ -379,7 +384,7 @@ export default function Patients() {
/>
<TextInput
label="Phone Number"
placeholder="Phone Number"
placeholder="XXX-XXX-XXXX"
withAsterisk
key={form.key('contactData.phone')}
{...form.getInputProps('contactData.phone')}
Expand All @@ -404,7 +409,6 @@ export default function Patients() {
]}
key={form.key('contactData.relationship')}
{...form.getInputProps('contactData.relationship')}
clearable
/>
</Accordion.Panel>
</Accordion.Item>
Expand Down Expand Up @@ -454,7 +458,6 @@ export default function Patients() {
data={['COMFORT', 'DNR', 'DNI', 'DNR_DNI', 'FULL']}
key={form.key('codeStatus')}
{...form.getInputProps('codeStatus')}
clearable
/>
</Accordion.Panel>
</Accordion.Item>
Expand Down

0 comments on commit 81664c9

Please sign in to comment.