// import axios from "axios"; import React, { useEffect, useState } from "react"; import toast from "react-hot-toast"; import { useRouter } from "next/router"; import Link from "next/link"; import axiosInstance from '../../src/axiosSecure'; //import { getDate } from "date-fns"; import { monthNamesBG, GetTimeFormat, GetDateFormat } from "../../src/helpers/const" import PublisherSearchBox from './PublisherSearchBox'; import AvailabilityList from "../availability/AvailabilityList"; import ShiftsList from "../publisher/ShiftsList.tsx"; import common from "../../src/helpers/common"; import ProtectedRoute from '../../components/protectedRoute'; import { UserRole } from "@prisma/client"; // import { Tabs, List } from 'tw-elements' // model Publisher { // id String @id @default(cuid()) // firstName String // lastName String // email String @unique // phone String? // isactive Boolean @default(true) // isImported Boolean @default(false) // age Int? // availabilities Availability[] // assignments Assignment[] // emailVerified DateTime? // accounts Account[] // sessions Session[] // role UserRole @default(USER) // desiredShiftsPerMonth Int @default(4) // isMale Boolean @default(true) // isNameForeign Boolean @default(false) // familyHeadId String? // Optional familyHeadId for each family member // familyHead Publisher? @relation("FamilyMember", fields: [familyHeadId], references: [id]) // familyMembers Publisher[] @relation("FamilyMember") // type PublisherType @default(Publisher) // Town String? // Comments String? // } Array.prototype.groupBy = function (prop) { return this.reduce(function (groups, item) { const val = item[prop] groups[val] = groups[val] || [] groups[val].push(item) return groups }, {}) } export default function PublisherForm({ item, me }) { const router = useRouter(); console.log("init PublisherForm: "); const urls = { apiUrl: "/api/data/publishers/", indexUrl: "/cart/publishers" } const [helpers, setHelper] = useState(null); const fetchModules = async () => { const h = (await import("../../src/helpers/const.js")).default; //console.log("fetchModules: " + JSON.stringify(h)); setHelper(h); } useEffect(() => { fetchModules(); }, []); const [publisher, set] = useState(item || { isactive: true, }); const handleChange = ({ target }) => { if (target.type === "checkbox") { set({ ...publisher, [target.name]: target.checked }); } else if (target.type === "number") { set({ ...publisher, [target.name]: parseInt(target.value) }); } else { set({ ...publisher, [target.name]: target.value }); } if (item?.firstName) { publisher.isMale = item.firstName && item.firstName.endsWith('а') ? false : true; } } const handleParentSelection = (head) => { //setSelectedParent(parent); // Update the publisher state with the selected publisher's ID console.log("handleParentSelection: " + JSON.stringify(head)); set({ ...publisher, familyHeadId: head.id }); // Create a new object excluding the familyHeadId property }; const handleSubmit = async (e) => { router.query.id = router.query.id || ""; console.log("handleSubmit: " + JSON.stringify(publisher)); console.log("urls.apiUrl + router.query.id: " + urls.apiUrl + router.query.id) e.preventDefault(); //remove availabilities, assignments from publisher publisher.availabilities = undefined; publisher.assignments = undefined; let { familyHeadId, userId, ...rest } = publisher; // Set the familyHead relation based on the selected head const familyHeadRelation = familyHeadId ? { connect: { id: familyHeadId } } : { disconnect: true }; const userRel = userId ? { connect: { id: userId } } : { disconnect: true }; // Return the new state without familyHeadId and with the correct familyHead relation rest = { ...rest, familyHead: familyHeadRelation, user: userRel }; try { if (router.query?.id) { await axiosInstance.put(urls.apiUrl + router.query.id, { ...rest, }); toast.success("Task Updated", { position: "bottom-center", }); } else { await axiosInstance.post(urls.apiUrl, publisher); toast.success("Task Saved", { position: "bottom-center", }); } router.push(urls.indexUrl); } catch (error) { console.log(JSON.stringify(error)); //toast.error(error.response.data.message); } }; const handleDelete = async (e) => { e.preventDefault(); try { //console.log("deleting publisher id = ", router.query.id, "; url=" + urls.apiUrl + router.query.id); await axiosInstance.delete(urls.apiUrl + router.query.id); toast.success("Записът изтрит", { position: "bottom-center", }); router.push(urls.indexUrl); } catch (error) { console.log(JSON.stringify(error)); toast.error(error.response.data.message); } }; let formTitle; me = common.parseBool(me); if (me) { formTitle = "Моят профил / Настройки"; } else if (router.query?.id) { formTitle = "Редактирай вестител"; // "Edit Publisher" } else { formTitle = "Създай вестител"; // "Create Publisher" } return ( <>