Skip to content

Commit

Permalink
Added zod validation in getServerSideProps to validate ctx.query
Browse files Browse the repository at this point in the history
  • Loading branch information
ertush committed Jul 17, 2024
1 parent 239b4ab commit 41618ef
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion components/Forms/CommunityUnitsForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ function EditCommunityUnitsCHEWSForm(props) {

const [healthUnitWorkers, setHealthUnitWorkers] = useState(props?.health_unit_workers)
const alert = useAlert()
const [deleteButton, setDeleteButton] = useState(props?.health_unit_workers.map((_, i) => ({[i]: false})))
const [deleteButton, setDeleteButton] = useState(props?.health_unit_workers?.map((_, i) => ({[i]: false})))

console.log({deleteButton})

Expand Down
3 changes: 2 additions & 1 deletion components/Forms/formComponents/EditListItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,14 @@ function EditListItem({
if(
itemName == 'chul_services' &&
editMode && itemData && itemData?.partners !== null &&
itemData?.partners.length > 0){
itemData?.partners?.length > 0){
setPartners(itemData?.partners)

}


if (Array.isArray(itemData?.currentServices)) {

const result = []

setSelectedItems(() => {
Expand Down
10 changes: 8 additions & 2 deletions pages/community-units/edit/[id].js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CommunityUnitEditForm } from '../../../components/Forms/CommunityUnitsForms'
import { useState, useEffect, createContext } from 'react'
import { checkToken } from '../../../controllers/auth/auth'
import {z} from 'zod'
// import Alert from '@mui/material/Alert'


Expand Down Expand Up @@ -50,8 +51,13 @@ export async function getServerSideProps(ctx) {
"services"
]

const zSchema = z.object({
id: z.string().uuid('Should be a uuid string'),
})


const queryId = zSchema.parse(ctx.query).id


function fetchFacilities(url) {

Expand Down Expand Up @@ -93,7 +99,7 @@ export async function getServerSideProps(ctx) {
switch (option) {

case "cu":
const cu = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/chul/units/${ctx?.query.id}/`, {
const cu = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/chul/units/${queryId}/`, {
headers: {
'Authorization': 'Bearer ' + token,
'Accept': 'application/json'
Expand All @@ -119,7 +125,7 @@ export async function getServerSideProps(ctx) {

case "facilities":

const url = `${process.env.NEXT_PUBLIC_API_URL}/common/sub_counties/?name=${response?.cu?.facility_subcounty.split(' ').join('+')}`
const url = `${process.env.NEXT_PUBLIC_API_URL}/common/sub_counties/?name=${response?.cu?.facility_subcounty.split(' ').join('+')}`

response['facilities'] = await fetchFacilities(url)

Expand Down

0 comments on commit 41618ef

Please sign in to comment.