Logic to change box as picked up
This commit is contained in:
24
app/api.ts
24
app/api.ts
@@ -1,3 +1,5 @@
|
|||||||
|
import { Box, CustomerData } from "./types";
|
||||||
|
|
||||||
export const fetchCustomerData = async (id: string) => {
|
export const fetchCustomerData = async (id: string) => {
|
||||||
const baseUrl = "http://localhost:9090";
|
const baseUrl = "http://localhost:9090";
|
||||||
|
|
||||||
@@ -15,3 +17,25 @@ export const fetchCustomerData = async (id: string) => {
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const markBoxPickedUp = 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: new Date().toISOString().split('.')[0] }),
|
||||||
|
});
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
|
}
|
||||||
|
const updatedBox = await response.json();
|
||||||
|
return updatedBox;
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,10 +35,14 @@
|
|||||||
border-radius: 0.5rem;
|
border-radius: 0.5rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.deliveryDateCell {
|
.deliveredCell {
|
||||||
background-color: #62ed8e;
|
background-color: #62ed8e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.pickedUpCell {
|
||||||
|
background-color: #6287ed;
|
||||||
|
}
|
||||||
|
|
||||||
.firstWeekPaddingCell {
|
.firstWeekPaddingCell {
|
||||||
height: 3rem;
|
height: 3rem;
|
||||||
width: 3rem;
|
width: 3rem;
|
||||||
|
|||||||
@@ -4,50 +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";
|
||||||
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) => (
|
|
||||||
<div key={index} className={`${styles.firstWeekPaddingCell}`}>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
));
|
|
||||||
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 isDeliveryDate = boxes.find(box => {
|
|
||||||
const boxDate = new Date(box.delivery_date);
|
|
||||||
return boxDate.getDate() === day && boxDate.getMonth() === month && boxDate.getFullYear() === date.getFullYear();
|
|
||||||
});
|
|
||||||
|
|
||||||
if (isDeliveryDate) {
|
|
||||||
return (
|
|
||||||
<div key={index} className={`${styles.calendarCell} ${styles.deliveryDateCell}`}>
|
|
||||||
{date.toLocaleDateString("fi-FI", {
|
|
||||||
day: "2-digit",
|
|
||||||
month: "2-digit",
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div key={index} className={`${styles.calendarCell}`}>
|
|
||||||
{date.toLocaleDateString("fi-FI", {
|
|
||||||
day: "2-digit",
|
|
||||||
month: "2-digit",
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
});
|
|
||||||
return calendarCells;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function Calendar() {
|
export default function Calendar() {
|
||||||
const [customerData, setCustomerData] = useAtom(customerState);
|
const [customerData, setCustomerData] = useAtom(customerState);
|
||||||
@@ -60,6 +17,68 @@ export default function Calendar() {
|
|||||||
setFirstWeekPadding(generateFirstWeekPadding(selectedMonth));
|
setFirstWeekPadding(generateFirstWeekPadding(selectedMonth));
|
||||||
}, [selectedMonth]);
|
}, [selectedMonth]);
|
||||||
|
|
||||||
|
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) => (
|
||||||
|
<div key={index} className={`${styles.firstWeekPaddingCell}`}>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
));
|
||||||
|
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 (
|
||||||
|
<div
|
||||||
|
key={index}
|
||||||
|
className={`${styles.calendarCell} ${matchingBox.pickup_date ? styles.pickedUpCell : styles.deliveredCell}`}
|
||||||
|
onClick={() => {
|
||||||
|
console.log(`Box picked up for date: ${date.toLocaleDateString("fi-FI")}`);
|
||||||
|
|
||||||
|
markBoxPickedUp(matchingBox.id).then((updatedBox) => {
|
||||||
|
console.log(`Box with ID ${updatedBox.id} marked as picked up.`);
|
||||||
|
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",
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div key={index} className={`${styles.calendarCell}`}>
|
||||||
|
{date.toLocaleDateString("fi-FI", {
|
||||||
|
day: "2-digit",
|
||||||
|
month: "2-digit",
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
return calendarCells;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h2 className={styles.monthTitle}>
|
<h2 className={styles.monthTitle}>
|
||||||
|
|||||||
Reference in New Issue
Block a user