From 90b16c707fea059739d4bedc3a477ab57862c67c Mon Sep 17 00:00:00 2001 From: eric Date: Mon, 16 Dec 2024 14:33:59 +0300 Subject: [PATCH 1/3] Updated pages/community-units/edit/[id].js community-units Edit Form --- pages/community-units/edit/[id].js | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/pages/community-units/edit/[id].js b/pages/community-units/edit/[id].js index 2dc41a9..94b96ed 100644 --- a/pages/community-units/edit/[id].js +++ b/pages/community-units/edit/[id].js @@ -61,14 +61,10 @@ export async function getServerSideProps(ctx) { const queryId = zSchema.parse(ctx.query).id - async function fetchFacilities() { + async function fetchFacilities(wardCode) { - const { response: user } = await getUserDetails(token, `${process.env.NEXT_PUBLIC_API_URL}/rest-auth/user/`) - - const params = `?sub_county=${user?.user_sub_counties?.length > 1 ? user?.user_sub_counties?.map(({sub_county}) => sub_county)?.join(',') : user?.user_sub_counties[0]}&fields=id,name,county,sub_county_name,constituency,ward_name` - - return fetch(`${process.env.NEXT_PUBLIC_API_URL}/facilities/facilities/${params}&reporting_in_dhis=true&closed=false`, { /*&owner_type=6a833136-5f50-46d9-b1f9-5f961a42249f*/ + return fetch(`${process.env.NEXT_PUBLIC_API_URL}/facilities/facilities/?ward_code=${wardCode}&reporting_in_dhis=true&closed=false`, { /*&owner_type=6a833136-5f50-46d9-b1f9-5f961a42249f*/ headers: { 'Authorization': 'Bearer ' + token, 'Accept': 'application/json' @@ -76,8 +72,7 @@ export async function getServerSideProps(ctx) { }) .then(resp => resp.json()) - .then(resp => { - + .then(resp => { return resp?.results?.map(({ id, name }) => ({ label: name, value: id })) }) .catch(console.error) @@ -104,7 +99,7 @@ export async function getServerSideProps(ctx) { }) - response["cu"] = await (await cu.json()) + response["cu"] = (await (await cu.json())) ?? [] break; @@ -117,16 +112,13 @@ export async function getServerSideProps(ctx) { }) - response["statuses"] = (await (await statuses.json()))?.results?.map(({ id, name }) => ({ label: name, value: id })) + response["statuses"] = ((await (await statuses.json()))?.results?.map(({ id, name }) => ({ label: name, value: id }))) ?? [] break; case "facilities": - // const url = `${process.env.NEXT_PUBLIC_API_URL}/common/sub_counties/?name=${response?.cu?.facility_subcounty.split(' ').join('+')}` - // const { response: user } = await getUserDetails(token, `${process.env.NEXT_PUBLIC_API_URL}/rest-auth/user/`) - const url = `${process.env.NEXT_PUBLIC_API_URL}/rest-user/` - - response['facilities'] = await fetchFacilities(url) + + response['facilities'] = (await fetchFacilities(response?.cu?.ward_code)) ?? [] break; @@ -139,7 +131,7 @@ export async function getServerSideProps(ctx) { }) - response["contact_types"] = (await (await contact_types.json()))?.results?.map(({ id, name }) => ({ label: name, value: id })) + response["contact_types"] = ((await (await contact_types.json()))?.results?.map(({ id, name }) => ({ label: name, value: id }))) ?? [] break; case "services": @@ -151,7 +143,7 @@ export async function getServerSideProps(ctx) { }) - response["services"] = (await (await services.json()))?.results + response["services"] = ((await (await services.json()))?.results) ?? [] break; } } From 4c7f1be2f64a9190570d4029eef89378c2347992 Mon Sep 17 00:00:00 2001 From: eric Date: Mon, 16 Dec 2024 15:51:39 +0300 Subject: [PATCH 2/3] Hotfix: Fixed the issue of Linked facilities not displaying in CHU Add form --- pages/community-units/add.js | 50 ++++++++++++++++------------------ pages/community-units/index.js | 14 +++++++++- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/pages/community-units/add.js b/pages/community-units/add.js index d975afa..5d61403 100644 --- a/pages/community-units/add.js +++ b/pages/community-units/add.js @@ -2,7 +2,7 @@ import React, { useState, useEffect, createContext, useContext } from 'react'; import Head from 'next/head'; import MainLayout from '../../components/MainLayout'; import EditListItem from '../../components/Forms/formComponents/EditListItem'; -import { checkToken } from '../../controllers/auth/auth'; +import { checkToken, getUserDetails } from '../../controllers/auth/auth'; import Box from '@mui/material/Box'; import Stepper from '@mui/material/Stepper'; import Step from '@mui/material/Step'; @@ -1337,7 +1337,7 @@ function AddCommunityUnit(props) { ); } -export async function getServerSideProps({req, res}) { +export async function getServerSideProps({req, res, query}) { res?.setHeader( 'Cache-Control', @@ -1357,28 +1357,23 @@ export async function getServerSideProps({req, res}) { ] - function fetchFacilities(url) { - return fetch(url, { - headers: { - 'Accept': 'application/json', - 'Authorization': `Bearer ${token}` - } - }) - .then(resp => resp.json()) - .then( (resp) => { - + function fetchFacilities(user/*userData*/) { + + // const decodedUser = Buffer.from(userData, 'base64').toString() + // const user = JSON.parse(decodedUser) + - const userSubCountyIDs = resp?.user_sub_counties.length > 1 ? resp?.user_sub_counties.map(({sub_county}) => sub_county).join(',') : resp?.user_sub_counties[0]?.sub_county - const userCountyID = resp?.county - const userGroup = resp?.groups[0]?.id - - - if(userGroup == 2 && userSubCountyIDs){ - - const subCountyFacilitiesURL = `${process.env.NEXT_PUBLIC_API_URL}/facilities/facilities/?sub_county=${userSubCountyIDs}&reporting_in_dhis=true&closed=false&fields=id,name,county,sub_county_name,constituency,ward_name&page_size=300` + const userSubCountyIDs = user?.sub_county + const userCountyID = user?.county + const userGroup = user?.group + // debug + console.log({userSubCountyIDs,userCountyID,userGroup}) + + if(userGroup == 2 && userSubCountyIDs){ + const subCountyFacilitiesURL = `${process.env.NEXT_PUBLIC_API_URL}/facilities/facilities/?sub_county=${userSubCountyIDs}&reporting_in_dhis=true&closed=false&fields=id,name,county,sub_county_name,constituency,ward_name&page_size=500` return fetch(subCountyFacilitiesURL, { headers:{ @@ -1424,12 +1419,11 @@ export async function getServerSideProps({req, res}) { }) } else { - console.log('default case') return [] } - }) - .catch(e => console.error('Error rest-auth user :', e.message)) + // }) + // .catch(e => console.error('Error rest-auth user :', e.message)) } @@ -1454,10 +1448,12 @@ export async function getServerSideProps({req, res}) { break; case "facilities": - - const url = `${process.env.NEXT_PUBLIC_API_URL}/rest-auth/user/` - - response['facilities'] = await fetchFacilities(url) + + response['facilities'] = await fetchFacilities({ + county: query?.county, + sub_county: query?.sub_county, + group: query?.group + } ?? '') break; case "contact_types": diff --git a/pages/community-units/index.js b/pages/community-units/index.js index fddc626..698affd 100644 --- a/pages/community-units/index.js +++ b/pages/community-units/index.js @@ -115,7 +115,19 @@ function CommunityUnit(props) { className='px-4 py-2 bg-gray-600 rounded text-white text-sm tracking-tighter font-semibold whitespace-nowrap hover:bg-black focus:bg-black active:bg-black uppercase'>