import { Dropable } from "@/tiny-dnd/dropable"; import { produce } from "immer"; import { Fragment } from "react"; import { days, employees, Task, TaskCard } from "./task-card"; import { useTasksContext } from "./useTasksContext"; export function PlanExample() { const { setTasks, tasks } = useTasksContext(); async function handleTaskDrop(taskId: string, employeeId: string | null, day: string) { const snapShot = tasks.find((task) => task.id === taskId); if (!snapShot) return; // Optimistic update setTasks((prev) => produce(prev, (draft: Array) => { const idx = tasks.findIndex((task) => task.id === taskId); if (idx === -1) return draft; draft[idx].employeeId = employeeId ? [employeeId] : []; draft[idx].day = [day]; draft[idx].loading = true; return draft; }), ); try { await simulateBackendCall(); } catch (err) { setTasks((prev) => produce(prev, (draft: Array) => { const idx = tasks.findIndex((task) => task.id === taskId); if (idx === -1) return draft; draft[idx] = snapShot; return draft; }), ); } finally { setTasks((prev) => produce(prev, (draft: Array) => { const idx = tasks.findIndex((task) => task.id === taskId); if (idx === -1) return draft; draft[idx].loading = false; return draft; }), ); } } return (
{employees.map((employee) => (
{employee.name}
{days.map((day) => { return ( handleTaskDrop(taskId, employee.id, day)} onDropData={(data) => console.log("dropped data", data)} >

{day}

{tasks .filter((task) => task.employeeId.includes(employee.id)) .filter((task) => task.day.includes(day)) .map((task) => ( { // TODO: Get event here, so we can stop propogation, if we want that console.log(`Dropped ${val} on ${existing}`); }} onDropData={(data, existingData) => { console.log("dropped data", data, existingData); }} /> ))}
); })}
))}
); } const simulateBackendCall = (): Promise => { return new Promise((resolve, reject) => { setTimeout(() => { const randomNumber = Math.floor(Math.random() * 10) + 1; if (randomNumber > 1) { resolve(); } else { reject(new Error("Backend borked")); } }, 3000); }); };