diff --git a/app/shopping-list/new/page.tsx b/app/shopping-list/new/page.tsx index cfda346..5f0d567 100644 --- a/app/shopping-list/new/page.tsx +++ b/app/shopping-list/new/page.tsx @@ -1,17 +1,37 @@ -"use client" +"use client"; -import { useRouter } from "next/navigation" -import { useState } from "react" +import Link from "next/link"; +import { useRouter } from "next/navigation"; +import { useMemo, useState } from "react"; + +function formatDDMMYY(value: string) { + if (!value) return ""; + const [yyyy, mm, dd] = value.split("-"); + if (!yyyy || !mm || !dd) return value; + return `${dd}/${mm}/${yyyy.slice(2)}`; +} export default function NewShoppingListPage() { - const router = useRouter() - const [start, setStart] = useState("") - const [end, setEnd] = useState("") + const router = useRouter(); + const [start, setStart] = useState(""); + const [end, setEnd] = useState(""); + + const hint = useMemo(() => { + if (!start && !end) return null; + const s = start ? formatDDMMYY(start) : "__/__/__"; + const e = end ? formatDDMMYY(end) : "__/__/__"; + return `Wybrany zakres: ${s} → ${e}`; + }, [start, end]); async function handleGenerate() { if (!start || !end) { - alert("Wybierz zarówno datę początkową, jak i końcową") - return + alert("Wybierz zarówno datę początkową, jak i końcową"); + return; + } + + if (start > end) { + alert("Data końcowa nie może być wcześniejsza niż początkowa."); + return; } try { @@ -20,42 +40,114 @@ export default function NewShoppingListPage() { headers: { "Content-Type": "application/json" }, body: JSON.stringify({ start, end }), cache: "no-store", - }) + }); - if (!res.ok) { - throw new Error("Nie udało się wygenerować listy zakupów") - } + if (!res.ok) throw new Error("Nie udało się wygenerować listy zakupów"); - router.push("/shopping-list") + router.push("/shopping-list"); } catch (err) { - console.error(err) - alert("Wystąpił błąd przy tworzeniu listy zakupów") + console.error(err); + alert("Wystąpił błąd przy tworzeniu listy zakupów"); } } return ( -
+ Format wyświetlania: DD/MM/YY +
- {/* Proste wprowadzanie dat*/} -+ Wybrano: {formatDDMMYY(start)} +
+ ) : null} ++ Wybrano: {formatDDMMYY(end)} +
+ ) : null} +{hint}
: null} + +