"use client"; import React, { useEffect, useRef, useState } from "react"; import { useForm, Controller } from "react-hook-form"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Label } from "@/components/ui/label"; import { Card } from "@/components/ui/card"; import { zodResolver } from "@hookform/resolvers/zod"; import * as z from "zod"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; import { useParams, useRouter } from "next/navigation"; import { Switch } from "@/components/ui/switch"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { cn } from "@/lib/utils"; import { CalendarIcon, Plus } from "lucide-react"; import { Calendar } from "@/components/ui/calendar"; import { addDays, format, parseISO, setDate } from "date-fns"; import { DateRange } from "react-day-picker"; import TimePicker from "react-time-picker"; import "react-time-picker/dist/TimePicker.css"; import "react-clock/dist/Clock.css"; import MapHome from "@/components/maps/MapHome"; import { Textarea } from "@/components/ui/textarea"; import { error, loading } from "@/lib/swal"; import Cookies from "js-cookie"; import { detailSchedule, postSchedule } from "@/service/schedule/schedule"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Dialog, DialogContent, DialogTrigger } from "@/components/ui/dialog"; import { Link } from "@/i18n/routing"; const taskSchema = z.object({ title: z.string().min(1, { message: "Judul diperlukan" }), level: z.string().min(1, { message: "Judul diperlukan" }), name: z.string().min(1, { message: "Judul diperlukan" }), location: z.string().min(1, { message: "Judul diperlukan" }), }); interface Detail { id: number; title: string; address: string; speakerTitle: string; speakerName: string; addressLat: number; addressLong: number; } export default function FormUpdatePressRelease() { const [open, setOpen] = useState(false); const { id } = useParams() as { id: string }; console.log(id); const router = useRouter(); const MySwal = withReactContent(Swal); const [isLiveStreamingEnabled, setIsLiveStreamingEnabled] = useState(false); type TaskSchema = z.infer; const [startTime, setStartTime] = useState("08:00"); const [endTime, setEndTime] = useState("09:00"); const [date, setDate] = useState(); const [selectedTarget, setSelectedTarget] = useState("all"); const [detail, setDetail] = useState(); const [refresh, setRefresh] = useState(false); const { control, handleSubmit, setValue, formState: { errors }, } = useForm({ resolver: zodResolver(taskSchema), defaultValues: { location: "", }, }); useEffect(() => { async function initState() { if (id) { const response = await detailSchedule(id); const details = response?.data?.data; setDetail(details); if (details) { setDate({ from: parseISO(details.startDate), to: parseISO(details.endDate), }); } if (details) { setStartTime(details.startTime); setEndTime(details.endTime); } } } initState(); }, [refresh, setValue]); const handleStartTime = (e: React.ChangeEvent) => { setStartTime(e.target.value); }; const handleEndTime = (e: React.ChangeEvent) => { setEndTime(e.target.value); }; const save = async (data: TaskSchema) => { const requestData: { id?: number; title: string; address: string; speakerTitle: string; speakerName: string; startTime: string; endTime: string; addressLat: string; addressLong: string; startDate: string | null; endDate: string | null; isYoutube: boolean; scheduleTypeId: number; } = { title: data.title, address: data.location, speakerTitle: data.level, speakerName: data.name, startTime, // Start time from state endTime, // End time from state addressLat: "0.0", // Replace with actual latitude addressLong: "0.0", // Replace with actual longitude startDate: date?.from ? format(date.from, "yyyy-MM-dd") : null, endDate: date?.to ? format(date.to, "yyyy-MM-dd") : null, isYoutube: isLiveStreamingEnabled, scheduleTypeId: 3, }; // Add id property if it exists if (id) { requestData.id = parseInt(id, 10); // Ensure id is a number } console.log("Form Data Submitted:", requestData); const response = await postSchedule(requestData); if (response?.error) { error(response?.message); return false; } Cookies.set("scheduleId", response?.data?.data.id, { expires: 1, }); MySwal.fire({ title: "Sukses", text: "Data berhasil disimpan.", icon: "success", confirmButtonColor: "#3085d6", confirmButtonText: "OK", }).then(() => { router.push("/en/contributor/schedule/press-release"); }); }; const onSubmit = (data: TaskSchema) => { MySwal.fire({ title: "Simpan Data", text: "Apakah Anda yakin ingin menyimpan data ini?", icon: "warning", showCancelButton: true, cancelButtonColor: "#d33", confirmButtonColor: "#3085d6", confirmButtonText: "Simpan", }).then((result) => { if (result.isConfirmed) { save(data); } }); }; return (

Form Konferensi Pers

{detail !== undefined ? (
{/* Input Title */}
( )} /> {errors.title?.message && (

{errors.title.message}

)}

Aktifkan fitur live streaming

setIsLiveStreamingEnabled(checked) } />
{isLiveStreamingEnabled && (
( )} /> {errors.title?.message && (

{errors.title.message}

)}
)}
{/* Kirim setValue ke MapHome */} setValue("location", location)} />
(