Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 862844f6af |
49
src/lib/ToastService/Toast.svelte
Normal file
49
src/lib/ToastService/Toast.svelte
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import type { ToastType } from "$lib/ToastService/types";
|
||||||
|
import { onMount } from "svelte";
|
||||||
|
|
||||||
|
const { toast, destroyer }: {
|
||||||
|
toast: ToastType,
|
||||||
|
destroyer: () => void
|
||||||
|
} = $props();
|
||||||
|
|
||||||
|
const alertClass = $derived.by(() => {
|
||||||
|
switch (toast.type) {
|
||||||
|
case "success":
|
||||||
|
return 'alert-success';
|
||||||
|
case "warning":
|
||||||
|
return 'alert-warning';
|
||||||
|
case "error":
|
||||||
|
return 'alert-error';
|
||||||
|
case "info":
|
||||||
|
return 'alert-info';
|
||||||
|
default:
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let timeMs = $state(0);
|
||||||
|
let expireTime = 50000;
|
||||||
|
let progressMax = 100;
|
||||||
|
let segmentTime = Math.round(expireTime / progressMax);
|
||||||
|
const progressValue = $derived(Math.round(timeMs / segmentTime));
|
||||||
|
|
||||||
|
function increment() {
|
||||||
|
timeMs += segmentTime;
|
||||||
|
if (timeMs >= expireTime) destroyer();
|
||||||
|
}
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
const interval = setInterval(increment, segmentTime);
|
||||||
|
return () => clearInterval(interval);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="w-[60vw]">
|
||||||
|
<div class="absolute w-full flex flex-col px-2">
|
||||||
|
<progress class="progress h-1 w-full" value={progressValue} max="100"></progress>
|
||||||
|
</div>
|
||||||
|
<div class={["alert", alertClass]}>
|
||||||
|
<span>{toast.content}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
27
src/lib/ToastService/ToastProvider.svelte
Normal file
27
src/lib/ToastService/ToastProvider.svelte
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import type { ServiceState, ToastType } from "$lib/ToastService/types";
|
||||||
|
import { setContext } from "svelte";
|
||||||
|
import Toast from "./Toast.svelte";
|
||||||
|
|
||||||
|
const { children } = $props();
|
||||||
|
|
||||||
|
let serviceState: ServiceState = $state({ toasts: {}, nextId: 0 });
|
||||||
|
|
||||||
|
function addToast(toast: ToastType) {
|
||||||
|
serviceState.toasts[serviceState.nextId++] = toast;
|
||||||
|
}
|
||||||
|
|
||||||
|
setContext('toastservice', addToast);
|
||||||
|
|
||||||
|
function destroyToast(id: string) {
|
||||||
|
delete serviceState.toasts[id];
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{@render children()}
|
||||||
|
<div class="toast toast-top toast-center z-80">
|
||||||
|
{#each Object.entries(serviceState.toasts) as [i, toast] (i)}
|
||||||
|
<Toast {toast} destroyer={() => destroyToast(i)}/>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
13
src/lib/ToastService/types.ts
Normal file
13
src/lib/ToastService/types.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
export type ToastType = {
|
||||||
|
type: 'info' | 'success' | 'warning' | 'error',
|
||||||
|
content: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
export type InternalToasts = {
|
||||||
|
[key: string]: ToastType,
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ServiceState = {
|
||||||
|
toasts: InternalToasts,
|
||||||
|
nextId: number,
|
||||||
|
}
|
||||||
8
src/lib/ToastService/useToast.ts
Normal file
8
src/lib/ToastService/useToast.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { getContext } from "svelte";
|
||||||
|
import type { ToastType } from "$lib/ToastService/types";
|
||||||
|
|
||||||
|
export function useToast(): { fireToast: (toast: ToastType) => void } {
|
||||||
|
return {
|
||||||
|
fireToast: getContext('toastservice')
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import '../app.css';
|
import '../app.css';
|
||||||
|
import ToastProvider from "$lib/ToastService/ToastProvider.svelte";
|
||||||
|
|
||||||
let { children } = $props();
|
let { children } = $props();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{@render children()}
|
<ToastProvider>
|
||||||
|
{@render children()}
|
||||||
|
</ToastProvider>
|
||||||
|
|||||||
@@ -2,9 +2,12 @@
|
|||||||
import { scale } from 'svelte/transition';
|
import { scale } from 'svelte/transition';
|
||||||
import { goto } from "$app/navigation";
|
import { goto } from "$app/navigation";
|
||||||
import BSlideOverlay from "$lib/BSlideOverlay.svelte";
|
import BSlideOverlay from "$lib/BSlideOverlay.svelte";
|
||||||
|
import { useToast } from "$lib/ToastService/useToast";
|
||||||
|
|
||||||
let { data } = $props();
|
let { data } = $props();
|
||||||
|
|
||||||
|
const { fireToast } = useToast();
|
||||||
|
|
||||||
let showOverlay = $state(false);
|
let showOverlay = $state(false);
|
||||||
let startingCharge = $state(false);
|
let startingCharge = $state(false);
|
||||||
|
|
||||||
@@ -13,12 +16,12 @@
|
|||||||
startingCharge = true;
|
startingCharge = true;
|
||||||
try {
|
try {
|
||||||
// TODO: StartCharge fetch request
|
// TODO: StartCharge fetch request
|
||||||
const response = await fetch(`/api/public/1/chargecontroller/${data.qrcode}/start_charge/`, {
|
// const response = await fetch(`/api/public/1/chargecontroller/${data.qrcode}/start_charge/`, {
|
||||||
method: 'POST',
|
// method: 'POST',
|
||||||
headers: {
|
// headers: {
|
||||||
'Accept': 'application/json',
|
// 'Accept': 'application/json',
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
console.log("nop");
|
console.log("nop");
|
||||||
await goto(`/${data.qrcode}/status`);
|
await goto(`/${data.qrcode}/status`);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -31,6 +34,9 @@
|
|||||||
<div class="row-start-5 col-start-1 flex flex-col justify-center items-center">
|
<div class="row-start-5 col-start-1 flex flex-col justify-center items-center">
|
||||||
<div class="pointer-events-auto">
|
<div class="pointer-events-auto">
|
||||||
<button class="btn btn-primary btn-lg uppercase" onclick={() => showOverlay = true}>Attivare la Ricarica</button>
|
<button class="btn btn-primary btn-lg uppercase" onclick={() => showOverlay = true}>Attivare la Ricarica</button>
|
||||||
|
<button class="btn btn-secondary" onclick={() => fireToast({type: 'success', content: 'Messaggio Ricevuto!'})}>
|
||||||
|
TEST
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<BSlideOverlay bind:show={showOverlay} closable={!startingCharge}>
|
<BSlideOverlay bind:show={showOverlay} closable={!startingCharge}>
|
||||||
|
|||||||
Reference in New Issue
Block a user