fix: minor refactor

pull/89/head
Pierre-Louis Guhur 1 year ago
parent a1c1d47a0e
commit e691e974a6

@ -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<Candidate>;
grades: Array<Grade>;
isRandomOrder: boolean;
restrictResult: boolean;
restrictVote: boolean;
endVote: string;
emails: Array<string>;
}
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: [],
};

@ -62,14 +62,10 @@ const Private = () => {
inputs={election.emails}
validator={validateEmail}
/>
<Row className="text-bg-light bt-3 p-2 text-muted fw-bold d-none d-md-block">
<Col className="col-auto">
<FontAwesomeIcon icon={faCircleInfo} />
</Col>
<Col className="col-auto d-flex align-items-center">
{t('admin.private-tip')}
</Col>
</Row>
<div className="bg-light bt-3 p-2 text-muted fw-bold d-none d-md-flex align-items-center ">
<FontAwesomeIcon icon={faCircleInfo} />
<div className="ms-3">{t('admin.private-tip')}</div>
</div>
</>
) : null}
</Container>

@ -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<string>,
description?: string,
mails?: Array<string>,
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';
}
};

@ -378,7 +378,7 @@ ol.result > li {
.text-muted {
color: #8F88BA!important;
opacity: 0.5;
// opacity: 0.5;
}
.text-bg-light {

Loading…
Cancel
Save