diff --git a/components/Forms/CommunityUnitsForms.js b/components/Forms/CommunityUnitsForms.js index b8440f09..41ad1ee8 100644 --- a/components/Forms/CommunityUnitsForms.js +++ b/components/Forms/CommunityUnitsForms.js @@ -107,21 +107,34 @@ function EditCommunityUnitsBasicDeatilsForm(props) { const payload = {} const formData = new FormData(event.target) const formDataObject = Object.fromEntries(formData) - payload['basic'] = {} + // payload['basic'] = {} + const contacts = [] + let contact = {} if (Array(touchedFields.values()).length >= 1) { for (let field of [...touchedFields.values()]) { if (props[field] !== formDataObject[field]) { if (/contact_type_\d/.test(field)) { - payload['contact_type'] = formDataObject[field] + contact = {...contact, contact_type: formDataObject[field]} + } else if (/contact_\d/.test(field)) { - payload['contact'] = formDataObject[field] + contact = {...contact, contact: formDataObject[field]} + contacts.push( + contact + ) + payload['contacts'] = contacts } else if (/chcs_.+/.test(field) || /chas_.+/.test(field) || /chps_.+/.test(field)){ payload[field] = formDataObject[field] } else { - payload['basic'][field] = formDataObject[field] + if(field == 'facility') { + payload['basic'] = {[field]: formDataObject[field]} + + } else { + payload[field] = formDataObject[field] + + } } } @@ -132,6 +145,7 @@ function EditCommunityUnitsBasicDeatilsForm(props) { payload['basic']['contact'] && delete payload['basic']['contact'] payload['basic']['contact_type'] && delete payload['basic']['contact_type'] + try { @@ -1261,30 +1275,38 @@ export function CommunityUnitEditForm(props) { {" > "} - {props?.name} ( # - {props?.code || "NO_CODE"} ) + {props?.props?.name} ( # + {props?.props?.code || "NO_CODE"} ) {/* Header snippet */}
+ + {/*
+                {
+                  JSON.stringify(props?.props?.name, null, 2)
+                }
+              
*/} + +
-

- {props?.name} -

+ + {props?.props?.name} +
- #{props?.code || "NO_CODE"} + #{" "}{props?.props?.code ?? "NO_CODE"}
@@ -1293,7 +1315,7 @@ export function CommunityUnitEditForm(props) { {/* Info snippet */}
- {props?.is_approved ? ( + {props?.props?.is_approved ? ( CHU Approved @@ -1304,25 +1326,25 @@ export function CommunityUnitEditForm(props) { Not approved )} - {props?.is_closed && ( + {props?.props?.is_closed && ( CHU Closed )} - {props?.deleted && ( + {props?.props?.deleted && ( CHU Deleted )} - {props?.active && ( + {props?.props?.active && ( CHU Active )} - {props?.has_edits && ( + {props?.props?.has_edits && ( Has changes diff --git a/components/Forms/FacilityContactsForm.js b/components/Forms/FacilityContactsForm.js index b3563b71..d58abed4 100644 --- a/components/Forms/FacilityContactsForm.js +++ b/components/Forms/FacilityContactsForm.js @@ -34,8 +34,8 @@ export function FacilityContactsForm() { const jobTitleOptions = options.job_titles; const submitType = useRef(null) - const facilityContactUuid = uuid() - const officerContactUuid = uuid() + // const facilityContactUuid = uuid() + // const officerContactUuid = uuid() @@ -47,16 +47,21 @@ export function FacilityContactsForm() { facilityContactsData['officer_reg_no'] = options?.data?.officer_in_charge?.reg_no; facilityContactsData['officer_title'] = options?.data?.officer_in_charge?.title; - + const editContactsUids = []; options?.data?.facility_contacts?.forEach((contact) => { - facilityContactsData[`contact_${facilityContactUuid}`] = contact.contact - facilityContactsData[`contact_type_${facilityContactUuid}`] = options.contact_types?.find(({ label }) => label == contact?.contact_type_name)?.value; + const uid = uuid() + editContactsUids.push(uid) + facilityContactsData[`contact_${uid}`] = contact.contact + facilityContactsData[`contact_type_${uid}`] = options.contact_types?.find(({ label }) => label == contact?.contact_type_name)?.value; }) + const editOfficerContactsUids = []; options?.data?.officer_in_charge?.contacts?.forEach((contact) => { - facilityContactsData[`officer_details_contact_${officerContactUuid}`] = contact?.contact - facilityContactsData[`officer_details_contact_type_${officerContactUuid}`] = options.contact_types?.find(({ label }) => label == contact?.contact_type_name)?.value; + const uid = uuid() + editOfficerContactsUids.push(uid) + facilityContactsData[`officer_details_contact_${uid}`] = contact?.contact + facilityContactsData[`officer_details_contact_type_${uid}`] = options.contact_types?.find(({ label }) => label == contact?.contact_type_name)?.value; }) @@ -136,21 +141,22 @@ export function FacilityContactsForm() { const currentUrl = new URL(window.document.location.href) if(options?.data?.facility_contacts.length > 0){ + // const uid = uuid() setFacilityContacts( - options?.data?.facility_contacts?.map(() => ( + options?.data?.facility_contacts?.map((_, i) => ( { - id: facilityContactUuid, + id: editContactsUids[i], contact:(() => ( ))() @@ -164,20 +170,20 @@ export function FacilityContactsForm() { if(options?.data?.officer_in_charge?.contacts.length > 0 ) { setOfficerContactDetails( - options?.data?.officer_in_charge?.contacts?.map(() => ( + options?.data?.officer_in_charge?.contacts?.map((_, i) => ( { - id: officerContactUuid, + id: editOfficerContactsUids[i], contact: (() => ( + key={editOfficerContactsUids[i]}> ))() @@ -290,6 +296,7 @@ export function FacilityContactsForm() { .then((resp) => { defer(() => updatedSavedChanges(true)); if (resp.status == 200 || resp.status == 204) { + setSubmitting(false) alert.success("Updated facility contacts successfully") if(submitType.current == null){ diff --git a/components/Forms/formComponents/FacilityContacts.js b/components/Forms/formComponents/FacilityContacts.js index 69c45c1a..c49f7939 100644 --- a/components/Forms/formComponents/FacilityContacts.js +++ b/components/Forms/formComponents/FacilityContacts.js @@ -1,9 +1,9 @@ -import { useRef, useContext, useEffect, useMemo, useState } from "react" // useContext +import { useRef,/* useContext, */ useEffect, useMemo, useState } from "react" // useContext import { XCircleIcon } from '@heroicons/react/outline' -import { FacilityContactsContext } from "../../../components/Forms/FacilityContactsForm" +// import { FacilityContactsContext } from "../../../components/Forms/FacilityContactsForm" // import { EditFacilityContactsContext } from "../../../pages/facilities/edit/[id]" -import { useAlert } from "react-alert" +// import { useAlert } from "react-alert" import Select from './FormikSelect' // import {Select as CustomSelect} from '../formComponents/Select' import { Field, useFormikContext } from 'formik' @@ -18,10 +18,8 @@ function FacilityContact( contacts }) { - const contactTypes = useContext(FacilityContactsContext); - - - const alert = useAlert() + // const contactTypes = useContext(FacilityContactsContext); + // const alert = useAlert() const contactTypeRef = useRef(null) const contactDetailsRef = useRef(null) @@ -41,8 +39,6 @@ function FacilityContact( useEffect(() => { - - if(contactTypeRef.current && contact_type_name && id){ if (contactTypeRef?.current){ @@ -69,7 +65,6 @@ function FacilityContact( id={`facility-contact-type-${index}`} placeholder="Select Contact Type" name={`${fieldNames[0]}_${index}`} - />
@@ -78,7 +73,6 @@ function FacilityContact( { (_contactType == null || _contactType == "FAX" ) && - - { delete prev[index] return prev?.filter(({id}) => id !== index) }); - - // try{ - // if(contactTypeRef?.current) { - // const resp = await fetch(`/api/common/submit_form_data/?path=delete_contact&id=${contactTypeRef?.current?.state?.value[0].id ?? null}`) - // if(resp.status == 204) alert.success('Deleted Facility Contact Successfully') - // } - // }catch(e){ - // console.error(e.message) - // } }else{ setFacilityContacts(prev => { @@ -214,9 +195,9 @@ function OfficerContactDetails ( }) { - const contactTypes = useContext(FacilityContactsContext) + // const contactTypes = useContext(FacilityContactsContext) - const alert = useAlert() + // const alert = useAlert() const contactTypeRef = useRef(null) @@ -376,14 +357,6 @@ function OfficerContactDetails ( }); - // try{ - // if(contactTypeRef?.current) { - // const resp = await fetch(`/api/common/submit_form_data/?path=delete_contact&id=${contactTypeRef?.current?.state?.value[0].id ?? null}`) - // if(resp.status == 204) alert.success('Deleted Facility Contact Successfully') - // } - // }catch(e){ - // console.error(e.message) - // } } else { setFacilityContacts(prev => { diff --git a/components/Forms/hooks/formHook.js b/components/Forms/hooks/formHook.js index 9fe23bd2..092d3f06 100644 --- a/components/Forms/hooks/formHook.js +++ b/components/Forms/hooks/formHook.js @@ -21,7 +21,7 @@ export const useLocalStorageState = ({ key, value }) => { const resetLocalStorage = useCallback(() => { // Reset locaalstorage - localStorage.clear() + // localStorage.clear() // [ // 'basic_details_form', // 'facility_contacts_form', diff --git a/controllers/facility/facilityHandlers.js b/controllers/facility/facilityHandlers.js index 0ffcb329..0869a7cf 100644 --- a/controllers/facility/facilityHandlers.js +++ b/controllers/facility/facilityHandlers.js @@ -741,9 +741,9 @@ async function handleBasicDetailsUpdates(token, formData, facility_id, updatedSa }) - if (resp.ok) { - localStorage.clear() - } + // if (resp.ok) { + // localStorage.clear() + // } @@ -836,9 +836,9 @@ async function handleFacilityContactsUpdates(token, values, facility_id, current body: JSON.stringify(payload) }) - if (resp.ok) { - localStorage.clear() - } + // if (resp.ok) { + // localStorage.clear() + // } return resp @@ -1114,9 +1114,9 @@ async function handleInfrastructureDelete(token, event, facility_infrastructure_ }) - if (resp.ok) { - localStorage.clear() - } + // if (resp.ok) { + // localStorage.clear() + // } return resp diff --git a/pages/community-units/approve/[id].js b/pages/community-units/approve/[id].js index a38b8145..d6a445e0 100644 --- a/pages/community-units/approve/[id].js +++ b/pages/community-units/approve/[id].js @@ -1,4 +1,4 @@ -import React, { useState, useEffect, useContext } from 'react'; +import React, { useState, useEffect, /*useContext*/ } from 'react'; import Head from 'next/head'; import MainLayout from '../../../components/MainLayout'; import { checkToken } from '../../../controllers/auth/auth'; @@ -11,26 +11,23 @@ import TableContainer from '@mui/material/TableContainer'; import TableHead from '@mui/material/TableHead'; import TableRow from '@mui/material/TableRow'; import { useRouter } from 'next/router' -import { UserContext } from '../../../providers/user'; import CommunityUnitSideMenu from '../../../components/CommunityUnitSideMenu'; import Spinner from '../../../components/Spinner' import { useAlert } from 'react-alert'; import Alert from '@mui/material/Alert' import {z} from 'zod' import withAuth from '../../../components/ProtectedRoute'; -// import { SettingsRemote } from '@mui/icons-material'; +import {v4 as uuid} from 'uuid' +import Link from 'next/link'; function ApproveCommunityUnit(props) { const router = useRouter(); - // const userCtx = useContext(UserContext); let cu = props.data; const alert = useAlert() - // Reference hooks for the services section - // const [user, setUser] = useState(userCtx); const [isCHULDetails, setIsCHULDetails] = useState(true); const [isClient, setIsClient] = useState(false); const [formError, setFormError] = useState(null) @@ -208,17 +205,8 @@ async function approveCHUUpdates (e, token) { } - - let reject = '' - - - - useEffect(() => { - // setUser(userCtx); - // if (user.id === 6) { - // router.push('/auth/login') - // } + setIsClient(true) }, []) @@ -259,7 +247,7 @@ async function approveCHUUpdates (e, token) { className={`col-span-5 grid grid-cols-6 gap-5 md:gap-8 py-6 w-full border ${cu.active ? 'border-gray-600' : 'border-yellow-600'} bg-transparent drop-shadow text-black p-4 md:divide-x md:divide-gray-200z items-center border-l-8 ${cu.active ? 'border-gray-600' : 'border-yellow-600'}`} >
-

{cu.name}

+ {cu.name}
#{cu.code || 'NO_CODE'}

@@ -330,9 +318,12 @@ async function approveCHUUpdates (e, token) { {/* CHU details */}

- {CHU_MainDetails.map((dt) => ( + {CHU_MainDetails.map((dt) => { + + const id = uuid() + return ( -
+
@@ -340,7 +331,7 @@ async function approveCHUUpdates (e, token) { {dt.value || " - "}

- ))} + )})} {cu.date_established && (
@@ -380,14 +371,17 @@ async function approveCHUUpdates (e, token) { {!isCHULDetails && (
- {CHULDetails.map((dt) => ( -
+ {CHULDetails.map((dt) => { + const id = uuid() + + return ( +

{dt.value || " - "}

- ))} + )})}
) } @@ -396,6 +390,11 @@ async function approveCHUUpdates (e, token) { {/* Pending updates approval */} {cu?.pending_updates && Object.keys(cu?.pending_updates).length > 0 && (
+
+                    {
+                      JSON.stringify(cu.pending_updates, null, 2)
+                    }
+                  

Pending Updates

@@ -405,25 +404,32 @@ async function approveCHUUpdates (e, token) { >
{ - Object.keys(cu.pending_updates).reverse().map((key, index) => { - if (key == 'basic') { + Object.keys(cu.pending_updates).reverse().map((key) => { + const id = uuid() + + if (key == 'basic' && JSON.stringify(cu.pending_updates?.basic) !== '{}') { return ( - + +
{'Basic :'}
- {columns.map((column, i) => ( + {columns.map((column) => { + + const id = uuid() + + return ( {column.label} - ))} + )})} @@ -540,25 +546,28 @@ async function approveCHUUpdates (e, token) { } if (key == 'services') { - const services = cu.pending_updates['services'].map((item, i) => { - return
+ const services = cu.pending_updates['services'].map((item) => { + const id = uuid() + return

{item.name}

}) - return <>
{'Services :'}

{services} + return <>
{'Services :'}

{services} } if (key == 'workers') { - const workers = cu.pending_updates['workers'].map((item, i) => { - return
+ const workers = cu.pending_updates['workers'].map((item) => { + const id = uuid() + + return

{item.first_name} {' '} {item.last_name} {'(In Charge)'}

}) - return <>
{'Workers :'}

{workers} + return <>
{'Workers :'}

{workers} }