Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cookie banner #193

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/components/CookieModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useCallback, useEffect, useState } from "react";

const CookieModal = () => {
const [cookieValue, setCookieValue] = useState<string | null>(null);
const [loading, setLoading] = useState<boolean>(true);

const handleButtonClick = useCallback((choice: string) => {
localStorage.setItem('userCookie', choice);
setCookieValue(choice);
}, []);

useEffect(() => {
const userCookie = localStorage.getItem('userCookie');
setCookieValue(userCookie);
setLoading(false);
}, []);

if (loading) return null;
sweta1308 marked this conversation as resolved.
Show resolved Hide resolved

return (
<>
{!cookieValue && (
<div className="fixed bottom-0 bg-[#F8FBFC] z-[20] p-6 shadow-shadow card sm:m-5 sm:max-w-xl lg:max-w-3xl">
<p className="text-[#292929] font-light">
By clicking &quot;Accept&quot;, you agree to the storing of
cookies on your device to enhance site navigation,
improve your experience, and assist in our marketing
efforts. To learn more, read our Privacy Policy.
</p>

<div className="mt-8 flex gap-3 justify-end">
<button
className="text-white bg-[#7147E8] rounded-3xl py-2 px-8 text-sm"
onClick={() => handleButtonClick('Accept')}
>
Accept
</button>
<button
className="text-[#7147E8] bg-white border border-[#7147E8] rounded-3xl py-2 px-8 text-sm"
onClick={() => handleButtonClick('Decline')}
>
Decline
</button>
</div>
</div>
)}
</>
)
}

export default CookieModal
17 changes: 15 additions & 2 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ import '@fontsource/metropolis/700.css';
import { config } from '@fortawesome/fontawesome-svg-core';
import AOS from 'aos';
import 'aos/dist/aos.css';
import { useEffect } from 'react';
import { useEffect, useState } from 'react';
import Script from 'next/script';
import Head from 'next/head';
import CookieModal from '@/components/CookieModal';

config.autoAddCss = false;

export default function App({ Component, pageProps }: AppProps) {
const [storedCookie, setStoredCookie] = useState<string | null>(null);

useEffect(() => {
const userCookie = localStorage.getItem('userCookie')
setStoredCookie(userCookie);
}, []);
sweta1308 marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
AOS.init({
offset: 80,
Expand All @@ -27,7 +35,9 @@ export default function App({ Component, pageProps }: AppProps) {

return (
<>
{/* Google Tag Manager */}
{storedCookie === 'Accept' && (
<>
{/* Google Tag Manager */}
<Head>
{/* eslint-disable-next-line @next/next/next-script-for-ga */}
<script
Expand Down Expand Up @@ -74,6 +84,9 @@ export default function App({ Component, pageProps }: AppProps) {
id="gtag-init"
strategy="afterInteractive"
/>
</>
)}
<CookieModal />
<Component {...pageProps} />
</>
);
Expand Down