parent
af5baf0775
commit
020f6c7b65
@ -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 (
|
|
||||||
<Container className="my-5 d-md-flex d-grid justify-content-md-center">
|
|
||||||
<Button
|
|
||||||
outline={true}
|
|
||||||
color="secondary"
|
|
||||||
className="bg-blue"
|
|
||||||
role="submit"
|
|
||||||
disabled={disabled}
|
|
||||||
icon={faCheck}
|
|
||||||
position="left"
|
|
||||||
>
|
|
||||||
{t('vote.submit')}
|
|
||||||
</Button>
|
|
||||||
</Container>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
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<BallotPayload | null>(null);
|
|
||||||
const [error, setError] = useState<ErrorPayload | null>(null);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
dispatch({
|
|
||||||
type: BallotTypes.ELECTION,
|
|
||||||
election: election,
|
|
||||||
});
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
if (!ballot.election) {
|
|
||||||
return <div>"Loading..."</div>;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 (
|
|
||||||
<PatternedBackground>
|
|
||||||
<WaitingBallot ballot={payload} error={error} />
|
|
||||||
</PatternedBackground>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<form
|
|
||||||
className="w-100 flex-fill d-flex align-items-center"
|
|
||||||
onSubmit={handleSubmit}
|
|
||||||
autoComplete="off"
|
|
||||||
>
|
|
||||||
<Head>
|
|
||||||
<title>{election.name}</title>
|
|
||||||
|
|
||||||
<meta key="og:title" property="og:title" content={election.name} />
|
|
||||||
<meta
|
|
||||||
property="og:description"
|
|
||||||
key="og:description"
|
|
||||||
content={t('common.application')}
|
|
||||||
/>
|
|
||||||
</Head>
|
|
||||||
|
|
||||||
<Blur />
|
|
||||||
<div className="w-100 h-100 d-flex flex-column justify-content-center">
|
|
||||||
<BallotDesktop hasVoted={previousBallot != null} />
|
|
||||||
<BallotMobile hasVoted={previousBallot != null} />
|
|
||||||
<ButtonSubmit />
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const Ballot = (props) => {
|
|
||||||
return (
|
|
||||||
<BallotProvider>
|
|
||||||
<VoteBallot {...props} />
|
|
||||||
</BallotProvider>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default Ballot;
|
|
@ -1,141 +1,191 @@
|
|||||||
import { useRouter } from 'next/router';
|
import {useEffect, useState} from 'react';
|
||||||
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
|
import Head from 'next/head';
|
||||||
import { useTranslation } from 'next-i18next';
|
import {serverSideTranslations} from 'next-i18next/serverSideTranslations';
|
||||||
import { Col, Container, Row } from 'reactstrap';
|
import {useTranslation} from 'next-i18next';
|
||||||
import Link from 'next/link';
|
import {Container} from 'reactstrap';
|
||||||
import Share from '@components/Share';
|
import {faCheck} from '@fortawesome/free-solid-svg-icons';
|
||||||
|
import BallotDesktop from '@components/ballot/BallotDesktop';
|
||||||
import Button from '@components/Button';
|
import Button from '@components/Button';
|
||||||
import ExperienceRow from '@components/Experience';
|
import BallotMobile from '@components/ballot/BallotMobile';
|
||||||
import AdvantagesRow from '@components/Advantages';
|
import Blur from '@components/Blur';
|
||||||
import Logo from '@components/Logo';
|
import {
|
||||||
import { getUrl, RouteTypes } from '@services/routes';
|
getElection,
|
||||||
import { faArrowRight } from '@fortawesome/free-solid-svg-icons';
|
getBallot,
|
||||||
import { displayRef, getLocaleShort } from '@services/utils';
|
castBallot,
|
||||||
import { MAJORITY_JUDGMENT_LINK } from '@services/constants';
|
ElectionPayload,
|
||||||
|
BallotPayload,
|
||||||
export async function getServerSideProps({ query: { pid, tid }, locale }) {
|
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 {
|
return {
|
||||||
props: {
|
props: {
|
||||||
...(await serverSideTranslations(locale, ['resource'])),
|
...translations,
|
||||||
electionRef: pid.replaceAll('-', ''),
|
election,
|
||||||
token: tid || null,
|
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 (
|
||||||
|
<Container className="my-5 d-md-flex d-grid justify-content-md-center">
|
||||||
|
<Button
|
||||||
|
outline={true}
|
||||||
|
color="secondary"
|
||||||
|
className="bg-blue"
|
||||||
|
role="submit"
|
||||||
|
disabled={disabled}
|
||||||
|
icon={faCheck}
|
||||||
|
position="left"
|
||||||
|
>
|
||||||
|
{t('vote.submit')}
|
||||||
|
</Button>
|
||||||
|
</Container>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
interface VoteInterface {
|
interface VoteInterface {
|
||||||
electionRef: string;
|
election: ElectionPayload;
|
||||||
|
err: string;
|
||||||
token?: string;
|
token?: string;
|
||||||
|
previousBallot: BallotPayload
|
||||||
}
|
}
|
||||||
|
const VoteBallot = ({election, token, previousBallot}: VoteInterface) => {
|
||||||
|
const {t} = useTranslation();
|
||||||
|
|
||||||
const GoToBallotConfirmDesktop = ({ electionRef, token }) => {
|
const [ballot, dispatch] = useBallot();
|
||||||
const { t } = useTranslation();
|
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
return (
|
const [voting, setVoting] = useState(false);
|
||||||
<div className="sectionOneHomeForm d-none d-md-block">
|
const [payload, setPayload] = useState<BallotPayload | null>(null);
|
||||||
<Row className="sectionOneHomeRowOne">
|
const [error, setError] = useState<ErrorPayload | null>(null);
|
||||||
<Col className="sectionOneHomeContent">
|
|
||||||
<Logo height="128" />
|
useEffect(() => {
|
||||||
<Row>
|
dispatch({
|
||||||
<h2 className="mb-4 mt-5">{t('common.welcome')}</h2>
|
type: BallotTypes.ELECTION,
|
||||||
</Row>
|
election: election,
|
||||||
<Row>
|
});
|
||||||
<h4 className="mb-5">{t('vote.home-desc')}</h4>
|
}, []);
|
||||||
</Row>
|
|
||||||
|
if (!ballot.election) {
|
||||||
<div className="w-100">
|
return <div>"Loading..."</div>;
|
||||||
<Link
|
}
|
||||||
className="d-grid d-md-block w-100"
|
|
||||||
href={getUrl(RouteTypes.BALLOT, router, electionRef, token)}
|
const handleSubmit = async (event) => {
|
||||||
>
|
event.preventDefault();
|
||||||
<Button
|
setVoting(true);
|
||||||
color="secondary"
|
|
||||||
outline={true}
|
try {
|
||||||
type="submit"
|
const res = await castBallot(ballot.votes, ballot.election, token);
|
||||||
icon={faArrowRight}
|
if (res.status !== 200) {
|
||||||
position="right"
|
console.error(res);
|
||||||
>
|
const msg = await res.json();
|
||||||
{t('vote.home-start')}
|
setError(msg);
|
||||||
</Button>
|
} else {
|
||||||
</Link>
|
const msg = await res.json();
|
||||||
</div>
|
setPayload(msg);
|
||||||
<Row className="noAds my-0">
|
}
|
||||||
<p>{t('home.noAds')}</p>
|
} catch (err) {
|
||||||
</Row>
|
console.error(err);
|
||||||
<Row>
|
setError(err.message);
|
||||||
<Link href={MAJORITY_JUDGMENT_LINK}>
|
}
|
||||||
<Button color="black" outline={true} className="mt-2 mb-5">
|
};
|
||||||
{t('common.about-mj')}
|
|
||||||
</Button>
|
if (voting) {
|
||||||
</Link>
|
return (
|
||||||
</Row>
|
<PatternedBackground>
|
||||||
</Col>
|
<WaitingBallot ballot={payload} error={error} />
|
||||||
</Row>
|
</PatternedBackground>
|
||||||
</div>
|
);
|
||||||
);
|
}
|
||||||
};
|
|
||||||
const GoToBallotConfirmMobile = ({ electionRef, token }) => {
|
|
||||||
const { t } = useTranslation();
|
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="d-block d-md-none bg-primary py-5 px-3 min-vh-100 d-flex d-md-none flex-column align-items-center justify-content-between">
|
<form
|
||||||
<Col className="sectionOneHomeContent">
|
className="w-100 flex-fill d-flex align-items-center"
|
||||||
<Logo width={164} />
|
onSubmit={handleSubmit}
|
||||||
<Row>
|
autoComplete="off"
|
||||||
<h2 className="mb-4 mt-5">{t('common.welcome')}</h2>
|
>
|
||||||
</Row>
|
<Head>
|
||||||
<Row>
|
<title>{election.name}</title>
|
||||||
<h4 className="mb-5">{t('vote.home-desc')}</h4>
|
|
||||||
</Row>
|
<meta key="og:title" property="og:title" content={election.name} />
|
||||||
|
<meta
|
||||||
<div className="w-100">
|
property="og:description"
|
||||||
<Link
|
key="og:description"
|
||||||
className="d-grid d-md-block w-100"
|
content={t('common.application')}
|
||||||
href={getUrl(RouteTypes.BALLOT, router, electionRef, token)}
|
/>
|
||||||
>
|
</Head>
|
||||||
<Button
|
|
||||||
color="secondary"
|
<Blur />
|
||||||
outline={true}
|
<div className="w-100 h-100 d-flex flex-column justify-content-center">
|
||||||
type="submit"
|
<BallotDesktop hasVoted={previousBallot != null} />
|
||||||
icon={faArrowRight}
|
<BallotMobile hasVoted={previousBallot != null} />
|
||||||
position="right"
|
<ButtonSubmit />
|
||||||
>
|
</div>
|
||||||
{t('vote.home-start')}
|
</form>
|
||||||
</Button>
|
|
||||||
</Link>
|
|
||||||
</div>
|
|
||||||
<Row className="noAds my-0">
|
|
||||||
<p>{t('home.noAds')}</p>
|
|
||||||
</Row>
|
|
||||||
<Row>
|
|
||||||
<Link href={MAJORITY_JUDGMENT_LINK}>
|
|
||||||
<Button className="btn-black mt-2 mb-5">
|
|
||||||
{t('common.about-mj')}
|
|
||||||
</Button>
|
|
||||||
</Link>
|
|
||||||
</Row>
|
|
||||||
</Col>
|
|
||||||
</div>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const Vote = ({ electionRef, token }: VoteInterface) => {
|
const Ballot = (props) => {
|
||||||
return (
|
return (
|
||||||
<>
|
<BallotProvider>
|
||||||
<section>
|
<VoteBallot {...props} />
|
||||||
<GoToBallotConfirmDesktop electionRef={electionRef} token={token} />
|
</BallotProvider>
|
||||||
<GoToBallotConfirmMobile electionRef={electionRef} token={token} />
|
|
||||||
</section>
|
|
||||||
<section className="sectionTwoHome p-2 pb-5 text-center">
|
|
||||||
<div className=" pt-5 mt-5">
|
|
||||||
<AdvantagesRow />
|
|
||||||
</div>
|
|
||||||
<ExperienceRow />
|
|
||||||
<Share />
|
|
||||||
</section>
|
|
||||||
</>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
export default Vote;
|
|
||||||
|
export default Ballot;
|
||||||
|
Loading…
Reference in new issue