From 020f6c7b65fa575c2b19df5d217e40772a90d1e3 Mon Sep 17 00:00:00 2001 From: Pierre-Louis Guhur Date: Thu, 19 Jan 2023 18:14:33 +0100 Subject: [PATCH] fix: remove votes homepage --- pages/ballot/[pid]/[[...tid]].tsx | 191 -------------------- pages/votes/[pid]/[[...tid]].tsx | 290 +++++++++++++++++------------- 2 files changed, 170 insertions(+), 311 deletions(-) delete mode 100644 pages/ballot/[pid]/[[...tid]].tsx diff --git a/pages/ballot/[pid]/[[...tid]].tsx b/pages/ballot/[pid]/[[...tid]].tsx deleted file mode 100644 index eebc156..0000000 --- a/pages/ballot/[pid]/[[...tid]].tsx +++ /dev/null @@ -1,191 +0,0 @@ -import {useEffect, useState} from 'react'; -import Head from 'next/head'; -import {serverSideTranslations} from 'next-i18next/serverSideTranslations'; -import {useTranslation} from 'next-i18next'; -import {Container} from 'reactstrap'; -import {faCheck} from '@fortawesome/free-solid-svg-icons'; -import BallotDesktop from '@components/ballot/BallotDesktop'; -import Button from '@components/Button'; -import BallotMobile from '@components/ballot/BallotMobile'; -import Blur from '@components/Blur'; -import { - getElection, - getBallot, - castBallot, - ElectionPayload, - BallotPayload, - ErrorPayload, -} from '@services/api'; -import { - useBallot, - BallotTypes, - BallotProvider, -} from '@services/BallotContext'; -import {getUrl, RouteTypes} from '@services/routes'; -import {isEnded} from '@services/utils'; -import WaitingBallot from '@components/WaitingBallot'; -import PatternedBackground from '@components/PatternedBackground'; - -const shuffle = (array) => array.sort(() => Math.random() - 0.5); - -export async function getServerSideProps({query: {pid, tid}, locale}) { - if (!pid) { - return {notFound: true}; - } - const electionRef = pid.replaceAll('-', ''); - - const [election, ballot, translations] = await Promise.all([ - getElection(electionRef), - tid ? getBallot(tid) : null, - serverSideTranslations(locale, ['resource']), - ]); - - if ('message' in election) { - return {notFound: true}; - } - - if (isEnded(election.date_end)) { - return { - redirect: { - destination: getUrl(RouteTypes.ENDED_VOTE, electionRef), - permanent: false, - }, - }; - } - - if ( - !election || - !election.candidates || - !Array.isArray(election.candidates) - ) { - return {notFound: true}; - } - - const description = JSON.parse(election.description); - - if (description.randomOrder) { - shuffle(election.candidates); - } - - return { - props: { - ...translations, - election, - token: tid || null, - previousBallot: ballot || null, - }, - }; -} - -const ButtonSubmit = () => { - const {t} = useTranslation(); - - const [ballot, dispatch] = useBallot(); - const disabled = ballot.votes.length !== ballot.election.candidates.length; - return ( - - - - ); -}; - -interface VoteInterface { - election: ElectionPayload; - err: string; - token?: string; - previousBallot: BallotPayload -} -const VoteBallot = ({election, token, previousBallot}: VoteInterface) => { - const {t} = useTranslation(); - - const [ballot, dispatch] = useBallot(); - - const [voting, setVoting] = useState(false); - const [payload, setPayload] = useState(null); - const [error, setError] = useState(null); - - useEffect(() => { - dispatch({ - type: BallotTypes.ELECTION, - election: election, - }); - }, []); - - if (!ballot.election) { - return
"Loading..."
; - } - - const handleSubmit = async (event) => { - event.preventDefault(); - setVoting(true); - - try { - const res = await castBallot(ballot.votes, ballot.election, token); - if (res.status !== 200) { - console.error(res); - const msg = await res.json(); - setError(msg); - } else { - const msg = await res.json(); - setPayload(msg); - } - } catch (err) { - console.error(err); - setError(err.message); - } - }; - - if (voting) { - return ( - - - - ); - } - - return ( -
- - {election.name} - - - - - - -
- - - -
- - ); -}; - -const Ballot = (props) => { - return ( - - - - ); -}; - -export default Ballot; diff --git a/pages/votes/[pid]/[[...tid]].tsx b/pages/votes/[pid]/[[...tid]].tsx index 45258dd..eebc156 100644 --- a/pages/votes/[pid]/[[...tid]].tsx +++ b/pages/votes/[pid]/[[...tid]].tsx @@ -1,141 +1,191 @@ -import { useRouter } from 'next/router'; -import { serverSideTranslations } from 'next-i18next/serverSideTranslations'; -import { useTranslation } from 'next-i18next'; -import { Col, Container, Row } from 'reactstrap'; -import Link from 'next/link'; -import Share from '@components/Share'; +import {useEffect, useState} from 'react'; +import Head from 'next/head'; +import {serverSideTranslations} from 'next-i18next/serverSideTranslations'; +import {useTranslation} from 'next-i18next'; +import {Container} from 'reactstrap'; +import {faCheck} from '@fortawesome/free-solid-svg-icons'; +import BallotDesktop from '@components/ballot/BallotDesktop'; import Button from '@components/Button'; -import ExperienceRow from '@components/Experience'; -import AdvantagesRow from '@components/Advantages'; -import Logo from '@components/Logo'; -import { getUrl, RouteTypes } from '@services/routes'; -import { faArrowRight } from '@fortawesome/free-solid-svg-icons'; -import { displayRef, getLocaleShort } from '@services/utils'; -import { MAJORITY_JUDGMENT_LINK } from '@services/constants'; - -export async function getServerSideProps({ query: { pid, tid }, locale }) { +import BallotMobile from '@components/ballot/BallotMobile'; +import Blur from '@components/Blur'; +import { + getElection, + getBallot, + castBallot, + ElectionPayload, + BallotPayload, + ErrorPayload, +} from '@services/api'; +import { + useBallot, + BallotTypes, + BallotProvider, +} from '@services/BallotContext'; +import {getUrl, RouteTypes} from '@services/routes'; +import {isEnded} from '@services/utils'; +import WaitingBallot from '@components/WaitingBallot'; +import PatternedBackground from '@components/PatternedBackground'; + +const shuffle = (array) => array.sort(() => Math.random() - 0.5); + +export async function getServerSideProps({query: {pid, tid}, locale}) { + if (!pid) { + return {notFound: true}; + } + const electionRef = pid.replaceAll('-', ''); + + const [election, ballot, translations] = await Promise.all([ + getElection(electionRef), + tid ? getBallot(tid) : null, + serverSideTranslations(locale, ['resource']), + ]); + + if ('message' in election) { + return {notFound: true}; + } + + if (isEnded(election.date_end)) { + return { + redirect: { + destination: getUrl(RouteTypes.ENDED_VOTE, electionRef), + permanent: false, + }, + }; + } + + if ( + !election || + !election.candidates || + !Array.isArray(election.candidates) + ) { + return {notFound: true}; + } + + const description = JSON.parse(election.description); + + if (description.randomOrder) { + shuffle(election.candidates); + } + return { props: { - ...(await serverSideTranslations(locale, ['resource'])), - electionRef: pid.replaceAll('-', ''), + ...translations, + election, token: tid || null, + previousBallot: ballot || null, }, }; } +const ButtonSubmit = () => { + const {t} = useTranslation(); + + const [ballot, dispatch] = useBallot(); + const disabled = ballot.votes.length !== ballot.election.candidates.length; + return ( + + + + ); +}; + interface VoteInterface { - electionRef: string; + election: ElectionPayload; + err: string; token?: string; + previousBallot: BallotPayload } +const VoteBallot = ({election, token, previousBallot}: VoteInterface) => { + const {t} = useTranslation(); -const GoToBallotConfirmDesktop = ({ electionRef, token }) => { - const { t } = useTranslation(); - const router = useRouter(); + const [ballot, dispatch] = useBallot(); - return ( -
- - - - -

{t('common.welcome')}

-
- -

{t('vote.home-desc')}

-
- -
- - - -
- -

{t('home.noAds')}

-
- - - - - - -
-
- ); -}; -const GoToBallotConfirmMobile = ({ electionRef, token }) => { - const { t } = useTranslation(); - const router = useRouter(); + const [voting, setVoting] = useState(false); + const [payload, setPayload] = useState(null); + const [error, setError] = useState(null); + + useEffect(() => { + dispatch({ + type: BallotTypes.ELECTION, + election: election, + }); + }, []); + + if (!ballot.election) { + return
"Loading..."
; + } + + const handleSubmit = async (event) => { + event.preventDefault(); + setVoting(true); + + try { + const res = await castBallot(ballot.votes, ballot.election, token); + if (res.status !== 200) { + console.error(res); + const msg = await res.json(); + setError(msg); + } else { + const msg = await res.json(); + setPayload(msg); + } + } catch (err) { + console.error(err); + setError(err.message); + } + }; + + if (voting) { + return ( + + + + ); + } return ( -
- - - -

{t('common.welcome')}

-
- -

{t('vote.home-desc')}

-
- -
- - - -
- -

{t('home.noAds')}

-
- - - - - - -
+
+ + {election.name} + + + + + + +
+ + + +
+ ); }; -const Vote = ({ electionRef, token }: VoteInterface) => { +const Ballot = (props) => { return ( - <> -
- - -
-
-
- -
- - -
- + + + ); }; -export default Vote; + +export default Ballot;