From de08bfa8ef4c45420ff201a47fcb9e5aa61fdf48 Mon Sep 17 00:00:00 2001 From: Pierre-Louis Guhur Date: Tue, 14 Mar 2023 10:27:14 +0100 Subject: [PATCH] fix: admin link to vote --- components/admin/TitleModal.tsx | 5 +--- pages/admin/[pid]/[tid].tsx | 23 ++++++++------- pages/errors/restricted.tsx | 48 ++++++++++++++++++++++++++++++++ pages/votes/[pid]/[[...tid]].tsx | 7 +++++ public/locales/en/resource.json | 2 ++ public/locales/fr/resource.json | 2 ++ services/routes.ts | 5 ++-- 7 files changed, 74 insertions(+), 18 deletions(-) create mode 100644 pages/errors/restricted.tsx diff --git a/components/admin/TitleModal.tsx b/components/admin/TitleModal.tsx index efae8ce..5ef82ac 100644 --- a/components/admin/TitleModal.tsx +++ b/components/admin/TitleModal.tsx @@ -1,16 +1,13 @@ -import {useState, useEffect, useRef, KeyboardEvent, ChangeEvent} from 'react'; +import {useState, useEffect, useRef, KeyboardEvent} from 'react'; import {Label, Modal, ModalBody, Form} from 'reactstrap'; import {faPlus, faArrowLeft} from '@fortawesome/free-solid-svg-icons'; import {useTranslation} from 'next-i18next'; import {ElectionTypes, useElection} from '@services/ElectionContext'; import Button from '@components/Button'; -import {checkName} from '@services/ElectionContext'; import {AppTypes, useAppContext} from '@services/context'; -import {useRouter} from 'next/router'; const TitleModal = ({isOpen, toggle}) => { const {t} = useTranslation(); - const router = useRouter(); const [election, dispatch] = useElection(); const [_, dispatchApp] = useAppContext(); const [name, setName] = useState(election.name); diff --git a/pages/admin/[pid]/[tid].tsx b/pages/admin/[pid]/[tid].tsx index 4d8b8c9..848bfbb 100644 --- a/pages/admin/[pid]/[tid].tsx +++ b/pages/admin/[pid]/[tid].tsx @@ -150,17 +150,16 @@ const HeaderRubbonDesktop = ({handleClosing, handleSubmit, waiting}) => {
{t('admin.admin-title')}
- - - + {!election.restricted && !isClosed(election) && ( )}
- + ); }; diff --git a/pages/errors/restricted.tsx b/pages/errors/restricted.tsx new file mode 100644 index 0000000..1b79f33 --- /dev/null +++ b/pages/errors/restricted.tsx @@ -0,0 +1,48 @@ +import {serverSideTranslations} from 'next-i18next/serverSideTranslations'; +import {useTranslation} from 'next-i18next'; +import {Container} from 'reactstrap'; +import {faEnvelope} from '@fortawesome/free-solid-svg-icons'; +import Button from '@components/Button'; +import {CONTACT_MAIL} from '@services/constants'; + + +export async function getServerSideProps({locale}) { + const translations = await serverSideTranslations(locale, ['resource']) + + return { + props: { + ...translations, + }, + }; +} + +const RestrictedVote = () => { + const {t} = useTranslation(); + + return ( +
+ +

{t('error.restricted-election-title')}

+

{t('error.restricted-election-desc')}

+ + + + +
+
+ ); +}; + + +export default RestrictedVote; + diff --git a/pages/votes/[pid]/[[...tid]].tsx b/pages/votes/[pid]/[[...tid]].tsx index eebc156..b5f7269 100644 --- a/pages/votes/[pid]/[[...tid]].tsx +++ b/pages/votes/[pid]/[[...tid]].tsx @@ -25,6 +25,7 @@ import {getUrl, RouteTypes} from '@services/routes'; import {isEnded} from '@services/utils'; import WaitingBallot from '@components/WaitingBallot'; import PatternedBackground from '@components/PatternedBackground'; +import {useRouter} from 'next/router'; const shuffle = (array) => array.sort(() => Math.random() - 0.5); @@ -109,6 +110,7 @@ const VoteBallot = ({election, token, previousBallot}: VoteInterface) => { const {t} = useTranslation(); const [ballot, dispatch] = useBallot(); + const router = useRouter(); const [voting, setVoting] = useState(false); const [payload, setPayload] = useState(null); @@ -145,6 +147,11 @@ const VoteBallot = ({election, token, previousBallot}: VoteInterface) => { } }; + if (election.restricted) { + const url = getUrl(RouteTypes.RESTRICTED_VOTE, router) + router.push(url); + } + if (voting) { return ( diff --git a/public/locales/en/resource.json b/public/locales/en/resource.json index b800ccb..d1457a3 100644 --- a/public/locales/en/resource.json +++ b/public/locales/en/resource.json @@ -64,6 +64,8 @@ "error.not-enough-grades": "Not enough grades", "error.not-enough-candidates": "Not enough candidates", "error.uncorrect-name": "The title is incorrect", + "error.restricted-election-title": "The election is restricted", + "error.restricted-election-desc": "You can only vote if you have been invited.", "faq.title": "Frequently asked questions", "faq.sec-1-title": "What is majority judgment?", "faq.sec-1-desc": "A simple and intuitive principle, which changes everything: the voter votes by giving his/her opinion on all the candidates, by attributing to each one a mention on a scale of values (e.g. Very good, Good, Fair, Insufficient, To be rejected). The candidate with the highest majority rating wins the election (the one with \"the highest majority\" rating).", diff --git a/public/locales/fr/resource.json b/public/locales/fr/resource.json index d1b29ed..788e13e 100644 --- a/public/locales/fr/resource.json +++ b/public/locales/fr/resource.json @@ -66,6 +66,8 @@ "error.uncorrect-name": "The title is incorrect", "error.not-enough-grades": "Il manque des mentions", "error.not-enough-candidates": "Il manque des candidats", + "error.restricted-election-title": "L'élection est restreinte", + "error.restricted-election-desc": "Vous ne pouvez accéder à cette élection que par une invitation.", "faq.title": "Foire aux questions", "faq.sec-1-title": "Qu'est-ce que le jugement majoritaire ?", "faq.sec-1-desc": "Un principe simple et intuitif, qui change tout : l’électeur vote en donnant son avis sur tous les candidats, en leur attribuant à chacun une mention sur une échelle de valeurs (par exemple. Très bien, Bien, Assez bien, Passable, Insuffisant, À Rejeter). Le candidat le mieux évalué par une majorité remporte l’élection (celui qui obtient la meilleure mention « majoritaire »).", diff --git a/services/routes.ts b/services/routes.ts index a67446e..c31d32a 100644 --- a/services/routes.ts +++ b/services/routes.ts @@ -1,8 +1,8 @@ /** * This file provides the paths to the pages */ -import { NextRouter } from 'next/router'; -import { getWindowUrl, displayRef, getLocaleShort } from './utils'; +import {NextRouter} from 'next/router'; +import {getWindowUrl, displayRef, getLocaleShort} from './utils'; export enum RouteTypes { ADMIN = 'admin', @@ -13,6 +13,7 @@ export enum RouteTypes { HOME = '', RESULTS = 'results', VOTE = 'votes', + RESTRICTED_VOTE = 'errors/restricted', } export const getUrl = (