Skip to content

Commit

Permalink
Merge pull request #475 from ertush/hotfix
Browse files Browse the repository at this point in the history
Hotfix: Added safe gaurds for fetching CUs based on user group commun…
  • Loading branch information
ertush authored Sep 6, 2024
2 parents 75fbd4c + 85f853b commit 98ad9b8
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 130 deletions.
45 changes: 37 additions & 8 deletions components/CommunityUnitSideMenu.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import React from 'react'
import { useContext } from 'react'
import List from '@mui/material/List';
import ListItemButton from '@mui/material/ListItemButton';
import ListItemText from '@mui/material/ListItemText';
import { useRouter } from 'next/router';
import { useSearchParams } from 'next/navigation';
import { UserContext } from '../providers/user';


export default function CommunityUnitSideMenu({ _pathId, filters, qf }) {
export default function CommunityUnitSideMenu({ /*_pathId,*/ filters /*, qf*/ }) {


const router = useRouter()

const searchParams = useSearchParams()

const userCtx = useContext(UserContext)

const userGroup = userCtx?.groups[0]?.id

const userCounty = userCtx?.user_counties[0]?.county

const userSubCounty = userCtx?.user_sub_counties[0]?.sub_county

function userOrgUnit() {
if(userGroup === 1) { // CHRIO
return {county: userCounty}
} else if (userGroup === 2) {
return {sub_county: userSubCounty}
} else {
return {}
}
}



const quickFilters = [
{
Expand Down Expand Up @@ -137,7 +156,9 @@ export default function CommunityUnitSideMenu({ _pathId, filters, qf }) {
router.push({
pathname:'/community-units',
query: {
filter:'all_chu'
filter:'all_chu',
...userOrgUnit()

}
})

Expand All @@ -149,7 +170,9 @@ export default function CommunityUnitSideMenu({ _pathId, filters, qf }) {
pathname:'/community-units',
query: {
filter:'approved_chu',
is_approved: true
is_approved: true,
...userOrgUnit()

}
})

Expand All @@ -162,7 +185,9 @@ export default function CommunityUnitSideMenu({ _pathId, filters, qf }) {
query: {
filter:'new_pending_approval_chu',
pending_approval: true,
has_edits: false
has_edits: false,
...userOrgUnit()

}
})

Expand All @@ -177,7 +202,9 @@ export default function CommunityUnitSideMenu({ _pathId, filters, qf }) {
query: {
filter:'updated_pending_approval_chu',
is_approved: true,
has_edits: true
has_edits: true,
...userOrgUnit()

}
})

Expand All @@ -190,7 +217,9 @@ export default function CommunityUnitSideMenu({ _pathId, filters, qf }) {
pathname:'/community-units',
query: {
filter:'rejected_chu',
is_rejected: true
is_rejected: true,
...userOrgUnit()

}
})

Expand Down
81 changes: 2 additions & 79 deletions components/FacilitySideMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PermissionContext } from '../providers/permissions';
import { useSearchParams } from 'next/navigation';
import { UserContext } from '../providers/user';

function FacilitySideMenu({ states, stateSetters, filters }) {
function FacilitySideMenu(/*{ states, stateSetters, filters }*/) {

const userPermissions = useContext(PermissionContext)

Expand All @@ -23,84 +23,7 @@ function FacilitySideMenu({ states, stateSetters, filters }) {

const userSubCounty = userCtx?.user_sub_counties[0]?.sub_county

// const quickFilters = [
// {
// name: 'All',
// id: 'all',
// filters: Object.keys(filters),
// },
// {
// name: 'Approved',
// id: 'approved',
// filters: [
// { id: "approved", value: true },
// { id: "approved_national_level", value: true },
// { id: "rejected", value: false },
// ],
// },
// {
// name: 'New pending validation',
// id: 'new_pending_validation',
// filters: [
// { id: "pending_approval", value: true },
// { id: "has_edits", value: false },

// ],
// },
// {
// name: 'Updated pending validation',
// id: 'updated_pending_validation',
// filters: [
// { id: "has_edits", value: true },
// { id: "pending_approval", value: true },
// ],
// },
// {
// name: 'Facilities pending approval',
// id: 'to_publish',
// filters: [
// { id: "to_publish", value: true },
// ],
// },
// {
// name: 'DHIS Synced Facilities',
// id: 'dhis_synced_facilities',
// filters: [
// { id: "approved", value: true },
// { id: "approved_national_level", value: true },
// { id: "rejected", value: false },
// { id: "reporting_in_dhis", value: true },
// ]
// },
// {
// name: 'Failed Validation',
// id: 'failed_validation',
// filters: [
// { id: "rejected", value: true },
// ],
// },
// {
// name: 'Incomplete',
// id: 'incomplete',
// filters: [
// { id: "is_complete", value: false },
// ]
// },
// {
// name: 'Rejected',
// id: 'rejected',
// filters: [
// { id: "rejected_national", value: true },
// ]
// },
// {
// name: 'Closed',
// id: 'closed',
// filters: [
// { id: "closed", value: true },
// ]
// }
// ]


function userOrgUnit() {
if(userGroup === 1) { // CHRIO
Expand Down
69 changes: 33 additions & 36 deletions pages/community-units/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { KeyboardArrowDown } from '@mui/icons-material';
import { KeyboardArrowRight } from '@mui/icons-material';
import { useSearchParams } from 'next/navigation';
import withAuth from '../../components/ProtectedRoute';

import { getUserDetails } from '../../controllers/auth/auth';

function CommunityUnit(props) {

Expand Down Expand Up @@ -253,6 +253,12 @@ function CommunityUnit(props) {
</button>

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

<div className="w-full md:col-span-4 mr-24 md:col-start-2 col-span-5 md:h-auto rounded bg-gray-50 shadow-md">
{/* Data Indicator section */}
Expand Down Expand Up @@ -359,7 +365,7 @@ function CommunityUnit(props) {
<div className="flex flex-col justify-center items-center w-full">

{cus && cus.length > 0 ? (
cus.map((comm_unit, index) => (
cus.map((comm_unit) => (
<div
key={comm_unit.id}
className='grid grid-cols-8 gap-2 border-b border-gray-400 py-4 hover:bg-gray-50 w-full'>
Expand Down Expand Up @@ -406,10 +412,8 @@ function CommunityUnit(props) {
<span className="whitespace-pre-line">{comm_unit.facility_ward || 'N/A'}</span>
</div>
<div className="flex flex-col items-start justify-start gap-0 leading-none whitespace-pre-wrap">
<label className="text-xs text-gray-500">Constituency:</label>
<span className="whitespace-pre-line">{comm_unit.constituency_name ||
comm_unit.facility_constituency ||
'N/A'}</span>
<label className="text-xs text-gray-500">Date Established:</label>
<span className="whitespace-pre-line">{new Date(comm_unit?.date_established).toDateString()}</span>
</div>
</div>
</div>
Expand Down Expand Up @@ -588,10 +592,29 @@ export async function getServerSideProps(ctx) {
url = `${url}&page=${ctx.query.page}`;
}

// console.log({url})

const {response: user} = await getUserDetails(token, `${process.env.NEXT_PUBLIC_API_URL}/rest-auth/user/`)

const userGroup = user?.groups[0]?.id


try {

if(userGroup === 1) { // CHRIO

const userCountyID = user?.county
url = `${url}&county=${userCountyID}`

} else if(userGroup === 2) { // SCHRIO

const userSubCountyID = user?.user_sub_counties[0]?.sub_county
url = `${url}&sub_county=${userSubCountyID}`

}

// console.log({url})


const r = await fetch(url, {
headers: {
Authorization: 'Bearer ' + token,
Expand Down Expand Up @@ -625,36 +648,10 @@ export async function getServerSideProps(ctx) {
}
};

const token = (await checkToken(ctx.req, ctx.res))?.token

const response = (() => checkToken(ctx.req, ctx.res)
.then((t) => {
if (t.error) {
throw new Error('Error checking token');
} else {
let token = t.token;
return fetchData(token).then((t) => t);
}
})
.catch((err) => {
console.log('Error checking token: ', err);
if (typeof window !== 'undefined' && window) {
if (ctx?.asPath) {
window.location.href = ctx?.asPath;
} else {
window.location.href = '/community-units';
}
}
// setTimeout(() => {
return {
error: true,
err: err,
data: [],
query: {},
path: ctx.asPath || '/community-units',
current_url: '',
};
// }, 1000);
}))()
const response = await fetchData(token)



return {
Expand Down
16 changes: 9 additions & 7 deletions pages/facilities/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,11 @@ function FacilityHome (props){
{/* Side Menu Filters Wide View port*/}
<div className="hidden md:flex col-span-1">
<FacilitySideMenu
filters={filters ?? {}}
/*filters={filters ?? {}}
states={[khisSynched, facilityFeedBack, pathId, allFctsSelected, title]}
stateSetters={[setKhisSynched, setFacilityFeedBack, setPathId, setAllFctsSelected, setTitle]} />
stateSetters={[setKhisSynched, setFacilityFeedBack, setPathId, setAllFctsSelected, setTitle]}
*/
/>
</div>

{/* Side Menu Filters Small View port*/}
Expand Down Expand Up @@ -872,10 +874,10 @@ function FacilityHome (props){
export async function getServerSideProps(ctx) {


// ctx?.res?.setHeader(
// 'Cache-Control',
// 'public, s-maxage=10, stale-while-revalidate=59'
// )
ctx?.res?.setHeader(
'Cache-Control',
'public, s-maxage=10, stale-while-revalidate=59'
)


// function fetchCurrentUser(token) {
Expand Down Expand Up @@ -1020,7 +1022,7 @@ export async function getServerSideProps(ctx) {

}

console.log({url})
// console.log({url})

facilities = (await (await fetch(url, {
headers: {
Expand Down

0 comments on commit 98ad9b8

Please sign in to comment.