'use client'; import { fetcher } from '@/app/api/tools'; import useSWR from 'swr'; import { useEffect, useRef, useState } from 'react'; import { getDishBg, getOrderBg, Order } from '../tools'; import { OrderID } from '../components'; export default function Home() { const audioRef = useRef({} as HTMLAudioElement); const [playedIDs, setPlayedIDs] = useState([]); const { data, mutate, error, isLoading } = useSWR(`/api/orders`, fetcher, { refreshInterval: 1000, }); const play = () => { if (audioRef.current) audioRef.current.play(); }; useEffect(() => { if (!data) return; const justPlayed = (data as Order[]) .filter((o) => { const diff = new Date().getTime() - new Date(o.created_on).getTime(); return diff < 10000 && o.status == 2; }) .map((o) => o.id) .filter((id) => !playedIDs.includes(id)); if (justPlayed.length > 0) { play(); setPlayedIDs(playedIDs.concat(justPlayed)); } }, [playedIDs, data]); const update_finished = (o: number, i: number, v: boolean) => { const d = data; d[o].data[i].finished = v; fetch(`/api/orders?id=${d[o].id}`, { method: 'PUT', body: JSON.stringify(d[o]), headers: { 'Content-Type': 'application/json', }, }).then(() => mutate(d)); }; const update_status = (o: number, v: number) => { const d = data; d[o].status = v; fetch(`/api/orders?id=${d[o].id}`, { method: 'PUT', body: JSON.stringify(d[o]), headers: { 'Content-Type': 'application/json', }, }).then(() => mutate(d)); }; if (error) return
Błąd przy ładowaniu danych
; if (isLoading) return
Ładowanie
; return ( <>

Zamówienia:

); }