Allow unpicking box, fix calendar render
This commit is contained in:
22
app/api.ts
22
app/api.ts
@@ -38,4 +38,26 @@ export const markBoxPickedUp = async (id: string) => {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const markBoxDelivered = async (id: string) => {
|
||||||
|
const baseUrl = "http://localhost:9090";
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${baseUrl}/loota/`, {
|
||||||
|
method: 'POST',
|
||||||
|
mode: "cors",
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ id: id, pickup_date: null }),
|
||||||
|
});
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
|
}
|
||||||
|
const updatedBox = await response.json();
|
||||||
|
return updatedBox;
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,7 @@ import { getMonthName, daysInMonth } from "../utils/dateUtils";
|
|||||||
import { customerState } from "../store";
|
import { customerState } from "../store";
|
||||||
import { Box } from "../types";
|
import { Box } from "../types";
|
||||||
import { useAtom } from "jotai";
|
import { useAtom } from "jotai";
|
||||||
import { markBoxPickedUp } from "../api";
|
import { markBoxDelivered, markBoxPickedUp } from "../api";
|
||||||
|
|
||||||
export default function Calendar() {
|
export default function Calendar() {
|
||||||
const [customerData, setCustomerData] = useAtom(customerState);
|
const [customerData, setCustomerData] = useAtom(customerState);
|
||||||
@@ -15,7 +15,7 @@ export default function Calendar() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setCalendarCells(generateCalendarCells(selectedMonth, customerData.boxes));
|
setCalendarCells(generateCalendarCells(selectedMonth, customerData.boxes));
|
||||||
setFirstWeekPadding(generateFirstWeekPadding(selectedMonth));
|
setFirstWeekPadding(generateFirstWeekPadding(selectedMonth));
|
||||||
}, [selectedMonth]);
|
}, [selectedMonth, customerData.boxes]);
|
||||||
|
|
||||||
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();
|
||||||
@@ -44,20 +44,33 @@ export default function Calendar() {
|
|||||||
key={index}
|
key={index}
|
||||||
className={`${styles.calendarCell} ${matchingBox.pickup_date ? styles.pickedUpCell : styles.deliveredCell}`}
|
className={`${styles.calendarCell} ${matchingBox.pickup_date ? styles.pickedUpCell : styles.deliveredCell}`}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
console.log(`Box picked up for date: ${date.toLocaleDateString("fi-FI")}`);
|
if (matchingBox.pickup_date) {
|
||||||
|
// If the box is already picked up, mark it as delivered
|
||||||
markBoxPickedUp(matchingBox.id).then((updatedBox) => {
|
markBoxDelivered(matchingBox.id).then((updatedBox) => {
|
||||||
console.log(`Box with ID ${updatedBox.id} marked as picked up.`);
|
setCustomerData((prev) => ({
|
||||||
setCustomerData((prev) => ({
|
...prev,
|
||||||
...prev,
|
boxes: prev.boxes.map(box =>
|
||||||
boxes: prev.boxes.map(box =>
|
box.id === updatedBox.id ? { ...box, pickup_date: null } : box
|
||||||
box.id === updatedBox.id ? { ...box, pickup_date: updatedBox.pickup_date } : 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);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
).catch((error) => {
|
|
||||||
console.error("Error marking box as picked up:", error);
|
|
||||||
})
|
|
||||||
}}>
|
}}>
|
||||||
{date.toLocaleDateString("fi-FI", {
|
{date.toLocaleDateString("fi-FI", {
|
||||||
day: "2-digit",
|
day: "2-digit",
|
||||||
|
|||||||
@@ -7,5 +7,5 @@ export type CustomerData = {
|
|||||||
export type Box = {
|
export type Box = {
|
||||||
id: string;
|
id: string;
|
||||||
delivery_date: Date;
|
delivery_date: Date;
|
||||||
pickup_date: Date;
|
pickup_date: Date | null;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user