From e691e974a638fa105027720553197cb193df5988 Mon Sep 17 00:00:00 2001 From: Pierre-Louis Guhur Date: Sun, 6 Nov 2022 17:33:32 +0100 Subject: [PATCH] fix: minor refactor --- components/admin/ElectionContext.tsx | 34 ++++++++++--- components/admin/Private.tsx | 12 ++--- services/api.ts | 74 ++++++++++++---------------- styles/scss/_app.scss | 2 +- 4 files changed, 64 insertions(+), 58 deletions(-) diff --git a/components/admin/ElectionContext.tsx b/components/admin/ElectionContext.tsx index 279bc0b..9556950 100644 --- a/components/admin/ElectionContext.tsx +++ b/components/admin/ElectionContext.tsx @@ -3,7 +3,6 @@ */ import { createContext, useContext, useReducer, useEffect } from 'react'; import { useRouter } from 'next/router'; -import { DEFAULT_GRADES } from '@services/constants'; // Store data about an election const ElectionContext = createContext(null); @@ -14,7 +13,7 @@ export function ElectionProvider({ children }) { /** * Provide the election and the dispatch to all children components */ - const [election, dispatch] = useReducer(electionReducer, initialElection); + const [election, dispatch] = useReducer(electionReducer, defaultElection); // At the initialization, set the title using GET param const router = useRouter(); @@ -51,7 +50,7 @@ export function useElectionDispatch() { return useContext(ElectionDispatchContext); } -function electionReducer(election, action) { +function electionReducer(election: Election, action) { /** * Manage all types of action doable on an election */ @@ -121,22 +120,43 @@ function electionReducer(election, action) { } } -const defaultCandidate = { +interface Candidate { + name: string; + description: string; + active: boolean; +} + +interface Grade { + name: string; + active: boolean; +} + +interface Election { + title: string; + description: string; + candidates: Array; + grades: Array; + isRandomOrder: boolean; + restrictResult: boolean; + restrictVote: boolean; + endVote: string; + emails: Array; +} + +const defaultCandidate: Candidate = { name: '', description: '', active: false, }; -const initialElection = { +const defaultElection: Election = { title: '', description: '', candidates: [{ ...defaultCandidate }, { ...defaultCandidate }], grades: [], - isTimeLimited: false, isRandomOrder: false, restrictResult: true, restrictVote: false, - startVote: null, endVote: null, emails: [], }; diff --git a/components/admin/Private.tsx b/components/admin/Private.tsx index 7c33fce..ac8d4af 100644 --- a/components/admin/Private.tsx +++ b/components/admin/Private.tsx @@ -62,14 +62,10 @@ const Private = () => { inputs={election.emails} validator={validateEmail} /> - - - - - - {t('admin.private-tip')} - - +
+ +
{t('admin.private-tip')}
+
) : null} diff --git a/services/api.ts b/services/api.ts index 40d6080..f1cca10 100644 --- a/services/api.ts +++ b/services/api.ts @@ -57,22 +57,20 @@ const sendInviteMail = (res) => { }; const createElection = ( - title, - candidates, - { - /** - * Create an election from its title, its candidates and a bunch of options - */ - mails, - numGrades, - start, - finish, - restrictResult, - locale, - }, - successCallback, - failureCallback + title: string, + candidates: Array, + description?: string, + mails?: Array, + numGrades?: number, + finish?: string, + restrictResult?: boolean, + locale?: string, + successCallback = null, + failureCallback = null ) => { + /** + * Create an election from its title, its candidates and a bunch of options + */ const endpoint = new URL(api.routesServer.setElection, api.urlServer); console.log(endpoint.href); @@ -89,7 +87,7 @@ const createElection = ( on_invitation_only: onInvitationOnly, num_grades: numGrades, elector_emails: mails || [], - start_at: start, + // start_at: start, finish_at: finish, select_language: locale || 'en', front_url: window.location.origin, @@ -202,33 +200,25 @@ export const INVITATION_ONLY_ERROR = 'E6:'; export const UNKNOWN_TOKEN_ERROR = 'E7:'; export const USED_TOKEN_ERROR = 'E8:'; export const WRONG_ELECTION_ERROR = 'E9:'; - -export const apiErrors = (error, t) => { - if (error.includes(UNKNOWN_ELECTION_ERROR)) { - return t('error.e1'); - } - if (error.includes(ONGOING_ELECTION_ERROR)) { - return t('error.e2'); - } - if (error.includes(NO_VOTE_ERROR)) { - return t('error.e3'); - } - if (error.includes(ELECTION_NOT_STARTED_ERROR)) { - return t('error.e4'); - } - if (error.includes(ELECTION_FINISHED_ERROR)) { - return t('error.e5'); - } - if (error.includes(INVITATION_ONLY_ERROR)) { - return t('error.e6'); - } - if (error.includes(USED_TOKEN_ERROR)) { - return t('error.e7'); - } - if (error.includes(WRONG_ELECTION_ERROR)) { - return t('error.e8'); +export const API_ERRORS = [ + UNKNOWN_TOKEN_ERROR, + ONGOING_ELECTION_ERROR, + NO_VOTE_ERROR, + ELECTION_NOT_STARTED_ERROR, + ELECTION_FINISHED_ERROR, + INVITATION_ONLY_ERROR, + UNKNOWN_TOKEN_ERROR, + USED_TOKEN_ERROR, + WRONG_ELECTION_ERROR, +]; + +export const apiErrors = (error: string): string => { + const errorCode = `${error.split(':')[0]}:`; + + if (API_ERRORS.includes(errorCode)) { + return `error.${error.split(':')[0].toLowerCase()}`; } else { - return t('error.catch22'); + return 'error.catch22'; } }; diff --git a/styles/scss/_app.scss b/styles/scss/_app.scss index 8ec1090..7f69114 100644 --- a/styles/scss/_app.scss +++ b/styles/scss/_app.scss @@ -378,7 +378,7 @@ ol.result > li { .text-muted { color: #8F88BA!important; - opacity: 0.5; + // opacity: 0.5; } .text-bg-light {