import axiosInstance from '../../src/axiosSecure'; import { useEffect, useState, useRef } from "react"; import { useRouter } from "next/router"; import toast from "react-hot-toast"; import Link from "next/link"; import DayOfWeek from "../DayOfWeek"; import TextEditor from "../TextEditor"; import FileUploadWithPreview from 'components/FileUploadWithPreview '; import ProtectedRoute, { serverSideAuth } from "../..//components/protectedRoute"; import { UserRole } from "@prisma/client"; const common = require('src/helpers/common'); // ------------------ LocationForm ------------------ // This component is used to create and edit locations // location model: // model Location { // id Int @id @default(autoincrement()) // name String // address String // isactive Boolean @default(true) // content String? @db.Text // cartEvents CartEvent[] // reports Report[] // backupLocationId Int? // backupLocation Location? @relation("BackupLocation", fields: [backupLocationId], references: [id]) // BackupForLocations Location[] @relation("BackupLocation") // } export default function LocationForm() { const [uploadedImages, setUploadedImages] = useState([]); const [isPreviewMode, setIsPreviewMode] = useState(false); useEffect(() => { const fetchUploadedImages = async () => { try { const response = await axiosInstance.get('/uploaded-images'); setUploadedImages(response.data.imageUrls); } catch (error) { console.error('Error fetching uploaded images:', error); } }; fetchUploadedImages(); }, []); const quillRef = useRef(null); const handleImageSelect = (e) => { const imageUrl = e.target.value; if (imageUrl && quillRef.current) { const editor = quillRef.getQuill(); const range = editor.getSelection(true); if (range) { editor.insertEmbed(range.index, 'image', imageUrl); } } }; const [content, setContent] = useState(""); const [location, set] = useState({ name: "", address: "", isactive: true, }); // const [isEdit, setIsEdit] = useState(false); const router = useRouter(); useEffect(() => { const fetchLocation = async (id) => { try { console.log("fetching location " + router.query.id); const { data } = await axiosInstance.get("/api/data/locations/" + id); set(data); setContent(data.content); } catch (error) { console.error(error); } }; if (router.query?.id) { fetchLocation(parseInt(router.query.id.toString())); } console.log("called"); }, [router.query.id]); const [locations, setLocations] = useState([]); 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(); } }, []); const handleChange = ({ target }) => { if (target.type === "checkbox") { set({ ...location, [target.name]: target.checked }); } else if (target.type === "number") { set({ ...location, [target.name]: parseInt(target.value) }); } else { set({ ...location, [target.name]: target.value }); } } const handleSubmit = async (e) => { e.preventDefault(); try { const dataToSend = { ...location, name: location.name.trim(), content: content, }; if (router.query?.id) { // UPDATE //connect backup location delete dataToSend.id; dataToSend.backupLocationId = parseInt(dataToSend.backupLocationId); // dataToSend.backupLocation = { connect: { id: location.backupLocationId } }; // delete dataToSend.backupLocationId; await axiosInstance.put("/api/data/locations/" + router.query.id, { ...dataToSend, }); toast.success("Task Updated", { position: "bottom-center", }); } else { // CREATE await axiosInstance.post("/api/data/locations", dataToSend); toast.success("Task Saved", { position: "bottom-center", }); } router.push("/cart/locations"); } catch (error) { //toast.error(error.response.data.message); } }; return ( <>
{/* Location.address */}
{/* UI for Location.isactive */}
{/* backupLocation */}
{locations && ( )}
{/* Location.content */}
{/* */} {router.query.id && ( <>
{ console.log('Uploaded image URL:', imageUrl); set(location => ({ ...location, [name]: imageUrl })); }} /> { console.log('Uploaded image URL:', imageUrl); set(location => ({ ...location, [name]: imageUrl })); }} /> { console.log('Uploaded image URL:', imageUrl); set(location => ({ ...location, [name]: imageUrl })); }} />
)}
обратно
{location.name} {location.address} {location.backupLocationName}
); }