import { CartEvent } from '@prisma/client'; import Link from "next/link"; import { useRouter } from "next/router"; import { useEffect, useState } from "react"; import toast from "react-hot-toast"; import axiosInstance from '../../src/axiosSecure'; import DayOfWeek from "../DayOfWeek"; import common from 'src/helpers/common'; /* model CartEvent { id Int @id @default(autoincrement()) startTime DateTime endTime DateTime shiftDuration Int shifts Shift[] dayofweek DayOfWeek isactive Boolean @default(true) }*/ interface Location { id: number; name: string; } interface IProps { item?: CartEvent; locations: Location[]; inline?: false; } export default function CartEventForm(props: IProps) { const router = useRouter(); console.log("init CartEventForm: " + JSON.stringify(props)); const urls = { apiUrl: "/api/data/cartevents/", indexUrl: "/cart/cartevents" } const [evt, setEvt] = useState(props.item || { id: router.query.id, startTime: "09:00",//8:00 endTime: "19:30",//20:00 shiftDuration: 90,//120 dayofweek: "Monday", }); //const locations = props?.locations || []; //get locations from api const [locations, setLocations] = useState(props?.locations || []); useEffect(() => { const fetchLocations = async () => { try { console.log("fetching locations"); const { data } = await axiosInstance.get("/api/data/locations"); setLocations(data); console.log(data); } catch (error) { console.error(error); } }; if (!locations.length) { fetchLocations(); } }, []); useEffect(() => { const fetch = async (id) => { try { console.log("fetching cart event from component " + router.query.id); const { data } = await axiosInstance.get(urls.apiUrl + id); data.startTime = common.formatTimeHHmm(data.startTime) data.endTime = common.formatTimeHHmm(data.endTime) setEvt(data); console.log("id:" + evt.id); //console.log(data); } catch (error) { console.error(error); } }; if (!props?.item) { setEvt(prevEvt => ({ ...prevEvt, id: router.query.id as string })); } if (evt.id) { fetch(parseInt(evt.id)); } }, [router.query.id]); const handleChange = ({ target }) => { console.log("CartEventForm.handleChange() " + target.name + " = " + target.value); if (target.type === "checkbox") { setEvt({ ...evt, [target.name]: target.checked }); } else if (target.type === "number") { console.log("setting " + target.name + " to " + parseInt(target.value)); setEvt({ ...evt, [target.name]: parseInt(target.value) }); } else { setEvt({ ...evt, [target.name]: target.value }); } console.log("CartEventForm.handleChange() " + JSON.stringify(evt)); } const handleSubmit = async (e) => { e.preventDefault(); try { const eventId = evt.id; delete evt.id; evt.startTime = common.parseTimeHHmm(evt.startTime.toString()); evt.endTime = common.parseTimeHHmm(evt.endTime.toString()); console.log("calling api @ " + urls.apiUrl + evt.id); console.log(evt); if (eventId) { //update // evt.location = { // id: evt.locationId // }; delete evt.locationId; await axiosInstance.put(urls.apiUrl + router.query.id, { ...evt, }); toast.success("Task Updated", { position: "bottom-center", }); } else { evt.locationId = parseInt(evt.locationId?.toString()); await axiosInstance.post(urls.apiUrl, evt); toast.success("Task Saved", { position: "bottom-center", }); } // if (props.inline) { // } else { router.push(urls.indexUrl); // } } catch (error) { // toast.error(error.response.data.message); console.error(error); } }; return (

{evt.id ? "Edit '" + evt.dayofweek + "'" : "Create"} Cart Event

{locations && ( )}
{!props?.inline && Cancel } {evt.id && }
) }