import { JSX, useEffect, useState } from "react"; import styles from "./Calendar.module.css"; import { getMonthName, daysInMonth } from "../utils/dateUtils"; import { customerState } from "../store"; import { Box } from "../types"; import { useAtom } from "jotai"; import { markBoxDelivered, markBoxPickedUp } from "../api"; import { Modal } from "@mui/material"; export default function Calendar() { const latestBoxStatuses = ["no boxes", "picked up", "delivered"]; //TODO: There's logic problem between "delivered" and "picked up" const [customerData, setCustomerData] = useAtom(customerState); const [selectedMonth, setSelectedMonth] = useState(new Date().getMonth()); const [calendarCells, setCalendarCells] = useState([]); const [firstWeekPadding, setFirstWeekPadding] = useState([]); const [showLatestBoxModal, setShowLatestBoxModal] = useState(false); const [latestBoxStatus, setLatestBoxStatus] = useState(latestBoxStatuses[0]); const generateFirstWeekPadding = (month: number) => { const firstDay = new Date(new Date().getFullYear(), month, 1).getDay(); const padding = Array.from({ length: firstDay === 0 ? 6 : firstDay - 1 }, (_, index) => (
 
)); return padding; } const generateCalendarCells = (month: number, boxes: Box[]) => { const monthDays = daysInMonth[month]; const calendarCells = Array.from({ length: monthDays }, (_, index) => { const day = index + 1; const date = new Date(new Date().getFullYear(), month, day); const matchingBox = boxes.find(box => { const boxDate = new Date(box.delivery_date); return boxDate.getDate() === day && boxDate.getMonth() === month && boxDate.getFullYear() === date.getFullYear(); }); if (matchingBox) { return (
{ 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 )}
); } return (
{date.toLocaleDateString("fi-FI", { day: "2-digit", month: "2-digit", })}
); }); return calendarCells; } const renderLatestBoxButton = () => { switch (latestBoxStatus) { case "no boxes": return ; case "picked up": return ; case "delivered": return ; default: return Virhe; } } useEffect(() => { setCalendarCells(generateCalendarCells(selectedMonth, customerData.boxes)); setFirstWeekPadding(generateFirstWeekPadding(selectedMonth)); setLatestBoxStatus(() => { if (!customerData.boxes || customerData.boxes.length === 0) { return latestBoxStatuses[0]; // no boxes } const monthBoxes = customerData.boxes.filter(box => new Date(box.delivery_date).getMonth() === selectedMonth); if (!monthBoxes || monthBoxes.length === 0) { return latestBoxStatuses[0]; // no boxes } const latestBox = monthBoxes[monthBoxes.length - 1]; if (latestBox.pickup_date) { return latestBoxStatuses[1]; // picked up } return latestBoxStatuses[2]; // delivered } ); }, [selectedMonth, customerData.boxes]); const handleLatestBoxClick = (event: React.MouseEvent) => { event.preventDefault(); setShowLatestBoxModal(true); } const markLatestBoxPickedUp = () => { const monthBoxes = customerData.boxes.filter(box => new Date(box.delivery_date).getMonth() === selectedMonth); if (monthBoxes.length === 0) return; console.log("Marking latest box as picked up"); console.log("Month boxes:", monthBoxes); const latestBox = monthBoxes[monthBoxes.length - 1]; markBoxPickedUp(latestBox.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 latest box as picked up:", error); }); } return ( <>

{getMonthName(selectedMonth)} {new Date().getFullYear()}

{renderLatestBoxButton()}
Ma Ti Ke To Pe La Su {firstWeekPadding} {calendarCells}
setShowLatestBoxModal(false)} className={styles.latestBoxModal} >

Viimeisin laatikko

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

); }