From 64b295f0fcd1f6a2a491fffddaceff055e36b66d Mon Sep 17 00:00:00 2001 From: Pierre-Louis Guhur Date: Wed, 18 Jan 2023 13:50:45 +0100 Subject: [PATCH] fix: error message hide-result election --- components/admin/AccessResults.tsx | 14 ++++++------ components/admin/LimitDate.tsx | 14 ++++++------ pages/results/[pid]/[[...tid]].tsx | 36 +++++++++++++++++++++++++----- public/locales/en/resource.json | 3 ++- public/locales/fr/resource.json | 1 + services/api.ts | 24 ++++++++++---------- 6 files changed, 60 insertions(+), 32 deletions(-) diff --git a/components/admin/AccessResults.tsx b/components/admin/AccessResults.tsx index ec685cb..bc5d94f 100644 --- a/components/admin/AccessResults.tsx +++ b/components/admin/AccessResults.tsx @@ -1,10 +1,10 @@ -import { useTranslation } from 'next-i18next'; -import { ElectionTypes, useElection } from '@services/ElectionContext'; -import { Container } from 'reactstrap'; +import {useTranslation} from 'next-i18next'; +import {ElectionTypes, useElection} from '@services/ElectionContext'; +import {Container} from 'reactstrap'; import Switch from '@components/Switch'; const AccessResults = () => { - const { t } = useTranslation(); + const {t} = useTranslation(); const [election, dispatch] = useElection(); const toggle = () => { @@ -17,13 +17,13 @@ const AccessResults = () => { return ( -
-
+
+
{t('admin.access-results')}
{election.hideResults ? ( -

+

{t('admin.access-results-desc')}

) : null} diff --git a/components/admin/LimitDate.tsx b/components/admin/LimitDate.tsx index 6d92a0e..d2e2580 100644 --- a/components/admin/LimitDate.tsx +++ b/components/admin/LimitDate.tsx @@ -1,12 +1,12 @@ -import { useState } from 'react'; -import { useTranslation } from 'next-i18next'; -import { Container, Row, Col } from 'reactstrap'; +import {useState} from 'react'; +import {useTranslation} from 'next-i18next'; +import {Container, Row, Col} from 'reactstrap'; import DatePicker from '@components/DatePicker'; import Switch from '@components/Switch'; -import { ElectionTypes, useElection } from '@services/ElectionContext'; +import {ElectionTypes, useElection} from '@services/ElectionContext'; const LimitDate = () => { - const { t } = useTranslation(); + const {t} = useTranslation(); const defaultEndDate = new Date(); defaultEndDate.setUTCDate(defaultEndDate.getUTCDate() + 15); const [endDate, setEndDate] = useState(defaultEndDate); @@ -30,8 +30,8 @@ const LimitDate = () => { return (
-
-
+
+
{t('admin.limit-duration')} {hasDate ? ( <> diff --git a/pages/results/[pid]/[[...tid]].tsx b/pages/results/[pid]/[[...tid]].tsx index 94ed0dd..8fa22ef 100644 --- a/pages/results/[pid]/[[...tid]].tsx +++ b/pages/results/[pid]/[[...tid]].tsx @@ -44,8 +44,8 @@ export async function getServerSideProps({query, locale}) { serverSideTranslations(locale, ['resource']), ]); - if ('msg' in payload) { - return {props: {err: {message: payload.msg}, electionRef, ...translations}}; + if ('message' in payload) { + return {props: {err: payload, electionRef, ...translations}}; } const numGrades = payload.grades.length; @@ -458,6 +458,7 @@ const Podium = ({candidates}: PodiumInterface) => { interface ErrorInterface { message: string; + details?: string; } interface ResultPageInterface { result?: ResultInterface; @@ -483,9 +484,34 @@ const ResultPage = ({ <>

{t('result.no-votes')}

- +
+ +
+ + + } + + ); + } + console.log(err) + + if (err && err.details.startsWith('The election is not closed')) { + const urlVote = getUrl(RouteTypes.VOTE, router, electionRef, token); + return ( + + { + <> +

{t('result.hide-results')}

+ +
+ +
} diff --git a/public/locales/en/resource.json b/public/locales/en/resource.json index 7e22f42..4ebc551 100644 --- a/public/locales/en/resource.json +++ b/public/locales/en/resource.json @@ -172,10 +172,11 @@ "admin.error": "Unable to load the election.", "result.download": "Download results", "result.go-to-admin": "Manage the election", - "result.go-to-vote": "Would you go to the voting page?", + "result.go-to-vote": "Do you want to go to the voting page?", "result.no-votes": "No vote has been recorded yet.", "result.merit-profile": "Merit profile of the candidate", "result.has-closed": "Closed since", + "result.hide-results": "Results are hidden until the election is closed", "result.opened": "The election is opened", "result.closed": "The election is closed", "result.result": "Results of the election", diff --git a/public/locales/fr/resource.json b/public/locales/fr/resource.json index 4ca7cd9..ca19352 100644 --- a/public/locales/fr/resource.json +++ b/public/locales/fr/resource.json @@ -176,6 +176,7 @@ "result.go-to-vote": "Souhaitez-vous accéder au vote ?", "result.no-votes": "Aucun vote n'a encore été enregistré.", "result.has-closed": "Terminée depuis", + "result.hide-results": "Les résults sont masqués jusqu'à la fermeture de l'élection.", "result.closed": "L'élection est terminée", "result.opened": "L'élection est en cours", "result.result": "Résultat du vote", diff --git a/services/api.ts b/services/api.ts index 7a1ecce..756e5b7 100644 --- a/services/api.ts +++ b/services/api.ts @@ -37,7 +37,7 @@ export interface ErrorPayload { export interface HTTPPayload { status: number; - msg: string; + message: string; } export interface ElectionPayload { @@ -186,22 +186,22 @@ export const updateElection = async ( }); if (!req.ok || req.status !== 200) { const payload = await req.json(); - return {status: req.status, msg: payload}; + return {status: req.status, ...payload}; } const payload = await req.json(); return {status: 200, ...payload}; } catch (e) { console.error(e); - return {status: 400, msg: `Unknown API error: ${e}`}; + return {status: 400, message: `Unknown API error: ${e}`}; } }; +/** + * Fetch results from external API + */ export const getResults = async ( pid: string ): Promise => { - /** - * Fetch results from external API - */ const endpoint = new URL( api.routesServer.getResults.replace(new RegExp(':slug', 'g'), pid), @@ -212,13 +212,14 @@ export const getResults = async ( const response = await fetch(endpoint.href); if (response.status != 200) { const payload = await response.json(); - return {status: response.status, msg: payload}; + console.log("PAYLOAD", payload) + return {status: response.status, ...payload}; } const payload = await response.json(); return {...payload, status: response.status}; } catch (error) { console.error(error); - return {status: 400, msg: `Unknown API error: ${error}`}; + return {status: 400, message: `Unknown API error: ${error}`}; } }; @@ -239,12 +240,12 @@ export const getElection = async ( if (response.status != 200) { const payload = await response.json(); - return {status: response.status, msg: payload}; + return {status: response.status, message: payload}; } const payload = await response.json(); return {...payload, status: response.status}; } catch (error) { - return {status: 400, msg: 'Unknown API error'}; + return {status: 400, message: 'Unknown API error'}; } }; @@ -270,7 +271,6 @@ export const getBallot = async ( }); if (response.status != 200) { - console.log("STATUS", await response.text()) return null; } @@ -278,7 +278,7 @@ export const getBallot = async ( return {...payload, status: response.status}; } catch (error) { - return {status: 400, msg: 'Unknown API error'}; + return {status: 400, message: 'Unknown API error'}; } };