From 5df5d10296701ac891bf17247d57ac58a6aabb61 Mon Sep 17 00:00:00 2001 From: Pierre-Louis Guhur Date: Tue, 17 Jan 2023 20:59:46 +0100 Subject: [PATCH] fix: results page --- components/admin/CandidateField.tsx | 27 +++++++++++----------- pages/results/[pid]/[[...tid]].tsx | 3 +-- services/ElectionContext.tsx | 36 ++++++++++++++++------------- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/components/admin/CandidateField.tsx b/components/admin/CandidateField.tsx index d964903..ec7c547 100644 --- a/components/admin/CandidateField.tsx +++ b/components/admin/CandidateField.tsx @@ -1,13 +1,13 @@ /** * This is the candidate field used during election creation */ -import { useState } from 'react'; +import {useState} from 'react'; import Image from 'next/image'; -import { useTranslation } from 'next-i18next'; -import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; -import { faPlus, faTrashCan } from '@fortawesome/free-solid-svg-icons'; -import { useSortable } from '@dnd-kit/sortable'; -import { ElectionTypes, useElection } from '@services/ElectionContext'; +import {useTranslation} from 'next-i18next'; +import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'; +import {faPlus, faTrashCan} from '@fortawesome/free-solid-svg-icons'; +import {useSortable} from '@dnd-kit/sortable'; +import {ElectionTypes, useElection, isCreated} from '@services/ElectionContext'; import VerticalGripDots from '@components/VerticalGripDots'; import whiteAvatar from '../../public/avatar.svg'; import CandidateModalSet from './CandidateModalSet'; @@ -28,7 +28,7 @@ const CandidateField = ({ editable = true, ...props }: CandidateProps) => { - const { t } = useTranslation(); + const {t} = useTranslation(); const [election, dispatch] = useElection(); const candidate = election.candidates[position]; @@ -38,11 +38,13 @@ const CandidateField = ({ const [modalDel, setModalDel] = useState(false); const [modalSet, setModalSet] = useState(false); - const { attributes, listeners, setNodeRef, transform, transition } = - useSortable({ id: position + 1 }); + const {attributes, listeners, setNodeRef, transform, transition} = + useSortable({id: position + 1}); const addCandidate = () => { - dispatch({ type: ElectionTypes.CANDIDATE_PUSH, value: 'default' }); + if (!isCreated(election)) { + dispatch({type: ElectionTypes.CANDIDATE_PUSH, value: 'default'}); + } }; const toggleSet = () => setModalSet((m) => !m); @@ -74,9 +76,8 @@ const CandidateField = ({ src={image} width={24} height={24} - className={`${ - image == defaultAvatar ? 'default-avatar' : '' - } bg-primary`} + className={`${image == defaultAvatar ? 'default-avatar' : '' + } bg-primary`} alt={t('common.thumbnail')} />
diff --git a/pages/results/[pid]/[[...tid]].tsx b/pages/results/[pid]/[[...tid]].tsx index 8fde3f1..f3c2f5f 100644 --- a/pages/results/[pid]/[[...tid]].tsx +++ b/pages/results/[pid]/[[...tid]].tsx @@ -51,7 +51,7 @@ export async function getServerSideProps({query, locale}) { const numGrades = payload.grades.length; const grades = payload.grades.map((g, i) => ({ ...g, - color: getGradeColor(i, numGrades), + color: getGradeColor(g.value, numGrades), })); const gradesByValue: {[key: number]: GradeResultInterface} = {}; grades.forEach((g) => (gradesByValue[g.value] = g)); @@ -527,7 +527,6 @@ const ResultPage = ({
{t('result.details')}
{Object.keys(candidateByRank) - .sort() .map((rank, i) => { return ( ( [defaultElection, () => {}] ); -export function ElectionProvider({ children }) { +export function ElectionProvider({children}) { /** * Provide the election and the dispatch to all children components */ @@ -155,25 +155,25 @@ function electionReducer( */ switch (action.type) { case ElectionTypes.RESET: { - return { ...action.value }; + return {...action.value}; } case ElectionTypes.SET: { - return { ...election, [action.field]: action.value }; + return {...election, [action.field]: action.value}; } case ElectionTypes.CANDIDATE_PUSH: { if (typeof action.value === 'string' && action.value !== 'default') { throw Error('Unexpected action'); } const candidate = - action.value === 'default' ? { ...defaultCandidate } : action.value; + action.value === 'default' ? {...defaultCandidate} : action.value; const candidates = [...election.candidates, candidate]; if (candidates.filter((c) => !c.active).length === 0) { return { ...election, - candidates: [...candidates, { ...defaultCandidate }], + candidates: [...candidates, {...defaultCandidate}], }; } else { - return { ...election, candidates }; + return {...election, candidates}; } } case ElectionTypes.CANDIDATE_RM: { @@ -182,7 +182,7 @@ function electionReducer( } const candidates = [...election.candidates]; candidates.splice(action.position, 1); - return { ...election, candidates }; + return {...election, candidates}; } case ElectionTypes.CANDIDATE_SET: { if (typeof action.position !== 'number') { @@ -198,14 +198,14 @@ function electionReducer( if (candidates.filter((c) => !c.active).length === 0) { return { ...election, - candidates: [...candidates, { ...defaultCandidate }], + candidates: [...candidates, {...defaultCandidate}], }; } - return { ...election, candidates }; + return {...election, candidates}; } case ElectionTypes.GRADE_PUSH: { const grades = [...election.grades, action.value]; - return { ...election, grades }; + return {...election, grades}; } case ElectionTypes.GRADE_RM: { if (typeof action.position !== 'number') { @@ -213,7 +213,7 @@ function electionReducer( } const grades = [...election.grades]; grades.splice(action.position); - return { ...election, grades }; + return {...election, grades}; } case ElectionTypes.GRADE_SET: { if (typeof action.position !== 'number') { @@ -222,11 +222,15 @@ function electionReducer( const grades = [...election.grades]; const grade = grades[action.position]; grade[action.field] = action.value; - return { ...election, grades }; + return {...election, grades}; } } } +export const isCreated = (election: ElectionContextInterface) => { + return election.ref !== null && election.ref !== "" && election.ref !== undefined; +}; + export const isClosed = (election: ElectionContextInterface) => { const dateEnd = new Date(election.dateEnd); const now = new Date();