Adding a dialog to the root layout.

This commit is contained in:
Bradley Shellnut 2022-08-29 16:41:11 -05:00
parent 800ae1da7e
commit 2e389309ce
7 changed files with 63 additions and 15 deletions

View file

@ -7,7 +7,7 @@
async function getRandomCollectionGame() { async function getRandomCollectionGame() {
if ($collectionStore.length > 0) { if ($collectionStore.length > 0) {
boredState.set({ loading: true }); boredState.set({ loading: true, dialogOpen: false });
let randomNumber: number = Math.round(Math.random() * $collectionStore.length - 1); let randomNumber: number = Math.round(Math.random() * $collectionStore.length - 1);
if ($collectionStore.at(randomNumber)) { if ($collectionStore.at(randomNumber)) {
gameStore.removeAll(); gameStore.removeAll();
@ -19,11 +19,11 @@
const responseData = await response.json(); const responseData = await response.json();
console.log('responseData', responseData); console.log('responseData', responseData);
gameStore.add(responseData?.game); gameStore.add(responseData?.game);
boredState.set({ loading: false }); boredState.set({ loading: false, dialogOpen: false });
} else { } else {
toast.send('Error!', { duration: 3000, type: ToastType.ERROR, dismissible: true }); toast.send('Error!', { duration: 3000, type: ToastType.ERROR, dismissible: true });
} }
boredState.set({ loading: false }); boredState.set({ loading: false, dialogOpen: false });
} else { } else {
toast.send('No items in your collection!', { toast.send('No items in your collection!', {
duration: 3000, duration: 3000,

View file

@ -4,7 +4,7 @@
async function handleSubmit(event: SubmitEvent) { async function handleSubmit(event: SubmitEvent) {
// submitting = true; // submitting = true;
boredState.set({ loading: true }); boredState.set({ loading: true, dialogOpen: false });
const form = event.target as HTMLFormElement; const form = event.target as HTMLFormElement;
console.log('form', form); console.log('form', form);
const response = await fetch('/api/games', { const response = await fetch('/api/games', {
@ -14,7 +14,7 @@
}); });
const responseData = await response.json(); const responseData = await response.json();
// submitting = false; // submitting = false;
boredState.set({ loading: false }); boredState.set({ loading: false, dialogOpen: false });
gameStore.removeAll(); gameStore.removeAll();
gameStore.addAll(responseData?.games); gameStore.addAll(responseData?.games);
// games = responseData?.games; // games = responseData?.games;

View file

@ -4,7 +4,7 @@ import { writable } from 'svelte/store';
// Custom store // Custom store
const state = () => { const state = () => {
const { subscribe, set, update } = writable<BoredStore>({ loading: false }); const { subscribe, set, update } = writable<BoredStore>({ loading: false, dialogOpen: false });
// function remove(id: string) { // function remove(id: string) {
// update((store) => { // update((store) => {
@ -20,7 +20,7 @@ const state = () => {
// } // }
function clear() { function clear() {
set({ loading: false }); set({ loading: false, dialogOpen: false });
} }
return { subscribe, set, update, clear }; return { subscribe, set, update, clear };

View file

@ -1,5 +1,6 @@
export type BoredStore = { export type BoredStore = {
loading: boolean; loading: boolean;
dialogOpen: boolean;
}; };
export enum ToastType { export enum ToastType {

View file

@ -1,9 +1,8 @@
import { collectionStore } from '$lib/stores/collectionStore'; import { collectionStore } from '$lib/stores/collectionStore';
import { toast } from '$lib/components/toast/toast'; import { toast } from '$lib/components/toast/toast';
import { ToastType, type GameType, type SavedGameType } from '$lib/types'; import { ToastType, type GameType, type SavedGameType } from '$lib/types';
import { browser } from '$app/environment';
function convertToSavedGame(game: GameType): SavedGameType { function convertToSavedGame(game: GameType | SavedGameType): SavedGameType {
return { return {
id: game.id, id: game.id,
name: game.name, name: game.name,
@ -11,12 +10,12 @@ function convertToSavedGame(game: GameType): SavedGameType {
}; };
} }
export function addToCollection(game: GameType) { export function addToCollection(game: GameType | SavedGameType) {
collectionStore.add(convertToSavedGame(game)); collectionStore.add(convertToSavedGame(game));
toast.send("Added to collection", { duration: 3000, type: ToastType.INFO }); toast.send("Added to collection", { duration: 3000, type: ToastType.INFO });
} }
export function removeFromCollection(game: GameType) { export function removeFromCollection(game: GameType | SavedGameType) {
collectionStore.remove(game.id); collectionStore.remove(game.id);
toast.send("Removed from collection", { duration: 3000, type: ToastType.INFO }); toast.send("Removed from collection", { duration: 3000, type: ToastType.INFO });
} }

View file

@ -1,8 +1,15 @@
<script lang="ts"> <script lang="ts">
import { browser } from '$app/environment'; import { browser } from '$app/environment';
import { fade } from 'svelte/transition';
import { navigating } from '$app/stores'; import { navigating } from '$app/stores';
import debounce from 'just-debounce-it'; import debounce from 'just-debounce-it';
import { Toy } from '@leveluptuts/svelte-toy'; import { Toy } from '@leveluptuts/svelte-toy';
import {
Dialog,
DialogOverlay,
DialogTitle,
DialogDescription
} from '@rgossiaux/svelte-headlessui';
import Header from '$lib/components/header/Header.svelte'; import Header from '$lib/components/header/Header.svelte';
import Loading from '$lib/components/loading.svelte'; import Loading from '$lib/components/loading.svelte';
import Transition from '$lib/components/transition/index.svelte'; import Transition from '$lib/components/transition/index.svelte';
@ -17,11 +24,11 @@
$: { $: {
if ($navigating) { if ($navigating) {
debounce(() => { debounce(() => {
boredState.set({ loading: true }); boredState.set({ loading: true, dialogOpen: false });
}, 250); }, 250);
} }
if (!$navigating) { if (!$navigating) {
boredState.set({ loading: false }); boredState.set({ loading: false, dialogOpen: false });
} }
} }
@ -74,10 +81,51 @@
<div class="background" /> <div class="background" />
</Portal> </Portal>
{/if} {/if}
{#if $boredState?.dialogOpen}
<div transition:fade>
<Dialog
open={$boredState?.dialogOpen}
on:close={() => boredState.set({ loading: false, dialogOpen: false })}
static
>
<DialogOverlay
style={'position: fixed; inset: 0; z-index: 100; background-color: rgb(0 0 0); opacity: 0.3;'}
/>
<div class="dialog">
<DialogTitle>Deactivate account</DialogTitle>
<DialogDescription>This will permanently deactivate your account</DialogDescription>
<p>
Are you sure you want to deactivate your account? All of your data will be permanently
removed. This action cannot be undone.
</p>
<button on:click={() => boredState.set({ loading: false, dialogOpen: false })}
>Deactivate</button
>
<button on:click={() => boredState.set({ loading: false, dialogOpen: false })}
>Cancel</button
>
</div>
</Dialog>
</div>
{/if}
<Toast /> <Toast />
</Transition> </Transition>
<style lang="scss"> <style lang="scss">
:global(.dialog) {
position: fixed;
top: 25%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 101;
border-radius: 10px;
background-color: var(--primary);
padding: 2rem;
/* max-width: 400px; */
}
.loading { .loading {
position: fixed; position: fixed;
top: 50%; top: 50%;

View file

@ -8,7 +8,7 @@
import { boredState } from '$root/lib/stores/boredState'; import { boredState } from '$root/lib/stores/boredState';
async function handleSearch(event: SubmitEvent) { async function handleSearch(event: SubmitEvent) {
boredState.set({ loading: true }); boredState.set({ loading: true, dialogOpen: false });
const form = event.target as HTMLFormElement; const form = event.target as HTMLFormElement;
console.log('form', form); console.log('form', form);
const response = await fetch('/api/game', { const response = await fetch('/api/game', {
@ -17,7 +17,7 @@
body: new FormData(form) body: new FormData(form)
}); });
const responseData = await response.json(); const responseData = await response.json();
boredState.set({ loading: false }); boredState.set({ loading: false, dialogOpen: false });
gameStore.removeAll(); gameStore.removeAll();
gameStore.addAll(responseData?.games); gameStore.addAll(responseData?.games);
} }