Fix latest box pickup logic
This commit is contained in:
@@ -8,13 +8,13 @@ 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 boxStatuses = ["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[]>([]);
|
||||
const [showLatestBoxModal, setShowLatestBoxModal] = useState(false);
|
||||
const [latestBoxStatus, setLatestBoxStatus] = useState<string>(latestBoxStatuses[0]);
|
||||
const [latestBoxStatus, setLatestBoxStatus] = useState<string>(boxStatuses[0]);
|
||||
|
||||
const generateFirstWeekPadding = (month: number) => {
|
||||
const firstDay = new Date(new Date().getFullYear(), month, 1).getDay();
|
||||
@@ -118,7 +118,7 @@ export default function Calendar() {
|
||||
className={styles.latestBoxDeliveredBtn}
|
||||
onClick={handleLatestBoxClick}
|
||||
>
|
||||
Kuittaa uusin laatikko haetuksi
|
||||
Kuittaa viimeisin laatikko haetuksi
|
||||
</button>;
|
||||
default:
|
||||
return <span className={styles.latestBoxStatus}>Virhe</span>;
|
||||
@@ -130,17 +130,17 @@ export default function Calendar() {
|
||||
setFirstWeekPadding(generateFirstWeekPadding(selectedMonth));
|
||||
setLatestBoxStatus(() => {
|
||||
if (!customerData.boxes || customerData.boxes.length === 0) {
|
||||
return latestBoxStatuses[0]; // no boxes
|
||||
return boxStatuses[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 deliveredBoxes = customerData.boxes.filter(box => new Date(box.delivery_date).getMonth() === selectedMonth && !box.pickup_date);
|
||||
if (monthBoxes.length === 0) {
|
||||
return boxStatuses[0]; // no boxes
|
||||
}
|
||||
const latestBox = monthBoxes[monthBoxes.length - 1];
|
||||
if (latestBox.pickup_date) {
|
||||
return latestBoxStatuses[1]; // picked up
|
||||
if (!deliveredBoxes || deliveredBoxes.length === 0) {
|
||||
return boxStatuses[1]; // all boxes picked up
|
||||
}
|
||||
return latestBoxStatuses[2]; // delivered
|
||||
return boxStatuses[2]; // delivered
|
||||
}
|
||||
);
|
||||
}, [selectedMonth, customerData.boxes]);
|
||||
@@ -151,11 +151,9 @@ export default function Calendar() {
|
||||
}
|
||||
|
||||
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];
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user