diff --git a/components/Logo.tsx b/components/Logo.tsx index b34c2d7..32fdfcf 100644 --- a/components/Logo.tsx +++ b/components/Logo.tsx @@ -1,22 +1,24 @@ import Image from 'next/image'; import Link from 'next/link'; -import { useTranslation } from 'next-i18next'; -import { getUrl, RouteTypes } from '@services/routes'; +import {useTranslation} from 'next-i18next'; +import {getUrl, RouteTypes} from '@services/routes'; import logoWithText from '../public/logos/logo.svg'; import logo from '../public/logos/logo-footer.svg'; -import { useRouter } from 'next/router'; +import {useRouter} from 'next/router'; +import {getLocaleShort} from '@services/utils'; interface LogoProps { title?: boolean; [props: string]: any; } -const Logo = ({ title = true, ...props }: LogoProps) => { - const { t } = useTranslation(); +const Logo = ({title = true, ...props}: LogoProps) => { + const {t} = useTranslation(); const router = useRouter(); + const locale = getLocaleShort(router); const src = title ? logoWithText : logo; return ( - + {t('logo.alt')} ); diff --git a/components/WaitingBallot.tsx b/components/WaitingBallot.tsx index 64cff9c..dc56a07 100644 --- a/components/WaitingBallot.tsx +++ b/components/WaitingBallot.tsx @@ -1,36 +1,37 @@ import Image from 'next/image'; -import { useTranslation } from 'next-i18next'; -import { CSSProperties, useEffect, useState } from 'react'; -import { faArrowRight } from '@fortawesome/free-solid-svg-icons'; -import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; -import { Col, Container, Row } from 'reactstrap'; +import {useTranslation} from 'next-i18next'; +import {CSSProperties, useEffect, useState} from 'react'; +import {faArrowRight} from '@fortawesome/free-solid-svg-icons'; +import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'; +import {Col, Container, Row} from 'reactstrap'; import Button from '@components/Button'; import Share from '@components/Share'; import ErrorMessage from '@components/Error'; -import { BallotPayload, ErrorPayload } from '@services/api'; -import { AppTypes, useAppContext } from '@services/context'; -import { displayRef, isEnded } from '@services/utils'; -import { getUrl, RouteTypes } from '@services/routes'; +import {BallotPayload, ErrorPayload} from '@services/api'; +import {AppTypes, useAppContext} from '@services/context'; +import {displayRef, getLocaleShort, isEnded} from '@services/utils'; +import {getUrl, RouteTypes} from '@services/routes'; import Logo from './Logo'; -import { FORM_FEEDBACK, MAJORITY_JUDGMENT_LINK } from '@services/constants'; +import {FORM_FEEDBACK, MAJORITY_JUDGMENT_LINK} from '@services/constants'; import urne from '../public/urne.svg'; import star from '../public/star.svg'; import logo from '../public/logo-red-blue.svg'; import Link from 'next/link'; -import { useRouter } from 'next/router'; +import {useRouter} from 'next/router'; export interface WaitingBallotInterface { ballot?: BallotPayload; error?: ErrorPayload; } -const ButtonResults = ({ election }) => { - const { t } = useTranslation(); +const ButtonResults = ({election}) => { + const {t} = useTranslation(); const router = useRouter(); + const locale = getLocaleShort(router); if (!election.hideResults || isEnded(election.date_end)) { return ( - +
{!election.restricted && !isClosed(election) && ( - + @@ -315,7 +317,7 @@ const TitleBanner = ({name, electionRef, token}: TitleBannerInterface) => {
{token ? (
- + @@ -475,9 +477,10 @@ const ResultPage = ({ }: ResultPageInterface) => { const {t} = useTranslation(); const router = useRouter(); + const locale = getLocaleShort(router); if (err && err.message.startsWith('No votes')) { - const urlVote = getUrl(RouteTypes.VOTE, router, electionRef, token); + const urlVote = getUrl(RouteTypes.VOTE, locale, electionRef, token); return ( { @@ -499,7 +502,7 @@ const ResultPage = ({ } if (err && err.details.startsWith('The election is not closed')) { - const urlVote = getUrl(RouteTypes.VOTE, router, electionRef, token); + const urlVote = getUrl(RouteTypes.VOTE, locale, electionRef, token); return ( { diff --git a/pages/votes/[pid]/[[...tid]].tsx b/pages/votes/[pid]/[[...tid]].tsx index b5f7269..e6468e8 100644 --- a/pages/votes/[pid]/[[...tid]].tsx +++ b/pages/votes/[pid]/[[...tid]].tsx @@ -22,7 +22,7 @@ import { BallotProvider, } from '@services/BallotContext'; import {getUrl, RouteTypes} from '@services/routes'; -import {isEnded} from '@services/utils'; +import {getLocaleShort, isEnded} from '@services/utils'; import WaitingBallot from '@components/WaitingBallot'; import PatternedBackground from '@components/PatternedBackground'; import {useRouter} from 'next/router'; @@ -46,9 +46,10 @@ export async function getServerSideProps({query: {pid, tid}, locale}) { } if (isEnded(election.date_end)) { + const url = getUrl(RouteTypes.ENDED_VOTE, locale, electionRef) return { redirect: { - destination: getUrl(RouteTypes.ENDED_VOTE, electionRef), + destination: url.toString(), permanent: false, }, }; @@ -148,7 +149,8 @@ const VoteBallot = ({election, token, previousBallot}: VoteInterface) => { }; if (election.restricted) { - const url = getUrl(RouteTypes.RESTRICTED_VOTE, router) + const locale = getLocaleShort(router); + const url = getUrl(RouteTypes.RESTRICTED_VOTE, locale) router.push(url); } diff --git a/services/routes.ts b/services/routes.ts index c31d32a..bff4942 100644 --- a/services/routes.ts +++ b/services/routes.ts @@ -1,7 +1,6 @@ /** * This file provides the paths to the pages */ -import {NextRouter} from 'next/router'; import {getWindowUrl, displayRef, getLocaleShort} from './utils'; export enum RouteTypes { @@ -18,11 +17,10 @@ export enum RouteTypes { export const getUrl = ( type: RouteTypes, - router: NextRouter, + locale: string, ref?: string, token?: string ): URL => { - const locale = getLocaleShort(router); if (ref) { if (token) { diff --git a/services/utils.ts b/services/utils.ts index a3dd80c..072ba12 100644 --- a/services/utils.ts +++ b/services/utils.ts @@ -2,10 +2,11 @@ * This file contains several utils functions */ -import { NextRouter } from 'next/router'; +import {NextRouter} from 'next/router'; import {URL_APP} from './constants'; export const getLocaleShort = (router: NextRouter): string => { + console.log("router", router) if (!router.locale) { return router.defaultLocale.substring(0, 2); } @@ -14,7 +15,7 @@ export const getLocaleShort = (router: NextRouter): string => { }; export const getWindowUrl = (): string => { - if( typeof window !== 'undefined' && window.location.origin) + if (typeof window !== 'undefined' && window.location.origin) return window.location.origin; if (process.env.NODE_ENV === 'development') return "http://localhost:3000";