2024-12-16 16:35:12 +01:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
import Link from 'next/link';
|
|
|
|
import { useParams, useRouter } from 'next/navigation';
|
|
|
|
import * as React from 'react';
|
2025-01-04 14:57:53 +01:00
|
|
|
import { useForm, useFieldArray } from 'react-hook-form';
|
2024-12-16 16:35:12 +01:00
|
|
|
import { fetcher } from '@/app/api/tools';
|
|
|
|
import useSWR from 'swr';
|
2025-01-04 14:57:53 +01:00
|
|
|
import { Dish } from '@/app/tools';
|
2024-12-16 16:35:12 +01:00
|
|
|
|
2025-02-10 20:56:55 +01:00
|
|
|
type FormDish = Dish & {
|
|
|
|
filter: string;
|
|
|
|
};
|
|
|
|
|
2024-12-16 16:35:12 +01:00
|
|
|
type FormValues = {
|
2025-02-10 20:56:55 +01:00
|
|
|
status: number;
|
2025-01-04 14:57:53 +01:00
|
|
|
client: string;
|
|
|
|
realization_time: string;
|
2025-02-10 20:56:55 +01:00
|
|
|
data: FormDish[];
|
2024-12-16 16:35:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function App() {
|
|
|
|
const { data, error, isLoading } = useSWR('/api/meals', fetcher);
|
|
|
|
|
|
|
|
const { id } = useParams();
|
|
|
|
const router = useRouter();
|
|
|
|
const {
|
|
|
|
register,
|
|
|
|
control,
|
|
|
|
handleSubmit,
|
|
|
|
formState: { errors },
|
2025-02-10 20:56:55 +01:00
|
|
|
getValues,
|
|
|
|
setValue,
|
|
|
|
watch,
|
2024-12-16 16:35:12 +01:00
|
|
|
} = useForm<FormValues>({
|
|
|
|
defaultValues: {
|
2025-02-10 20:56:55 +01:00
|
|
|
status: 2,
|
2025-01-04 14:57:53 +01:00
|
|
|
client: '',
|
2025-02-10 20:56:55 +01:00
|
|
|
realization_time: new Date().toLocaleTimeString('pl-PL', {
|
|
|
|
hour: '2-digit',
|
|
|
|
minute: '2-digit',
|
|
|
|
}),
|
2025-01-04 14:57:53 +01:00
|
|
|
data: [
|
2025-02-10 20:56:55 +01:00
|
|
|
{
|
|
|
|
item: '',
|
|
|
|
additional_info: '',
|
|
|
|
finished: false,
|
|
|
|
takeout: false,
|
|
|
|
times: 1,
|
|
|
|
filter: '',
|
|
|
|
},
|
2025-01-04 14:57:53 +01:00
|
|
|
],
|
2024-12-16 16:35:12 +01:00
|
|
|
},
|
2025-01-04 14:57:53 +01:00
|
|
|
mode: 'all',
|
2025-02-10 20:56:55 +01:00
|
|
|
criteriaMode: 'all',
|
|
|
|
shouldFocusError: true
|
2024-12-16 16:35:12 +01:00
|
|
|
});
|
|
|
|
const { fields, append, remove } = useFieldArray({
|
|
|
|
name: 'data',
|
|
|
|
control,
|
|
|
|
});
|
|
|
|
const onSubmit = (data: FormValues) => {
|
|
|
|
fetch('/api/orders', {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({ waiter: id, ...data }),
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(({ status }) => console.log(status))
|
2025-01-04 14:57:53 +01:00
|
|
|
.then(() => router.push(`/waiter/${id}`));
|
2024-12-16 16:35:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if (error) return <div>Błąd przy ładowaniu danych</div>;
|
|
|
|
if (isLoading) return <div>Ładowanie</div>;
|
|
|
|
|
2025-02-10 20:56:55 +01:00
|
|
|
watch('data')
|
|
|
|
|
2024-12-16 16:35:12 +01:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<form
|
|
|
|
id="form"
|
|
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
|
|
className="flex flex-col max-w-screen-md w-full my-4"
|
|
|
|
>
|
|
|
|
<p className="text-2xl text-center">Nowe zamówienie</p>
|
2025-01-04 14:57:53 +01:00
|
|
|
<p>
|
|
|
|
<label htmlFor={`client`}>Klient</label>
|
|
|
|
<input
|
|
|
|
placeholder="Klient"
|
|
|
|
type="text"
|
|
|
|
{...register(`client` as const, {
|
|
|
|
required: false,
|
|
|
|
})}
|
|
|
|
className={errors?.client ? 'error' : ''}
|
|
|
|
/>
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
<label htmlFor={`realization_time`}>Godzina realizacji</label>
|
|
|
|
<input
|
|
|
|
{...register(`realization_time` as const, {
|
|
|
|
required: false,
|
|
|
|
})}
|
|
|
|
className={errors?.realization_time ? 'error' : ''}
|
|
|
|
/>
|
|
|
|
</p>
|
2024-12-16 16:35:12 +01:00
|
|
|
{fields.map((field, index) => {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
key={field.id}
|
|
|
|
className="border-2 m-2 p-2 gap-4 flex flex-col items-stretch justify-around content-center"
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
className="hidden"
|
|
|
|
{...register(`data.${index}.finished` as const, {
|
|
|
|
required: false,
|
|
|
|
})}
|
|
|
|
/>
|
2025-02-10 20:56:55 +01:00
|
|
|
<div className="basis-1/3 gap-2">
|
2024-12-16 16:35:12 +01:00
|
|
|
<label htmlFor={`data.${index}.item`}>Danie</label>
|
2025-02-10 20:56:55 +01:00
|
|
|
<input
|
|
|
|
{...register(`data.${index}.filter` as const, {
|
|
|
|
required: false,
|
|
|
|
})}
|
|
|
|
placeholder='Wyszukaj danie'
|
|
|
|
className={errors?.data?.[index]?.filter ? 'error' : ''}
|
|
|
|
onChange={(e) => setValue(`data.${index}.item`, data
|
|
|
|
?.filter((data: { id: number; name: string }) =>
|
|
|
|
data.name
|
|
|
|
.toLowerCase()
|
|
|
|
.includes(
|
|
|
|
e.target.value.toLowerCase()
|
|
|
|
)
|
|
|
|
)[0].name, {shouldValidate: true})}
|
|
|
|
/>
|
2024-12-16 16:35:12 +01:00
|
|
|
<select
|
|
|
|
{...register(`data.${index}.item` as const, {
|
|
|
|
required: true,
|
|
|
|
})}
|
|
|
|
className={errors?.data?.[index]?.item ? 'error' : ''}
|
|
|
|
>
|
2025-02-10 20:56:55 +01:00
|
|
|
{!!getValues(`data.${index}.filter`) ? (
|
|
|
|
null
|
|
|
|
) : (
|
|
|
|
<option value="Inne">Inne</option>
|
|
|
|
)}
|
|
|
|
{data
|
|
|
|
?.filter((data: { id: number; name: string }) =>
|
|
|
|
data.name
|
|
|
|
.toLowerCase()
|
|
|
|
.includes(
|
|
|
|
getValues(`data.${index}.filter`).toLowerCase()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.map((data: { id: number; name: string }) => (
|
|
|
|
<option key={data.name} value={data.name}>
|
|
|
|
{data.name}
|
|
|
|
</option>
|
|
|
|
))}
|
2024-12-16 16:35:12 +01:00
|
|
|
</select>
|
2025-02-10 20:56:55 +01:00
|
|
|
<label htmlFor={`data.${index}.times`}>Ilość</label>
|
|
|
|
<input
|
|
|
|
{...register(`data.${index}.times` as const, {
|
|
|
|
required: false,
|
|
|
|
})}
|
|
|
|
type="number"
|
|
|
|
className={errors?.data?.[index]?.times ? 'error' : ''}
|
|
|
|
/>
|
2025-01-04 14:57:53 +01:00
|
|
|
<section className="flex flex-row my-2 gap-2">
|
|
|
|
<label htmlFor={`data.${index}.takeout`}>Na wynos</label>
|
|
|
|
<input
|
|
|
|
{...register(`data.${index}.takeout` as const, {
|
|
|
|
required: false,
|
|
|
|
})}
|
|
|
|
type="checkbox"
|
|
|
|
className={errors?.data?.[index]?.takeout ? 'error' : ''}
|
|
|
|
/>
|
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
<div className="basis-1/3">
|
2024-12-16 16:35:12 +01:00
|
|
|
<label htmlFor={`data.${index}.additional_info`}>
|
|
|
|
Dodatkowe informacje
|
|
|
|
</label>
|
|
|
|
<textarea
|
|
|
|
placeholder="Dodatkowe informacje"
|
|
|
|
{...register(`data.${index}.additional_info` as const, {
|
|
|
|
required: false,
|
|
|
|
})}
|
|
|
|
className={
|
|
|
|
errors?.data?.[index]?.additional_info ? 'error' : ''
|
|
|
|
}
|
|
|
|
/>
|
2025-01-04 14:57:53 +01:00
|
|
|
</div>
|
2024-12-16 16:35:12 +01:00
|
|
|
{fields.length > 1 ? (
|
|
|
|
<p className="basis-1/3 flex items-center justify-center">
|
|
|
|
<button type="button" onClick={() => remove(index)}>
|
|
|
|
Usuń
|
|
|
|
</button>
|
|
|
|
</p>
|
|
|
|
) : (
|
|
|
|
<></>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="mx-auto"
|
|
|
|
onClick={() =>
|
|
|
|
append({
|
|
|
|
item: '',
|
|
|
|
additional_info: '',
|
|
|
|
finished: false,
|
2025-01-04 14:57:53 +01:00
|
|
|
takeout: false,
|
2025-02-10 20:56:55 +01:00
|
|
|
times: 1,
|
|
|
|
filter: '',
|
2024-12-16 16:35:12 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
|
|
|
Dodaj kolejne danie
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
className={
|
|
|
|
'mt-10 mx-auto p-2 border-2 text-black hover:text-black ' +
|
|
|
|
(!!errors.data
|
|
|
|
? 'bg-red-300 border-red-500'
|
|
|
|
: 'bg-green-300 border-green-500')
|
|
|
|
}
|
|
|
|
disabled={!!errors.data}
|
|
|
|
>
|
|
|
|
Złóż zamówienie
|
|
|
|
</button>
|
|
|
|
</form>
|
2025-01-04 14:57:53 +01:00
|
|
|
<Link href={`/waiter/${id}`} className="mt-10">
|
|
|
|
<button>Powrót</button>
|
|
|
|
</Link>
|
2024-12-16 16:35:12 +01:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|