You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
222 lines
8.1 KiB
222 lines
8.1 KiB
import React, { useState } from "react";
|
|
import Layout from "../components/Layout";
|
|
import Image from 'next/image';
|
|
import Head from "next/head";
|
|
import { useSession } from "next-auth/react";
|
|
import { useForm } from "react-hook-form";
|
|
import Router from "next/router";
|
|
import style from '../styles/Index.module.css';
|
|
import testerCodeCadeau from '../public/images/svg/testerlecode.svg'
|
|
import feuilleGauche from '../public/images/feuilleGauche.webp'
|
|
import feuilleDroite from '../public/images/feuilleBasDroite.webp'
|
|
import cadeau from '../public/images/cadeau.webp'
|
|
import jeuConcours from '../public/images/jeuconcours.webp'
|
|
import { toast } from "react-toastify";
|
|
import arrowUp from '../public/images/png/arrowup.png'
|
|
import footerGrass from '../public/images/footer_image.webp'
|
|
import Link from "next/link";
|
|
import Script from "next/script";
|
|
|
|
type CodeSubmitForm = {
|
|
codeCadeau: string;
|
|
};
|
|
|
|
type ResponseData = {
|
|
success: string,
|
|
winProduct: {
|
|
actif: boolean,
|
|
id: number,
|
|
nomProduit: string,
|
|
prixTtc: number
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const Index = () => {
|
|
|
|
const { data: session } = useSession();
|
|
const [response, setResponse] = useState<ResponseData | null>(null)
|
|
const [winAnimation, setWinAnimation] = useState(false)
|
|
const {
|
|
register,
|
|
handleSubmit,
|
|
formState: { errors }
|
|
} = useForm<CodeSubmitForm>();
|
|
|
|
const onSubmit = async (data: CodeSubmitForm) => {
|
|
try {
|
|
const body = { codeCadeau: data.codeCadeau };
|
|
await fetch(`/api/testcode`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
})
|
|
.then((res) => res.json())
|
|
.then(async (data) => {
|
|
setResponse(data);
|
|
if (data.err) {
|
|
console.log('ERR', data);
|
|
toast.error(data.err, {
|
|
position: "top-center",
|
|
autoClose: 2000,
|
|
hideProgressBar: false,
|
|
closeOnClick: true,
|
|
pauseOnHover: true,
|
|
draggable: true,
|
|
progress: undefined,
|
|
theme: "light",
|
|
});
|
|
return;
|
|
}
|
|
setWinAnimation(true)
|
|
await confettiTime();
|
|
})
|
|
} catch (error) {
|
|
console.error('ERROR :', error);
|
|
}
|
|
}
|
|
|
|
const confettiTime = async (): Promise<void> => {
|
|
const JSConfetti = (await import('js-confetti')).default
|
|
const jsConfetti = new JSConfetti()
|
|
jsConfetti.addConfetti()
|
|
}
|
|
|
|
const getCadeau = () => {
|
|
Router.push("/mon-compte")
|
|
}
|
|
return (
|
|
<>
|
|
<Head>
|
|
<title>Thé Tip Top</title>
|
|
<meta name="description" content="Participez au jeu concours Thé Tip Top ! Recevez un cadeau, et tentez votre chance pour gagner le gros lot final !" />
|
|
<meta name="robots" content="index, follow" />
|
|
<meta name="googlebot" content="index, follow" />
|
|
<meta name="googlebot-news" content="index, follow" />
|
|
<meta name="slurp" content="index, follow" />
|
|
<meta name="msnbot" content="index, follow" />
|
|
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossOrigin="" />
|
|
</Head>
|
|
<Script async src="https://www.googletagmanager.com/gtag/js?id=G-QNZD85D13X"></Script>
|
|
<Script>
|
|
{`
|
|
window.dataLayer = window.dataLayer || [];
|
|
function gtag(){dataLayer.push(arguments);}
|
|
gtag('js', new Date());
|
|
|
|
gtag('config', 'G-QNZD85D13X');
|
|
`}
|
|
</Script>
|
|
<Script>{`(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
|
|
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
|
|
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
|
|
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
|
|
})(window,document,'script','dataLayer','GTM-MMHM9HX');`}</Script>
|
|
|
|
<div className={`${style['masthead']} body-content`}>
|
|
<div className="d-flex flex-wrap justify-content-center align-items-center text-center">
|
|
<div className={`${style['feuilleGauche']} ${style['imageContainer']}`}>
|
|
<Image
|
|
src={feuilleGauche}
|
|
alt="thetiptop feuille Gauche"
|
|
layout="responsive"
|
|
className={`${style['rotatingSlow']}`}
|
|
/>
|
|
</div>
|
|
<div className={`${style['jeuConcours']} ${style['imageContainer']} ${style['zoomInOutCadeauContainer']}`}>
|
|
<Link href="/tout-savoir" legacyBehavior><a>
|
|
<Image
|
|
src={jeuConcours}
|
|
alt="thetiptop jeu concours, tout savoir"
|
|
layout="responsive"
|
|
className={`${style['zoomInOutCadeau']}`}
|
|
/>
|
|
</a></Link>
|
|
</div>
|
|
<div className={`${style['cadeau']} ${style['imageContainer']}`}>
|
|
<Image
|
|
src={cadeau}
|
|
alt="thetiptop cadeau"
|
|
layout="responsive"
|
|
className={`${style['zoomInOutJeuConcours']}`}
|
|
/>
|
|
</div>
|
|
<div className={`${style['feuilleDroite']} ${style['imageContainer']}`}>
|
|
<Image
|
|
src={feuilleDroite}
|
|
alt="thetiptop feuille Droite"
|
|
layout="responsive"
|
|
className={`${style['rotatingSlow']}`}
|
|
/>
|
|
</div>
|
|
|
|
{!winAnimation && <div className="col-12 align-items-center d-flex flex-column">
|
|
<div className={`${style['bloc-content']} col-12 col-md-6`}>
|
|
<div className={`${style['imageWrapper']}`}>
|
|
<Image
|
|
src={testerCodeCadeau}
|
|
alt="thetiptop navigation logo"
|
|
layout="responsive"
|
|
className={`${style['rotatingImage']}`}
|
|
/>
|
|
</div>
|
|
<form onSubmit={handleSubmit(onSubmit)}>
|
|
<div className="form-group mx-auto my-3 w-100">
|
|
<input type="text"
|
|
id="codeInput" aria-describedby="codeHelp"
|
|
data-testid="codeInput"
|
|
placeholder="A293049430..."
|
|
minLength={10}
|
|
maxLength={10}
|
|
{...register('codeCadeau')}
|
|
className={`form-control ${errors.codeCadeau ? 'is-invalid' : ''}`}
|
|
|
|
/>
|
|
<small id="codeHelp" className="form-text text-muted">Code à usage unique</small>
|
|
<div className="invalid-feedback alert alert-danger">{errors.codeCadeau?.message}</div>
|
|
</div>
|
|
<div className="form-group mx-auto my-3 w-100">
|
|
<button type="submit" data-testid="submit" className={`${style['aButton']} btn btn-primary`}>Découvrir mon cadeau !</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>}
|
|
{winAnimation && <div className="col-12 align-items-center d-flex flex-column">
|
|
<div className={`${style['cadeau-bloc-content']} col-12 col-md-6`} onClick={() => setWinAnimation(false)}
|
|
>
|
|
<div className={`${style['cadeau-bloc']}`}>
|
|
<h1>Vous avez gagnez : un {response.winProduct.nomProduit.toLowerCase()} !</h1>
|
|
</div>
|
|
</div>
|
|
<div className={`${style['imageWrapper']}`}>
|
|
<Image
|
|
src={arrowUp}
|
|
alt="thetiptop flèche haute"
|
|
|
|
width={50}
|
|
height={50}
|
|
className={`${style['rotatingImage']}`}
|
|
/>
|
|
</div>
|
|
<div className="row">
|
|
<button onClick={getCadeau} className={`${style['aButton']} btn btn-primary`}>Voir mes cadeaux !</button>
|
|
<button onClick={() => setWinAnimation(false)} className={`${style['aButton']} btn btn-primary mt-2`}>Rejouer un ticket !</button>
|
|
</div>
|
|
</div>}
|
|
</div>
|
|
<div className={`${style['footerGrass']} position-absolute`}>
|
|
<Image
|
|
src={footerGrass}
|
|
alt="thetiptop feuille Gauche"
|
|
layout="responsive"
|
|
/>
|
|
</div>
|
|
</div>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default Index;
|