Skip to content

Commit

Permalink
Merge pull request #438 from ertush/hotfix
Browse files Browse the repository at this point in the history
Fixed Issue of updating facility contacts and facility officer in cha…
  • Loading branch information
ertush authored Aug 2, 2024
2 parents 91fa415 + ed90b05 commit 906b147
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 112 deletions.
58 changes: 40 additions & 18 deletions components/Forms/CommunityUnitsForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]

}

}
}
Expand All @@ -132,6 +145,7 @@ function EditCommunityUnitsBasicDeatilsForm(props) {
payload['basic']['contact'] && delete payload['basic']['contact']
payload['basic']['contact_type'] && delete payload['basic']['contact_type']




try {
Expand Down Expand Up @@ -1261,30 +1275,38 @@ export function CommunityUnitEditForm(props) {
</Link>
{" > "}
<span className="text-gray-500">
{props?.name} ( #
<i className="text-black">{props?.code || "NO_CODE"}</i> )
{props?.props?.name} ( #
<i className="text-black">{props?.props?.code || "NO_CODE"}</i> )
</span>
</div>

{/* Header snippet */}
<div
className={
`md:col-span-7 grid grid-cols-6 gap-5 md:gap-8 py-6 w-full border ${props?.active ? "border-gray-400 rounded" : "border-red-600"} bg-transparent drop-shadow text-black p-4 md:divide-x md:divide-gray-200z items-center border-l-8 " +
${props?.active ? "border-gray-400 rounded" : "border-yellow-700"}
`md:col-span-7 grid grid-cols-6 gap-5 md:gap-8 py-6 w-full border ${props?.props?.active ? "border-gray-400 rounded" : "border-red-600"} bg-transparent drop-shadow text-black p-4 md:divide-x md:divide-gray-200z items-center border-l-8 " +
${props?.props?.active ? "border-gray-400 rounded" : "border-yellow-700"}
`}
>

{/* <pre>
{
JSON.stringify(props?.props?.name, null, 2)
}
</pre> */}


<div className="col-span-6 md:col-span-3">
<h1 className="text-4xl tracking-tight font-bold leading-tight">
{props?.name}
</h1>
<Link href={`/community-units/${props?.props?.id}`} className="text-4xl tracking-tight font-bold leading-tight">
{props?.props?.name}
</Link>
<div className="flex gap-2 items-center w-full justify-between">
<span
className={
"font-bold text-2xl " +
(props?.code ? "text-gray-900" : "text-gray-500")
(props?.props?.code ? "text-gray-900" : "text-gray-500")
}
>
#{props?.code || "NO_CODE"}
#{" "}{props?.props?.code ?? "NO_CODE"}
</span>

</div>
Expand All @@ -1293,7 +1315,7 @@ export function CommunityUnitEditForm(props) {
{/* Info snippet */}
<div className="flex flex-wrap gap-3 items-center justify-end col-span-6 md:col-span-2">
<div className="flex flex-wrap gap-3 w-full items-center justify-start md:justify-center">
{props?.is_approved ? (
{props?.props?.is_approved ? (
<span className="bg-blue-200 text-gray-900 p-1 leading-none text-sm whitespace-nowrap cursor-default flex items-center gap-x-1">
<CheckCircleIcon className="h-4 w-4" />
CHU Approved
Expand All @@ -1304,25 +1326,25 @@ export function CommunityUnitEditForm(props) {
Not approved
</span>
)}
{props?.is_closed && (
{props?.props?.is_closed && (
<span className="bg-gray-200 text-gray-900 p-1 leading-none text-sm whitespace-nowrap cursor-default flex items-center gap-x-1">
<LockClosedIcon className="h-4 w-4" />
CHU Closed
</span>
)}
{props?.deleted && (
{props?.props?.deleted && (
<span className="bg-gray-200 text-gray-900 p-1 leading-none text-sm whitespace-nowrap cursor-default flex items-center gap-x-1">
<XCircleIcon className="h-4 w-4" />
CHU Deleted
</span>
)}
{props?.active && (
{props?.props?.active && (
<span className="bg-blue-200 text-gray-900 p-1 leading-none text-sm whitespace-nowrap cursor-default flex items-center gap-x-1">
<CheckCircleIcon className="h-4 w-4" />
CHU Active
</span>
)}
{props?.has_edits && (
{props?.props?.has_edits && (
<span className="bg-blue-200 text-gray-900 p-1 leading-none text-sm whitespace-nowrap cursor-default flex items-center gap-x-1">
<InformationCircleIcon className="h-4 w-4" />
Has changes
Expand Down
37 changes: 22 additions & 15 deletions components/Forms/FacilityContactsForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()



Expand All @@ -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;
})


Expand Down Expand Up @@ -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:(() => (
<FacilityContactsContext.Provider
value={facilityContacts}
key={facilityContactUuid}
key={editContactsUids[i]}
>
<FacilityContact
contactTypeOptions={contactTypeOptions}
fieldNames={['contact_type', 'contact']}
setFacilityContacts={setFacilityContacts}
contacts={[null, null, null]}
index={facilityContactUuid}
index={editContactsUids[i]}
/>
</FacilityContactsContext.Provider>
))()
Expand All @@ -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: (() => (
<FacilityContactsContext.Provider
value={officerContactDetails}
key={officerContactUuid}>
key={editOfficerContactsUids[i]}>

<OfficerContactDetails
contactTypeOptions={contactTypeOptions}
fieldNames={['officer_details_contact_type', 'officer_details_contact']}
contacts={[null, null, null]}
setFacilityContacts={setOfficerContactDetails}
index={officerContactUuid}
index={editOfficerContactsUids[i]}
/>
</ FacilityContactsContext.Provider >
))()
Expand Down Expand Up @@ -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){
Expand Down
41 changes: 7 additions & 34 deletions components/Forms/formComponents/FacilityContacts.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)
Expand All @@ -41,8 +39,6 @@ function FacilityContact(


useEffect(() => {


if(contactTypeRef.current && contact_type_name && id){

if (contactTypeRef?.current){
Expand All @@ -69,7 +65,6 @@ function FacilityContact(
id={`facility-contact-type-${index}`}
placeholder="Select Contact Type"
name={`${fieldNames[0]}_${index}`}

/>

<div className="w-full col-start-2 flex items-center gap-x-3 justify-between">
Expand All @@ -78,7 +73,6 @@ function FacilityContact(

{
(_contactType == null || _contactType == "FAX" ) &&

<Field
as="input"
required
Expand All @@ -92,7 +86,6 @@ function FacilityContact(

{
( _contactType == 'LANDLINE' || _contactType == 'MOBILE') &&

<div className=" flex flex-col w-full gap-1 relative max-h-min">
<Field
as="input"
Expand All @@ -115,7 +108,6 @@ function FacilityContact(
{
_contactType == 'EMAIL' &&
<div className=" flex flex-col w-full gap-1 relative max-h-min">

<Field
as="input"
reuired
Expand Down Expand Up @@ -165,23 +157,12 @@ function FacilityContact(
ev.preventDefault();

if(!contacts.includes(undefined)){


setFacilityContacts(prev => {

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 => {
Expand Down Expand Up @@ -214,9 +195,9 @@ function OfficerContactDetails (
}) {


const contactTypes = useContext(FacilityContactsContext)
// const contactTypes = useContext(FacilityContactsContext)

const alert = useAlert()
// const alert = useAlert()


const contactTypeRef = useRef(null)
Expand Down Expand Up @@ -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 => {
Expand Down
2 changes: 1 addition & 1 deletion components/Forms/hooks/formHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const useLocalStorageState = ({ key, value }) => {
const resetLocalStorage = useCallback(() => {

// Reset locaalstorage
localStorage.clear()
// localStorage.clear()
// [
// 'basic_details_form',
// 'facility_contacts_form',
Expand Down
18 changes: 9 additions & 9 deletions controllers/facility/facilityHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,9 +741,9 @@ async function handleBasicDetailsUpdates(token, formData, facility_id, updatedSa
})


if (resp.ok) {
localStorage.clear()
}
// if (resp.ok) {
// localStorage.clear()
// }



Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -1114,9 +1114,9 @@ async function handleInfrastructureDelete(token, event, facility_infrastructure_

})

if (resp.ok) {
localStorage.clear()
}
// if (resp.ok) {
// localStorage.clear()
// }

return resp

Expand Down
Loading

0 comments on commit 906b147

Please sign in to comment.