fix: results page

pull/100/head
Pierre-Louis Guhur 1 year ago
parent c67f317563
commit 5df5d10296

@ -1,13 +1,13 @@
/** /**
* This is the candidate field used during election creation * This is the candidate field used during election creation
*/ */
import { useState } from 'react'; import {useState} from 'react';
import Image from 'next/image'; import Image from 'next/image';
import { useTranslation } from 'next-i18next'; import {useTranslation} from 'next-i18next';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
import { faPlus, faTrashCan } from '@fortawesome/free-solid-svg-icons'; import {faPlus, faTrashCan} from '@fortawesome/free-solid-svg-icons';
import { useSortable } from '@dnd-kit/sortable'; import {useSortable} from '@dnd-kit/sortable';
import { ElectionTypes, useElection } from '@services/ElectionContext'; import {ElectionTypes, useElection, isCreated} from '@services/ElectionContext';
import VerticalGripDots from '@components/VerticalGripDots'; import VerticalGripDots from '@components/VerticalGripDots';
import whiteAvatar from '../../public/avatar.svg'; import whiteAvatar from '../../public/avatar.svg';
import CandidateModalSet from './CandidateModalSet'; import CandidateModalSet from './CandidateModalSet';
@ -28,7 +28,7 @@ const CandidateField = ({
editable = true, editable = true,
...props ...props
}: CandidateProps) => { }: CandidateProps) => {
const { t } = useTranslation(); const {t} = useTranslation();
const [election, dispatch] = useElection(); const [election, dispatch] = useElection();
const candidate = election.candidates[position]; const candidate = election.candidates[position];
@ -38,11 +38,13 @@ const CandidateField = ({
const [modalDel, setModalDel] = useState(false); const [modalDel, setModalDel] = useState(false);
const [modalSet, setModalSet] = useState(false); const [modalSet, setModalSet] = useState(false);
const { attributes, listeners, setNodeRef, transform, transition } = const {attributes, listeners, setNodeRef, transform, transition} =
useSortable({ id: position + 1 }); useSortable({id: position + 1});
const addCandidate = () => { const addCandidate = () => {
dispatch({ type: ElectionTypes.CANDIDATE_PUSH, value: 'default' }); if (!isCreated(election)) {
dispatch({type: ElectionTypes.CANDIDATE_PUSH, value: 'default'});
}
}; };
const toggleSet = () => setModalSet((m) => !m); const toggleSet = () => setModalSet((m) => !m);
@ -74,9 +76,8 @@ const CandidateField = ({
src={image} src={image}
width={24} width={24}
height={24} height={24}
className={`${ className={`${image == defaultAvatar ? 'default-avatar' : ''
image == defaultAvatar ? 'default-avatar' : '' } bg-primary`}
} bg-primary`}
alt={t('common.thumbnail')} alt={t('common.thumbnail')}
/> />
<div className="ps-2 fw-bold"> <div className="ps-2 fw-bold">

@ -51,7 +51,7 @@ export async function getServerSideProps({query, locale}) {
const numGrades = payload.grades.length; const numGrades = payload.grades.length;
const grades = payload.grades.map((g, i) => ({ const grades = payload.grades.map((g, i) => ({
...g, ...g,
color: getGradeColor(i, numGrades), color: getGradeColor(g.value, numGrades),
})); }));
const gradesByValue: {[key: number]: GradeResultInterface} = {}; const gradesByValue: {[key: number]: GradeResultInterface} = {};
grades.forEach((g) => (gradesByValue[g.value] = g)); grades.forEach((g) => (gradesByValue[g.value] = g));
@ -527,7 +527,6 @@ const ResultPage = ({
<div> <div>
<h5 className="text-white">{t('result.details')}</h5> <h5 className="text-white">{t('result.details')}</h5>
{Object.keys(candidateByRank) {Object.keys(candidateByRank)
.sort()
.map((rank, i) => { .map((rank, i) => {
return ( return (
<CandidateCard <CandidateCard

@ -9,9 +9,9 @@ import {
Dispatch, Dispatch,
SetStateAction, SetStateAction,
} from 'react'; } from 'react';
import { useRouter } from 'next/router'; import {useRouter} from 'next/router';
import { CandidateItem, GradeItem } from './type'; import {CandidateItem, GradeItem} from './type';
import { gradeColors } from '@services/grades'; import {gradeColors} from '@services/grades';
export interface ElectionContextInterface { export interface ElectionContextInterface {
name: string; name: string;
@ -38,7 +38,7 @@ const defaultCandidate: CandidateItem = {
const defaultElection: ElectionContextInterface = { const defaultElection: ElectionContextInterface = {
name: '', name: '',
description: '', description: '',
candidates: [{ ...defaultCandidate }, { ...defaultCandidate }], candidates: [{...defaultCandidate}, {...defaultCandidate}],
grades: [], grades: [],
randomOrder: true, randomOrder: true,
hideResults: false, hideResults: false,
@ -112,7 +112,7 @@ const ElectionContext = createContext<[ElectionContextInterface, DispatchType]>(
[defaultElection, () => {}] [defaultElection, () => {}]
); );
export function ElectionProvider({ children }) { export function ElectionProvider({children}) {
/** /**
* Provide the election and the dispatch to all children components * Provide the election and the dispatch to all children components
*/ */
@ -155,25 +155,25 @@ function electionReducer(
*/ */
switch (action.type) { switch (action.type) {
case ElectionTypes.RESET: { case ElectionTypes.RESET: {
return { ...action.value }; return {...action.value};
} }
case ElectionTypes.SET: { case ElectionTypes.SET: {
return { ...election, [action.field]: action.value }; return {...election, [action.field]: action.value};
} }
case ElectionTypes.CANDIDATE_PUSH: { case ElectionTypes.CANDIDATE_PUSH: {
if (typeof action.value === 'string' && action.value !== 'default') { if (typeof action.value === 'string' && action.value !== 'default') {
throw Error('Unexpected action'); throw Error('Unexpected action');
} }
const candidate = const candidate =
action.value === 'default' ? { ...defaultCandidate } : action.value; action.value === 'default' ? {...defaultCandidate} : action.value;
const candidates = [...election.candidates, candidate]; const candidates = [...election.candidates, candidate];
if (candidates.filter((c) => !c.active).length === 0) { if (candidates.filter((c) => !c.active).length === 0) {
return { return {
...election, ...election,
candidates: [...candidates, { ...defaultCandidate }], candidates: [...candidates, {...defaultCandidate}],
}; };
} else { } else {
return { ...election, candidates }; return {...election, candidates};
} }
} }
case ElectionTypes.CANDIDATE_RM: { case ElectionTypes.CANDIDATE_RM: {
@ -182,7 +182,7 @@ function electionReducer(
} }
const candidates = [...election.candidates]; const candidates = [...election.candidates];
candidates.splice(action.position, 1); candidates.splice(action.position, 1);
return { ...election, candidates }; return {...election, candidates};
} }
case ElectionTypes.CANDIDATE_SET: { case ElectionTypes.CANDIDATE_SET: {
if (typeof action.position !== 'number') { if (typeof action.position !== 'number') {
@ -198,14 +198,14 @@ function electionReducer(
if (candidates.filter((c) => !c.active).length === 0) { if (candidates.filter((c) => !c.active).length === 0) {
return { return {
...election, ...election,
candidates: [...candidates, { ...defaultCandidate }], candidates: [...candidates, {...defaultCandidate}],
}; };
} }
return { ...election, candidates }; return {...election, candidates};
} }
case ElectionTypes.GRADE_PUSH: { case ElectionTypes.GRADE_PUSH: {
const grades = [...election.grades, action.value]; const grades = [...election.grades, action.value];
return { ...election, grades }; return {...election, grades};
} }
case ElectionTypes.GRADE_RM: { case ElectionTypes.GRADE_RM: {
if (typeof action.position !== 'number') { if (typeof action.position !== 'number') {
@ -213,7 +213,7 @@ function electionReducer(
} }
const grades = [...election.grades]; const grades = [...election.grades];
grades.splice(action.position); grades.splice(action.position);
return { ...election, grades }; return {...election, grades};
} }
case ElectionTypes.GRADE_SET: { case ElectionTypes.GRADE_SET: {
if (typeof action.position !== 'number') { if (typeof action.position !== 'number') {
@ -222,11 +222,15 @@ function electionReducer(
const grades = [...election.grades]; const grades = [...election.grades];
const grade = grades[action.position]; const grade = grades[action.position];
grade[action.field] = action.value; 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) => { export const isClosed = (election: ElectionContextInterface) => {
const dateEnd = new Date(election.dateEnd); const dateEnd = new Date(election.dateEnd);
const now = new Date(); const now = new Date();

Loading…
Cancel
Save