Skip to content

Commit

Permalink
Merge pull request #478 from ertush/hotfix
Browse files Browse the repository at this point in the history
Hotfix
  • Loading branch information
ertush authored Sep 10, 2024
2 parents d4f8b92 + 71bb79d commit 3403dc0
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 11,238 deletions.
8 changes: 8 additions & 0 deletions components/FacilitySideMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ function FacilitySideMenu(/*{ states, stateSetters, filters }*/) {
filter:'pending_validation_facilities',
pending_approval: true,
has_edits: false,
closed: false,
...userOrgUnit()

}
Expand Down Expand Up @@ -176,6 +177,7 @@ function FacilitySideMenu(/*{ states, stateSetters, filters }*/) {
query: {
filter:'updated_pending_validation_facilities',
have_updates: true,
closed: false,
...userOrgUnit()

}
Expand Down Expand Up @@ -210,6 +212,7 @@ function FacilitySideMenu(/*{ states, stateSetters, filters }*/) {
query: {
filter:'pending_approval_facilities',
to_publish: true,
closed: false,
...userOrgUnit()

}
Expand Down Expand Up @@ -247,6 +250,7 @@ function FacilitySideMenu(/*{ states, stateSetters, filters }*/) {
approved_national_level: true,
rejected: false,
reporting_in_dhis: true,
closed: false,
...userOrgUnit()

}
Expand Down Expand Up @@ -281,6 +285,7 @@ function FacilitySideMenu(/*{ states, stateSetters, filters }*/) {
query: {
filter:'failed_validation_facilities',
rejected: true,
closed: false,
...userOrgUnit()

}
Expand Down Expand Up @@ -315,6 +320,7 @@ function FacilitySideMenu(/*{ states, stateSetters, filters }*/) {
query: {
filter:'rejected_facilities',
rejected_national: true,
closed: false,
...userOrgUnit()

}
Expand Down Expand Up @@ -383,6 +389,7 @@ function FacilitySideMenu(/*{ states, stateSetters, filters }*/) {
query: {
filter:'incomplete_facilities',
incomplete: true,
closed: false,
...userOrgUnit()

}
Expand Down Expand Up @@ -416,6 +423,7 @@ function FacilitySideMenu(/*{ states, stateSetters, filters }*/) {
query: {
filter:'synchronized_regulated_facilities',
mfl_code_null: true,
close: false,
...userOrgUnit()

}
Expand Down
196 changes: 109 additions & 87 deletions components/Forms/BasicDetailsForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

"use client"

import { useContext, useEffect, useState, useMemo, useRef } from 'react';
import { useContext, useEffect, useState, useCallback, useMemo, useRef } from 'react';
import { Select as CustomSelect } from './formComponents/Select';
import { FormOptionsContext } from '../../pages/facilities/add';
import {
Expand All @@ -15,10 +15,6 @@ import { useRouter } from 'next/router';
import { Alert } from '@mui/lab';
import { UpdateFormIdContext } from './Form';

// import { indexOf } from 'underscore';

// import { FacilityIdContext, FacilityWardDataContext } from './Form';


export function BasicDeatilsForm({ editMode }) {

Expand Down Expand Up @@ -63,6 +59,7 @@ export function BasicDeatilsForm({ editMode }) {
const formContext = useContext(FormOptionsContext);
const [options, setOptions] = useState(formContext)
const [facilityTypeDetailOptions, setFacilityTypeDetailOptions] = useState(options?.facility_type_details)
const [countyId, setCountyId] = useState(options?.data?.county_id)
const [ownerTypeDetailsOptions, setOwnerTypeDetailsOptions] = useState(Array.from(options?.owners ?? [], o => {
if (o?.owner_type_name || o?.created_by || o?.updated_by) {
return {
Expand Down Expand Up @@ -91,14 +88,87 @@ export function BasicDeatilsForm({ editMode }) {
},
];

async function setFilteredOptions(countyId, selectName) {
if(countyId === options?.data?.county_id) {
try {
const sub_counties = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/common/sub_counties/?county=${countyId}`, {
headers: {
'Accept': 'application/json',
'Authorization': `Bearer ${options?.token}`
}
})

const constituencies = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/common/constituencies/?county=${countyId}`, {
headers: {
'Accept': 'application/json',
'Authorization': `Bearer ${options?.token}`
}
})


const wards = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/common/wards/?county=${countyId}`, {
headers: {
'Accept': 'application/json',
'Authorization': `Bearer ${options?.token}`
}
})

const _sub_counties = (await sub_counties.json())?.results

if (!_sub_counties) throw Error('Unable to Fetch sub counties')



const _constitutencies = (await constituencies.json())?.results

if (!constituencies) throw Error('Unable to Fetch sub counties')


const _wards = (await wards.json())?.results

if (!_wards) throw Error('Unable to Fetch sub counties')



if(selectName === "sub_county_id") setSubCountyOptions(() => _sub_counties?.map(({id, name}) => ({value:id, label:name})))

if(selectName === "constituency_id") setConstituencyOptions(() => _constitutencies?.map(({id, name}) => ({value:id, label:name})))

if(selectName === "ward") setWardOptions(() => _wards?.map(({id, name}) => ({value:id, label:name})))


} catch(e) {
if(e instanceof TypeError){
console.error(`Error: ${e.message}`)
} else if(typeof e === 'string') {
console.error(e)
} else {
console.error(e)
}
}
}

}

// Event handlers
function handleFocus(e) {
const handleFocus = useCallback((e) => {

if(editMode){
setFilteredOptions(countyId, e.currentTarget.name)
}

if(!editMode) {
if(e.currentTarget.name === 'sub_county_id' && !countyId) setSubCountyOptions([])
if(e.currentTarget.name === 'constituency_id' && !countyId) setConstituencyOptions([])
if(e.currentTarget.name === 'ward' && !countyId) setWardOptions([])
}

setTouchedFields(touchedFields => {
return [...touchedFields, e.target.name]
})
}
})

function handleDateChange(e) {
const handleDateChange = useCallback((e) => {

e.preventDefault()

Expand All @@ -117,14 +187,24 @@ export function BasicDeatilsForm({ editMode }) {
e.currentTarget.value = ''

}
}
})

async function handleSelectChange(e) {



const handleSelectChange = useCallback(async (e) => {

const keph = document.getElementsByName('keph_level');
const kephDisplay = document.getElementsByName('keph_level_display');


if(e.currentTarget.name === 'county_id') {
setCountyId(e.currentTarget.value)
// Filter sub county, constituency and ward Options in edit mode
}



// Handle facility Type Change
if (e.target.name == 'facility_type_parent') {

Expand Down Expand Up @@ -347,10 +427,10 @@ export function BasicDeatilsForm({ editMode }) {
}
}
}
}
})


function handleBasicDetailsUpdate(e) {
const handleBasicDetailsUpdate = useCallback((e) => {

e.preventDefault()

Expand Down Expand Up @@ -418,10 +498,10 @@ export function BasicDeatilsForm({ editMode }) {



}
})


function handeBasicDetailsCreate(e) {
const handeBasicDetailsCreate = useCallback((e) => {

e.preventDefault()

Expand Down Expand Up @@ -557,9 +637,9 @@ export function BasicDeatilsForm({ editMode }) {



}
})

function handleNumberInputChange(e) {
const handleNumberInputChange = useCallback((e) => {

// Total Funcational Input Beds validation

Expand All @@ -577,13 +657,12 @@ export function BasicDeatilsForm({ editMode }) {

setTotalFunctionalBeds(totalBeds)

}
})


// Effects
useEffect(() => {


// console.log({facility: options?.data})
async function updateFacilityTypeDetailOptions(e) {
try {
Expand Down Expand Up @@ -646,6 +725,12 @@ export function BasicDeatilsForm({ editMode }) {
.catch(console.error)

}
/**
*
* @param {string} countyId
*/



if (window && !editMode) {
const path = new URL(window.document.location.href)
Expand Down Expand Up @@ -711,6 +796,8 @@ export function BasicDeatilsForm({ editMode }) {
else if (editMode) {

let parent = "";



getFacilityTypeDetailsParent(options?.data?.facility_type, options?.token)
.then(facilityTypeDetails => {
Expand Down Expand Up @@ -741,72 +828,7 @@ export function BasicDeatilsForm({ editMode }) {
)


// async function fetchDetailOptions() {
// if (facilityTypeValue) {
// try {
// const facilityTypeDetails = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/facilities/facility_types_details/?is_parent=false&parent=${facilityTypeValue}`, {
// headers: {
// 'Accept': 'application/json',
// 'Authorization': `Bearer ${options?.token}`
// }
// })

// const filteredFacilityType = (await facilityTypeDetails.json())?.results



// const facilityType = Array.from(options?.facility_types, ({ id, name }) => {
// return {
// label: name,
// value: id
// }
// })


// setFacilityTypeDetailOptions(facilityType ?? options?.facility_type_details)

// }
// catch (e) {
// console.error(e.message)
// }
// }

// if(options?.data?.owner_type){

// try {
// const owners = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/facilities/owners/?owner_type=${options?.data?.owner_type}`, {
// headers: {
// 'Accept': 'application/json',
// 'Authorization': `Bearer ${options?.token}`
// }
// })

// const filteredOwners = (await owners.json())?.results

// if (!filteredOwners) throw Error('Unable to Fetch Owner Type Details')


// const facilityOwnerOptions = Array.from(filteredOwners, ({ id, name }) => {
// return {
// label: name,
// value: id
// }
// })


// setOwnerTypeDetailsOptions(facilityOwnerOptions ?? options?.owner_types)

// }
// catch (e) {
// console.error(e.message)
// }


// }
// }


// fetchDetailOptions()

}


Expand All @@ -817,7 +839,7 @@ export function BasicDeatilsForm({ editMode }) {




if (isClient) {


Expand Down Expand Up @@ -1732,7 +1754,7 @@ export function BasicDeatilsForm({ editMode }) {

</div>
</div>

{/* Sub-county */}
<div className='md:col-start-2 col-span-1 '>
<div className='w-full flex flex-col items-start justify-start gap-1 mb-3'>
Expand All @@ -1746,6 +1768,7 @@ export function BasicDeatilsForm({ editMode }) {
*
</span>
</label>

<CustomSelect
options={subCountyOptions ?? []}
required
Expand All @@ -1755,7 +1778,6 @@ export function BasicDeatilsForm({ editMode }) {
onFocus={handleFocus}
name='sub_county_id'


/>

</div>
Expand Down
Loading

0 comments on commit 3403dc0

Please sign in to comment.