Mark latest box picked up button
This commit is contained in:
@@ -5,17 +5,16 @@ 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<JSX.Element[]>([]);
|
||||
const [firstWeekPadding, setFirstWeekPadding] = useState<JSX.Element[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
setCalendarCells(generateCalendarCells(selectedMonth, customerData.boxes));
|
||||
setFirstWeekPadding(generateFirstWeekPadding(selectedMonth));
|
||||
}, [selectedMonth, customerData.boxes]);
|
||||
const [showLatestBoxModal, setShowLatestBoxModal] = useState(false);
|
||||
const [latestBoxStatus, setLatestBoxStatus] = useState<string>(latestBoxStatuses[0]);
|
||||
|
||||
const generateFirstWeekPadding = (month: number) => {
|
||||
const firstDay = new Date(new Date().getFullYear(), month, 1).getDay();
|
||||
@@ -78,9 +77,9 @@ export default function Calendar() {
|
||||
})}
|
||||
<br />
|
||||
{matchingBox.pickup_date ? (
|
||||
<span className={styles.statusText}>Jako</span>
|
||||
) : (
|
||||
<span className={styles.statusText}>Haettu</span>
|
||||
) : (
|
||||
<span className={styles.statusText}>Jako</span>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
@@ -98,6 +97,77 @@ export default function Calendar() {
|
||||
return calendarCells;
|
||||
}
|
||||
|
||||
const renderLatestBoxButton = () => {
|
||||
switch (latestBoxStatus) {
|
||||
case "no boxes":
|
||||
return <button
|
||||
className={styles.latestBoxNoBoxesBtn}
|
||||
disabled
|
||||
>
|
||||
Ei laatikoita tässä kuussa
|
||||
</button>;
|
||||
case "picked up":
|
||||
return <button
|
||||
className={styles.latestBoxPickedUpBtn}
|
||||
disabled
|
||||
>
|
||||
Uusin laatikko on jo haettu
|
||||
</button>;
|
||||
case "delivered":
|
||||
return <button
|
||||
className={styles.latestBoxDeliveredBtn}
|
||||
onClick={handleLatestBoxClick}
|
||||
>
|
||||
Kuittaa uusin laatikko haetuksi
|
||||
</button>;
|
||||
default:
|
||||
return <span className={styles.latestBoxStatus}>Virhe</span>;
|
||||
}
|
||||
}
|
||||
|
||||
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<HTMLButtonElement>) => {
|
||||
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 (
|
||||
<>
|
||||
<h2 className={styles.monthTitle}>
|
||||
@@ -111,6 +181,9 @@ export default function Calendar() {
|
||||
Seuraava kuukausi >
|
||||
</button>
|
||||
</div>
|
||||
<div className={styles.latestBoxBtnContainer}>
|
||||
{renderLatestBoxButton()}
|
||||
</div>
|
||||
<div className={styles.calendarGrid}>
|
||||
<span>Ma</span>
|
||||
<span>Ti</span>
|
||||
@@ -122,6 +195,31 @@ export default function Calendar() {
|
||||
{firstWeekPadding}
|
||||
{calendarCells}
|
||||
</div>
|
||||
<Modal
|
||||
open={showLatestBoxModal}
|
||||
onClose={() => setShowLatestBoxModal(false)}
|
||||
className={styles.latestBoxModal}
|
||||
>
|
||||
<div className={styles.latestBoxModalContent}>
|
||||
<h2>Viimeisin laatikko</h2>
|
||||
<p>Oletko varma, että haluat kuitata viimeisimmän laatikon haetuksi?</p>
|
||||
<button
|
||||
className={styles.latestBoxConfirmButton}
|
||||
onClick={() => {
|
||||
markLatestBoxPickedUp();
|
||||
setShowLatestBoxModal(false);
|
||||
}}
|
||||
>
|
||||
Kyllä
|
||||
</button>
|
||||
<button
|
||||
className={styles.latestBoxCancelButton}
|
||||
onClick={() => setShowLatestBoxModal(false)}
|
||||
>
|
||||
Peruuta
|
||||
</button>
|
||||
</div>
|
||||
</Modal>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user