Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed local API query being used instead of REST API #101

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
61 changes: 42 additions & 19 deletions src/app/(pages)/vote/[electionId]/[positionId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,61 @@ import Link from 'next/link'
import { Button } from '../../../../_components/Button'
import { Media } from '../../../../_components/Media'
import classes from '../../../nominations/[electionId]/NominationPage/index.module.scss'
// eslint-disable-next-line import/no-duplicates
import payload from 'payload'
import payloadConfig from '../../../../../payload/payload.config'
import { VoteCandidate } from '../../../../_components/VoteCandidate'
import { getID } from '../../../../../payload/utilities/getID'
import DraggableList from '../../../../_components/DraggableList'
import { VoteCandidateList } from '../../../../_components/VoteCandidateList'
import qs from 'qs'

// Pres = http://localhost:3000/vote/65ea0784b436290ac4943c39/65e62035b733f7583ee3b795

// const initializePayload = async () => {
// const config = await payloadConfig
// const initOptions = {
// ...config,
// secret: process.env.PAYLOAD_SECRET, // Ensure this environment variable is set
// }
// return getPayload(initOptions)
// }

const getCandidates = async (electionId: string, positionId: string) => {
return await payload.find({
collection: 'nominations',
pagination: false,
where: {
election: {
equals: electionId,
},
position: {
equals: positionId,
},
droppedOut: {
equals: false,
},
const query = {
election: {
equals: electionId,
},
depth: 2,
})
position: {
equals: positionId,
},
droppedOut: {
equals: false,
},
}

const stringifiedQuery = qs.stringify(
{
where: query,
pagination: false,
depth: 2,
},
{ addQueryPrefix: true },
)

const response = await fetch(`http://localhost:3000/api/nominations${stringifiedQuery}`)
if (!response.ok) {
throw new Error('Failed to fetch candidates')
}
return await response.json()
}

const getPosition = async (positionId: string) => {
return await payload.findByID({
collection: 'positions',
id: positionId,
})
const response = await fetch(`http://localhost:3000/api/positions/${positionId}`)
if (!response.ok) {
throw new Error('Failed to fetch position')
}
return await response.json()
}

export default async function Nomination({ params: { electionId, positionId } }) {
Expand Down
Loading