// import axios from "axios"; import React, { use, 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 PwaManager from "../PwaManager"; import common from "../../src/helpers/common"; import ProtectedRoute from '../../components/protectedRoute'; import { monthNamesBG, GetTimeFormat, GetDateFormat } from "../../src/helpers/const" import PublisherSearchBox from './PublisherSearchBox'; import AvailabilityList from "../availability/AvailabilityList"; import ShiftsList from "../publisher/ShiftsList.tsx"; import ConfirmationModal from "../ConfirmationModal"; import { UserRole } from "@prisma/client"; import { useSession } from "next-auth/react" // import { Tabs, List } from 'tw-elements' 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(); const { data: session } = useSession() const [congregations, setCongregations] = useState([]); const urls = { apiUrl: "/api/data/publishers/", congregationsUrl: "/api/data/congregations", indexUrl: session?.user?.role == UserRole.ADMIN ? "/cart/publishers" : "/dash" } console.log("urls.indexUrl: " + urls.indexUrl); const [helpers, setHelper] = useState(null); const [isModalOpen, setIsModalOpen] = useState(false); const fetchModules = async () => { const h = (await import("../../src/helpers/const.js")).default; //console.log("fetchModules: " + JSON.stringify(h)); setHelper(h); const response = await axiosInstance.get(urls.congregationsUrl); setCongregations(response.data); } 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, congregationId, ...rest } = publisher; // Set the familyHead relation based on the selected head const familyHeadRelation = familyHeadId ? { connect: { id: familyHeadId } } : null; const userRel = userId ? { connect: { id: userId } } : null; const congregationRel = congregationId ? { connect: { id: parseInt(congregationId) } } : null; // Return the new state without familyHeadId and with the correct familyHead relation rest = { ...rest, ...(familyHeadRelation ? { familyHead: familyHeadRelation } : {}), ...(userRel ? { user: userRel } : {}), ...(congregationRel ? { congregation: congregationRel } : {}), }; 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, rest); 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 ( <>