From 97101259bc1a4d97d3cbbc1630a2b27930415257 Mon Sep 17 00:00:00 2001 From: eric Date: Mon, 12 Aug 2024 13:01:59 +0300 Subject: [PATCH] Updated approveCHUUpdates function --- pages/_app.js | 74 ++-------------------- pages/community-units/approve/[id].js | 90 +++++++++++++++------------ pages/dashboard/index.js | 2 +- pages/index.js | 31 ++++----- 4 files changed, 68 insertions(+), 129 deletions(-) diff --git a/pages/_app.js b/pages/_app.js index 05fc74c5..8c7352d9 100644 --- a/pages/_app.js +++ b/pages/_app.js @@ -12,9 +12,10 @@ import Backdrop from '@mui/material/Backdrop'; import CircularProgress from '@mui/material/CircularProgress'; import { useEffect, useState } from 'react'; // import { getUserDetails } from "../controllers/auth/public_auth"; +import { SWRConfig } from 'swr' import { IsUserLoggedInCtx } from '.'; -function LoadAnimation({open}) { +function LoadAnimation({open}) { return ( theme.zIndex.drawer + 1 }} @@ -52,76 +53,9 @@ export default function App(props) { router.events.on('routeChangeError', () => {setIsNavigating(false)}); - // if(Number(userID) !== 6) { - // // window.localStorage.removeItem('user') - // // window.localStorage.clear() - // router.push('/logout') - - // } - let mtd = true; - // function initializePage() { - - // const API_URL = process.env.NEXT_PUBLIC_API_URL; - - // console.log(router.asPath) - - if (mtd) { - - // console.log(' Checking if user is logged in ..') - - // if (typeof window !== "undefined") { - - - // } - - // (typeof window !== "undefined" && window.document.cookie.indexOf("access_token=") > -1) || false; - - - // let session_token = null; - - // if (is_user_logged_in) { - // session_token = JSON.parse( - // window.document.cookie.split("access_token=")[1].split(";")[0] - // ); - // } - - // if ( - // // !is_user_logged_in - // typeof window == "undefined" && - // session_token == null - - // ) { - // console.log('Fetching User....') - // getUserDetails(session_token.token, `${API_URL}/rest-auth/user/`).then( - // (usr) => { - - // if (usr.error || usr.detail) { - // setIsLoggedIn(false); - // setUser(null); - // } else { - - // if(usr.type !== undefined) { - // console.log({usr}) - // usr.type == 6 ? setIsLoggedIn(false) : setIsLoggedIn(true); - // } - - // setUser(usr); - - // } - // } - // ); - // } else { - - // console.log("no session. Refreshing..."); - // // router.push('/auth/login') - // } - } - // } - - // initializePage() // Logout after 30 mins of inactivity const time = 60 * 1000 * 30 @@ -189,7 +123,9 @@ export default function App(props) { { isNavigating && } - + new Map() }}> + + diff --git a/pages/community-units/approve/[id].js b/pages/community-units/approve/[id].js index 15cbf339..b417eaa0 100644 --- a/pages/community-units/approve/[id].js +++ b/pages/community-units/approve/[id].js @@ -36,31 +36,31 @@ function ApproveCommunityUnit(props) { const columns = [ - { label: 'Field', minWidth: 100 }, - { label: 'Old Value', minWidth: 100 }, - { label: 'New Value', minWidth: 100, } + { label: 'Field', minWidth: 100, id: uuid()}, + { label: 'Old Value', minWidth: 100, id: uuid() }, + { label: 'New Value', minWidth: 100, id: uuid()} ]; const CHULDetails = [ - { value: `${cu.facility_subcounty}`, label: 'Sub County ' }, - { value: `${cu.facility_constituency}`, label: 'Constituency' }, - { value: `${cu.facility_ward}`, label: 'Ward' }, - { value: `${cu.households_monitored}`, label: 'Households Monitored' }, + { value: `${cu.facility_subcounty}`, label: 'Sub County ', id: uuid()}, + { value: `${cu.facility_constituency}`, label: 'Constituency', id: uuid() }, + { value: `${cu.facility_ward}`, label: 'Ward', id: uuid() }, + { value: `${cu.households_monitored}`, label: 'Households Monitored', id: uuid() }, ]; const CHU_MainDetails = [ - { value: `${cu.status_name}`, label: 'Functional Status' }, - { value: `${cu.code}`, label: 'CHU Code' }, - { value: `${cu.number_of_chvs}`, label: 'Number of CHVs' }, - { value: `${cu.facility_name}`, label: 'Linked Facility' }, - { value: `${cu.facility_county}`, label: 'County' }, + { value: `${cu.status_name}`, label: 'Functional Status', id: uuid()}, + { value: `${cu.code}`, label: 'CHU Code', id: uuid()}, + { value: `${cu.number_of_chvs}`, label: 'Number of CHVs', id: uuid() }, + { value: `${cu.facility_name}`, label: 'Linked Facility', id: uuid() }, + { value: `${cu.facility_county}`, label: 'County', id: uuid() }, ] + function approveCHU (e, token) { e.preventDefault(); - if (e.target.name == 'btn_approve_chu') setIsSubmittingApproval(true); @@ -118,16 +118,19 @@ function ApproveCommunityUnit(props) { alert.error(`Unable to approve CHU`) const detail = await res.json() + + const error = Object.entries(detail)?.map(([k, v]) => ([k, v]))?.join(":") + + if(error) throw new Error(error.replace(',', ':')) - const error = Array.isArray(Object.values(detail)) && Object.values(detail).length == 1 ? detail[Object.keys(detail)[0]][0] : '' - setFormError(error) + // const error = Array.isArray(Object.values(detail)) && Object.values(detail).length == 1 ? detail[Object.keys(detail)[0]][0] : '' } }) .catch(e => { - + setFormError(e.message) console.error(e.message) }) .finally(() => { @@ -144,6 +147,10 @@ async function approveCHUUpdates (e, token) { e.preventDefault(); // console.log({status}) + + console.log(formError) + + let payload = '' if (e.target.name == 'btn_approve_chu_updates') { setIsSubmittingApproval(true) @@ -166,10 +173,12 @@ async function approveCHUUpdates (e, token) { method: 'PATCH', body: JSON.stringify(payload) }) - .then(resp => resp.json()) - .then(async (res) => { + .then(resp => ({resp, data: resp.json()})) + .then(async ({resp, data}) => { + + + if(resp.ok) { - if(res.ok) { alert.success(`${payload.is_rejected ? 'Rejected' : 'Approved'} CHU Updates successfully`) router.push({ @@ -180,17 +189,17 @@ async function approveCHUUpdates (e, token) { } else { alert.error(`Unable to approve CHU Updates`) - const detail = await res.json() - - const error = Array.isArray(Object.values(detail)) && Object.values(detail).length == 1 ? detail[Object.keys(detail)[0]][0] : '' - - setFormError(error) + const detail = await data + + const error = Object.entries(detail)?.map(([k, v]) => ([k, v]))?.join(":") + + if(error) throw new Error(error.replace(',', ':')) } }) .catch(e => { - console.log(e.message) + setFormError(e.message) }) } catch (e) { @@ -317,17 +326,16 @@ async function approveCHUUpdates (e, token) { {/* CHU details */}
- {CHU_MainDetails.map((dt) => { - - const id = uuid() + {CHU_MainDetails.map(({label, value, id}) => { + return (

- {dt.value || " - "} + {value || " - "}

)})} @@ -370,14 +378,13 @@ async function approveCHUUpdates (e, token) { {!isCHULDetails && (
- {CHULDetails.map((dt) => { - const id = uuid() - + {CHULDetails.map(({label, value, id}) => { + return (
- +

- {dt.value || " - "} + {value || " - "}

)})} @@ -397,6 +404,9 @@ async function approveCHUUpdates (e, token) {

Pending Updates

+ + {formError && {formError}} +
{columns.map((column) => { - const id = uuid() - return ( @@ -558,9 +566,9 @@ async function approveCHUUpdates (e, token) { } if (key == 'workers') { const workers = cu.pending_updates['workers'].map((item) => { - const id = uuid() + // const id = uuid() - return
+ return

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

@@ -625,7 +633,7 @@ async function approveCHUUpdates (e, token) {

Approval comment:

{/* {cu.is_approved} */} - {formError && {formError}} + {formError !== null && {formError}} { - if (!startTimestamp) startTimestamp = timestamp; - const progress = Math.min((timestamp - startTimestamp) / duration, 1); - event.target.innerHTML = Math.floor(progress * (end - start) + start); - if (progress < 1) { - window.requestAnimationFrame(step); - } - }; - window.requestAnimationFrame(step); - } + // function animateValue(event, start, end, duration) { + // let startTimestamp = null; + // const step = (timestamp) => { + // if (!startTimestamp) startTimestamp = timestamp; + // const progress = Math.min((timestamp - startTimestamp) / duration, 1); + // event.target.innerHTML = Math.floor(progress * (end - start) + start); + // if (progress < 1) { + // window.requestAnimationFrame(step); + // } + // }; + // window.requestAnimationFrame(step); + // }