119 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-12-16 16:35:12 +01:00
'use client';
import { fetcher } from '@/app/api/tools';
import useSWR from 'swr';
2024-12-17 22:58:49 +01:00
import { useEffect, useRef, useState } from 'react';
import { getDishBg, getOrderBg, Order } from '../tools';
import { OrderID } from '../components';
2024-12-16 16:35:12 +01:00
export default function Home() {
2024-12-17 22:58:49 +01:00
const audioRef = useRef({} as HTMLAudioElement);
const [playedIDs, setPlayedIDs] = useState<number[]>([]);
2024-12-16 16:35:12 +01:00
const { data, mutate, error, isLoading } = useSWR(`/api/orders`, fetcher, {
refreshInterval: 1000,
});
2024-12-17 22:58:49 +01:00
const play = () => {
if (audioRef.current) audioRef.current.play();
2024-12-17 22:58:49 +01:00
};
useEffect(() => {
if (!data) return;
const justPlayed = (data as Order[])
.filter(
(o) =>
new Date(o.updated_on).getTime() - new Date(o.created_on).getTime() <
10000 && o.status == 1
)
.map((o) => o.id)
.filter((id) => !playedIDs.includes(id));
if (justPlayed.length > 0) {
play();
setPlayedIDs(playedIDs.concat(justPlayed));
}
}, [playedIDs, data]);
2024-12-17 22:58:49 +01:00
2024-12-16 16:35:12 +01:00
const update_finished = (o: number, i: number, v: boolean) => {
const d = data;
2024-12-16 16:35:12 +01:00
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;
2024-12-16 16:35:12 +01:00
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 <div>Błąd przy ładowaniu danych</div>;
if (isLoading) return <div>Ładowanie</div>;
return (
<>
2024-12-17 22:58:49 +01:00
<audio ref={audioRef} src="/ping.wav"></audio>
2024-12-16 16:35:12 +01:00
<p className="text-4xl">Zamówienia:</p>
<ul className="max-w-screen-md w-full">
{data?.map((order: Order, oi: number) => (
2024-12-17 22:58:49 +01:00
<li
key={order.id}
className={`m-2 p-2 rounded-md border-2 ${getOrderBg(order)}`}
2024-12-17 22:58:49 +01:00
>
<OrderID order={order} />
2024-12-16 16:35:12 +01:00
<h2 className="text-xl">{order.waiter_name}</h2>
{ (order.client) ? <h2 className="text-xl">Klient: {order.client}</h2> : <></>}
2024-12-16 16:35:12 +01:00
<h3>
<select
value={order.status}
onChange={(e) =>
update_status(oi, e.target.value as unknown as number)
}
className="form border-2 my-2 border-gray-500"
2024-12-16 16:35:12 +01:00
>
2025-02-10 20:56:55 +01:00
{/* <option value="1">Zamówienie złożone</option> */}
2024-12-16 16:35:12 +01:00
<option value="2">Zamówienie w trakcie przygotowywania</option>
<option value="3">Zamówienie gotowe</option>
2025-02-10 20:56:55 +01:00
{/* <option value="4">Zamówienie zrealizowane</option> */}
2024-12-16 16:35:12 +01:00
</select>
</h3>
<div className="">
<ul>
{order.data.map((dish, i) => (
<li
key={dish.item}
className={`p-2 m-2 flex gap-4 border-2 ${getDishBg(dish)}`}
2024-12-16 16:35:12 +01:00
>
<input
type="checkbox"
checked={dish.finished}
onChange={(e) => update_finished(oi, i, e.target.checked)}
/>
<div>
2025-02-10 20:56:55 +01:00
<p className="text-lg">{dish.item}{dish.times > 1 ? ` - x${dish.times}`: ''} - {dish.takeout ? 'Na wynos' : 'Na miejscu'}</p>
2024-12-16 16:35:12 +01:00
<div>{dish.additional_info}</div>
</div>
</li>
))}
</ul>
</div>
</li>
))}
</ul>
</>
);
}