Fix latest box pickup logic
This commit is contained in:
@@ -8,13 +8,13 @@ import { markBoxDelivered, markBoxPickedUp } from "../api";
|
|||||||
import { Modal } from "@mui/material";
|
import { Modal } from "@mui/material";
|
||||||
|
|
||||||
export default function Calendar() {
|
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 [customerData, setCustomerData] = useAtom(customerState);
|
||||||
const [selectedMonth, setSelectedMonth] = useState(new Date().getMonth());
|
const [selectedMonth, setSelectedMonth] = useState(new Date().getMonth());
|
||||||
const [calendarCells, setCalendarCells] = useState<JSX.Element[]>([]);
|
const [calendarCells, setCalendarCells] = useState<JSX.Element[]>([]);
|
||||||
const [firstWeekPadding, setFirstWeekPadding] = useState<JSX.Element[]>([]);
|
const [firstWeekPadding, setFirstWeekPadding] = useState<JSX.Element[]>([]);
|
||||||
const [showLatestBoxModal, setShowLatestBoxModal] = useState(false);
|
const [showLatestBoxModal, setShowLatestBoxModal] = useState(false);
|
||||||
const [latestBoxStatus, setLatestBoxStatus] = useState<string>(latestBoxStatuses[0]);
|
const [latestBoxStatus, setLatestBoxStatus] = useState<string>(boxStatuses[0]);
|
||||||
|
|
||||||
const generateFirstWeekPadding = (month: number) => {
|
const generateFirstWeekPadding = (month: number) => {
|
||||||
const firstDay = new Date(new Date().getFullYear(), month, 1).getDay();
|
const firstDay = new Date(new Date().getFullYear(), month, 1).getDay();
|
||||||
@@ -118,7 +118,7 @@ export default function Calendar() {
|
|||||||
className={styles.latestBoxDeliveredBtn}
|
className={styles.latestBoxDeliveredBtn}
|
||||||
onClick={handleLatestBoxClick}
|
onClick={handleLatestBoxClick}
|
||||||
>
|
>
|
||||||
Kuittaa uusin laatikko haetuksi
|
Kuittaa viimeisin laatikko haetuksi
|
||||||
</button>;
|
</button>;
|
||||||
default:
|
default:
|
||||||
return <span className={styles.latestBoxStatus}>Virhe</span>;
|
return <span className={styles.latestBoxStatus}>Virhe</span>;
|
||||||
@@ -130,17 +130,17 @@ export default function Calendar() {
|
|||||||
setFirstWeekPadding(generateFirstWeekPadding(selectedMonth));
|
setFirstWeekPadding(generateFirstWeekPadding(selectedMonth));
|
||||||
setLatestBoxStatus(() => {
|
setLatestBoxStatus(() => {
|
||||||
if (!customerData.boxes || customerData.boxes.length === 0) {
|
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);
|
const monthBoxes = customerData.boxes.filter(box => new Date(box.delivery_date).getMonth() === selectedMonth);
|
||||||
if (!monthBoxes || monthBoxes.length === 0) {
|
const deliveredBoxes = customerData.boxes.filter(box => new Date(box.delivery_date).getMonth() === selectedMonth && !box.pickup_date);
|
||||||
return latestBoxStatuses[0]; // no boxes
|
if (monthBoxes.length === 0) {
|
||||||
|
return boxStatuses[0]; // no boxes
|
||||||
}
|
}
|
||||||
const latestBox = monthBoxes[monthBoxes.length - 1];
|
if (!deliveredBoxes || deliveredBoxes.length === 0) {
|
||||||
if (latestBox.pickup_date) {
|
return boxStatuses[1]; // all boxes picked up
|
||||||
return latestBoxStatuses[1]; // picked up
|
|
||||||
}
|
}
|
||||||
return latestBoxStatuses[2]; // delivered
|
return boxStatuses[2]; // delivered
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}, [selectedMonth, customerData.boxes]);
|
}, [selectedMonth, customerData.boxes]);
|
||||||
@@ -151,11 +151,9 @@ export default function Calendar() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const markLatestBoxPickedUp = () => {
|
const markLatestBoxPickedUp = () => {
|
||||||
const monthBoxes = customerData.boxes.filter(box => new Date(box.delivery_date).getMonth() === selectedMonth);
|
const deliveredBoxes = customerData.boxes.filter(box => new Date(box.delivery_date).getMonth() === selectedMonth && !box.pickup_date);
|
||||||
if (monthBoxes.length === 0) return;
|
if (deliveredBoxes.length === 0) return;
|
||||||
console.log("Marking latest box as picked up");
|
const latestBox = deliveredBoxes[0];
|
||||||
console.log("Month boxes:", monthBoxes);
|
|
||||||
const latestBox = monthBoxes[monthBoxes.length - 1];
|
|
||||||
markBoxPickedUp(latestBox.id).then((updatedBox) => {
|
markBoxPickedUp(latestBox.id).then((updatedBox) => {
|
||||||
setCustomerData((prev) => ({
|
setCustomerData((prev) => ({
|
||||||
...prev,
|
...prev,
|
||||||
|
|||||||
Reference in New Issue
Block a user