diff --git a/app/components/Calendar.tsx b/app/components/Calendar.tsx index b8c573d..6d062bf 100644 --- a/app/components/Calendar.tsx +++ b/app/components/Calendar.tsx @@ -15,6 +15,17 @@ export default function Calendar() { const [firstWeekPadding, setFirstWeekPadding] = useState([]); const [showLatestBoxModal, setShowLatestBoxModal] = useState(false); const [latestBoxStatus, setLatestBoxStatus] = useState(boxStatuses[0]); + const [latestBox, setLatestBox] = useState({ + id: "", + pickup_date: null, + delivery_date: new Date(), + }); + const [showClickedBoxModal, setShowClickedBoxModal] = useState(false); + const [clickedBox, setClickedBox] = useState({ + id: "", + pickup_date: null, + delivery_date: new Date(), + }); const generateFirstWeekPadding = (month: number) => { const firstDay = new Date(new Date().getFullYear(), month, 1).getDay(); @@ -43,45 +54,25 @@ export default function Calendar() { key={index} className={`${styles.calendarCell} ${matchingBox.pickup_date ? styles.pickedUpCell : styles.deliveredCell}`} onClick={() => { - if (matchingBox.pickup_date) { - // If the box is already picked up, mark it as delivered - markBoxDelivered(matchingBox.id).then((updatedBox) => { - setCustomerData((prev) => ({ - ...prev, - boxes: prev.boxes.map(box => - box.id === updatedBox.id ? { ...box, pickup_date: null } : box - ), - })); - } - ).catch((error) => { - console.error("Error marking box as delivered:", error); - }); - return; - } else { - markBoxPickedUp(matchingBox.id).then((updatedBox) => { - setCustomerData((prev) => ({ - ...prev, - boxes: prev.boxes.map(box => - box.id === updatedBox.id ? { ...box, pickup_date: updatedBox.pickup_date } : box - ), - })); - } - ).catch((error) => { - console.error("Error marking box as picked up:", error); - }) - } - }}> - {date.toLocaleDateString("fi-FI", { - day: "2-digit", - month: "2-digit", - })} -
- {matchingBox.pickup_date ? ( - Haettu - ) : ( - Jako - )} - + setClickedBox(matchingBox); + setShowClickedBoxModal(true); + } + }> + { + date.toLocaleDateString("fi-FI", { + day: "2-digit", + month: "2-digit", + }) + } + < br /> + { + matchingBox.pickup_date ? ( + Haettu + ) : ( + Jako + ) + } + ); } @@ -97,6 +88,36 @@ export default function Calendar() { return calendarCells; } + const changeClickedBoxStatus = () => { + if (clickedBox.pickup_date) { + // If the box is already picked up, mark it as delivered + markBoxDelivered(clickedBox.id).then((updatedBox) => { + setCustomerData((prev) => ({ + ...prev, + boxes: prev.boxes.map(box => + box.id === updatedBox.id ? { ...box, pickup_date: null } : box + ), + })); + } + ).catch((error) => { + console.error("Error marking box as delivered:", error); + }); + return; + } else { + markBoxPickedUp(clickedBox.id).then((updatedBox) => { + setCustomerData((prev) => ({ + ...prev, + boxes: prev.boxes.map(box => + box.id === updatedBox.id ? { ...box, pickup_date: updatedBox.pickup_date } : box + ), + })); + } + ).catch((error) => { + console.error("Error marking box as picked up:", error); + }) + } + } + const renderLatestBoxButton = () => { switch (latestBoxStatus) { case "no boxes": @@ -151,9 +172,6 @@ export default function Calendar() { } const markLatestBoxPickedUp = () => { - const deliveredBoxes = customerData.boxes.filter(box => new Date(box.delivery_date).getMonth() === selectedMonth && !box.pickup_date); - if (deliveredBoxes.length === 0) return; - const latestBox = deliveredBoxes[0]; markBoxPickedUp(latestBox.id).then((updatedBox) => { setCustomerData((prev) => ({ ...prev, @@ -166,6 +184,16 @@ export default function Calendar() { }); } + useEffect(() => { + const deliveredBoxes = customerData.boxes.filter(box => new Date(box.delivery_date).getMonth() === selectedMonth && !box.pickup_date); + if (deliveredBoxes.length === 0) return; + const latestBox = deliveredBoxes[0]; + + if (latestBox) { + setLatestBox(latestBox); + } + }, [customerData.boxes, selectedMonth]); + return ( <>

@@ -200,7 +228,10 @@ export default function Calendar() { >

Viimeisin laatikko

-

Oletko varma, että haluat kuitata viimeisimmän laatikon haetuksi?

+

Oletko varma, että haluat kuitata viimeisimmän laatikon ({new Date(latestBox.delivery_date).toLocaleDateString("fi-FI", { + day: "2-digit", + month: "2-digit", + })}) haetuksi?

+ setShowClickedBoxModal(false)} + className={styles.latestBoxModal} + > +
+

Satolaatikko {" "} + {new Date(clickedBox.delivery_date).toLocaleDateString("fi-FI", { + day: "2-digit", + month: "2-digit", + })} +

+

Oletko varma, että haluat {clickedBox.pickup_date ? "peruuttaa " : "kuitata "} + {new Date(clickedBox.delivery_date).toLocaleDateString("fi-FI", { + day: "2-digit", + month: "2-digit", + })} päivän laatikon {clickedBox.pickup_date ? "haun" : "haetuksi"}?

+ + +
+
); } \ No newline at end of file