feat(#5): Add cookie consent banner

This commit is contained in:
Josh Creek
2024-05-14 20:54:57 +01:00
parent c4bd0d78bc
commit e9d5f116fa
2 changed files with 55 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
<script lang="ts">
import { onMount } from 'svelte';
import { isBrowser } from '@supabase/ssr';
let showBanner = false;
const acceptCookies = () => {
if (isBrowser()) {
localStorage.setItem('cookiesAccepted', 'true');
showBanner = false;
}
location.reload();
};
let cookiesAccepted = false;
onMount(() => {
if (isBrowser()) {
if (localStorage.getItem('cookiesAccepted')) {
cookiesAccepted = true;
}
}
});
</script>
{#if !cookiesAccepted}
<div class="fixed bottom-0 left-0 w-full bg-gray-800 text-white p-4">
<p class="text-sm">
We use cookies to ensure you get the best experience on our website. By continuing, you agree
to our use of cookies.
</p>
<button class="bg-blue-500 hover:bg-blue-600 px-4 py-2 rounded" on:click={acceptCookies}
>Accept</button
>
</div>
{/if}
+18
View File
@@ -1,11 +1,13 @@
<script lang="ts">
import 'tailwindcss/tailwind.css';
import { onDestroy, onMount } from 'svelte';
import { isBrowser } from '@supabase/ssr';
import { basket, type Basket, type Item } from '$lib/stores/basket.js';
import { general } from '$lib/stores/generalStore.js';
import { invalidate } from '$app/navigation';
import SignIn from '$lib/components/SignIn.svelte';
import SignOut from '$lib/components/SignOut.svelte';
import CookieConsent from '$lib/components/CookieConsent.svelte';
export let data;
let { supabase, session } = data;
@@ -20,11 +22,14 @@
const unsubscribeToGeneralStore = general.subscribe((value) => {
localGeneral = value;
});
onDestroy(() => {
unsubscribeToBasket();
unsubscribeToGeneralStore();
});
let cookiesAccepted = false;
onMount(() => {
const { data } = supabase.auth.onAuthStateChange((event, _session) => {
if (_session?.expires_at !== session?.expires_at) {
@@ -33,6 +38,12 @@
}
});
if (isBrowser()) {
if (localStorage.getItem('cookiesAccepted')) {
cookiesAccepted = true;
}
}
return () => data.subscription.unsubscribe();
});
</script>
@@ -44,6 +55,10 @@
<meta property="og:description" content="" />
<link rel="canonical" href="" />
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
{#if cookiesAccepted}
<!-- Google Analytics script goes here -->
{/if}
</svelte:head>
<header>
@@ -162,6 +177,8 @@
<slot />
</main>
<CookieConsent />
<footer class="footer items-center p-4 bg-neutral text-neutral-content">
<aside class="items-center grid-flow-col">
<svg
@@ -218,6 +235,7 @@
</a>
</nav>
</footer>
<div
id="success-toast"
role="alert"