/* eslint-disable @typescript-eslint/no-unused-vars */ /* eslint-disable react-hooks/exhaustive-deps */ import { BaseBox, ButtonCustom, CenterCustom, LoaderCustom, StackCustom, TextCustom, ViewWrapper, } from "@/components"; import { AccentColor, MainColor } from "@/constants/color-palet"; import { useAuth } from "@/hooks/use-auth"; import { apiEventConfirmationAction, apiEventGetConfirmation, apiEventJoin, } from "@/service/api-client/api-event"; import { Ionicons } from "@expo/vector-icons"; import dayjs from "dayjs"; import isBetween from "dayjs/plugin/isBetween"; import { Redirect, router, Stack, useFocusEffect, useLocalSearchParams, } from "expo-router"; import React, { useCallback, useState } from "react"; import { View } from "react-native"; import Toast from "react-native-toast-message"; // Extend Day.js dengan plugin isBetween dayjs.extend(isBetween); interface DataEvent { id: string; title: string; tanggal: Date; tanggalSelesai: Date; lokasi: string; Author: { id: string; username: string; Profile: { id: string; name: string; }; }; } export default function UserEventConfirmation() { const { token } = useAuth(); const { id, userId: authorId } = useLocalSearchParams(); const { user } = useAuth(); const [data, setData] = useState(null); const [peserta, setPeserta] = useState(null); const [konfirmasi, setKonfirmasi] = useState(null); useFocusEffect( useCallback(() => { checkTokenAndDataParticipants() || console.log("Token is null"); }, [token, id, user?.id]) ); const checkTokenAndDataParticipants = async () => { if (!token) { return ; } try { const response = await apiEventGetConfirmation({ id: id as string, userId: user?.id as string, }); if (response.success) { setData(response.data?.dataEvent); setPeserta(response.data?.peserta); setKonfirmasi(response.data?.kehadiran); } } catch (error) { console.log("[ERROR CONFIRMATION]", error); } }; const handlerReturn = () => { const now = dayjs(); // asumsi: UTC, sesuai dengan API // --- [1] Loading & Data tidak ditemukan --- if (data === undefined || data === null) { if (peserta === null && konfirmasi === null) { return ; } return ( Data Tidak Ditemukan ); } // --- [2] Ambil waktu event dari `data` --- const eventStart = dayjs(data.tanggal); const eventEnd = dayjs(data.tanggalSelesai); // --- [3] Definisikan jendela konfirmasi: 1 jam sebelum mulai → 1 jam setelah selesai --- const confirmationStart = eventStart.subtract(1, "hour"); const confirmationEnd = eventEnd.add(1, "hour"); const isWithinConfirmationWindow = now.isBetween( confirmationStart, confirmationEnd, null, "[]" ); // --- [4] Status waktu event (untuk pesan UI) --- const isBeforeEvent = now.isBefore(eventStart); const isAfterEvent = now.isAfter(eventEnd); const isDuringEvent = !isBeforeEvent && !isAfterEvent; // --- [5] Handle berdasarkan waktu dan status peserta/konfirmasi --- // 🟢 Acara sudah selesai if (isAfterEvent) { if (peserta === false) { return ( ); } return ( ); } // 🔵 Acara belum mulai & belum terdaftar if (isBeforeEvent) { if (peserta === false) { return ( ); } // Peserta sudah daftar → cek apakah sudah boleh konfirmasi if (isWithinConfirmationWindow && peserta === true) { if (konfirmasi === false) { return ( ); } return ( ); } return ( ); } // 🟡 Acara sedang berlangsung & belum terdaftar if (isDuringEvent) { if (peserta === false) { return ( ); } if (peserta === true) { if (isWithinConfirmationWindow) { if (konfirmasi === false) { return ( ); } return ( ); } // Ini sangat jarang terjadi selama event berlangsung, tapi aman return ( ); } } // 🛑 Fallback aman return ( ); }; return ( <> ( // // router.navigate("/(application)/(user)/event/create") // } // /> // ), }} /> {handlerReturn()} ); } const TamplateBox = ({ data, children, }: { data: DataEvent; children: React.ReactNode; }) => { return ( <> {data?.title} {dayjs(data?.tanggal).format("DD MMM YYYY: HH:mm")} {" "} -{" "} {dayjs(data?.tanggalSelesai).format("DD MMM YYYY: HH:mm")} {children} ); }; const TamplateText = ({ text }: { text: React.ReactNode }) => { return ( <> {text} ); }; type BackToOtherPathProps = | { path: "home" | "beranda-event"; id?: never; isAfterEvent?: never } | { path: "event"; id: string; isAfterEvent: boolean }; const BackToOtherPath = ({ path, id, isAfterEvent }: BackToOtherPathProps) => { return ( <> {path === "home" ? ( { router.replace("/(application)/home"); }} > Home ) : ( { router.replace("/(application)/home"); }} > Home { if (path === "event") { if (isAfterEvent) { router.push(`/(application)/(user)/event/${id}/history`); } else { router.push(`/(application)/(user)/event/${id}/publish`); } } else if (path === "beranda-event") { router.push(`/(application)/(user)/event`); } else { console.log("[PATH]", path); } }} > Lihat {path === "event" ? "Event" : "Beranda Event"} )} ); }; // 🔵 Acara belum mulai & belum terdaftar const NotStarted_And_UserNotParticipan = ({ id, userId, data, }: { id: string; userId: string; data: DataEvent; }) => { const [isLoading, setIsLoading] = useState(false); const handlerJoinEvent = async () => { try { setIsLoading(true); const response = await apiEventJoin({ id: id as string, userId: userId as string, }); if (!response.success) { Toast.show({ type: "error", text1: "Anda gagal join", }); return; } Toast.show({ type: "success", text1: "Anda berhasil join", }); router.navigate(`/(application)/(user)/event/${id}/publish`); } catch (error) { console.log("[ERROR JOIN EVENT]", error); } finally { setIsLoading(false); } }; return ( <> Join ); }; // 🟡 ZONA ACARA BERLANGSUNG // Acara sedang berlangsung & belum terdaftar & user harus join dan konfirmasi const UserNotParticipan_And_DuringEvent = ({ id, userId, data, }: { id: string; userId: string; data: DataEvent; }) => { const [isLoading, setIsLoading] = useState(false); const handlerSubmit = async () => { try { setIsLoading(true); const response = await apiEventConfirmationAction({ id: id as string, userId: userId as string, category: "join_and_confirm", }); if (!response.success) { Toast.show({ type: "error", text1: "Anda gagal join & konfirmasi", }); return; } Toast.show({ type: "success", text1: "Anda berhasil join & konfirmasi", }); router.navigate(`/(application)/(user)/event/${id}/publish`); } catch (error) { console.log("[ERROR JOIN & CONFIRMATION EVENT]", error); } finally { setIsLoading(false); } }; return ( <> handlerSubmit()} isLoading={isLoading}> Join & Konfirmasi ); }; // 🟡 ZONA ACARA BERLANGSUN // User sudah terdaftar & Event sedang berlangsung & user harus konfirmasi const UserParticipan_And_DuringEvent = ({ id, userId, data, }: { id: string; userId: string; data: DataEvent; }) => { const [isLoading, setIsLoading] = useState(false); const handlerSubmit = async () => { try { setIsLoading(true); const response = await apiEventConfirmationAction({ id: id as string, userId: userId as string, category: "confirmation", }); if (!response.success) { Toast.show({ type: "error", text1: "Anda gagal konfirmasi", }); return; } Toast.show({ type: "success", text1: "Anda berhasil konfirmasi", }); router.navigate(`/(application)/(user)/event/${id}/publish`); } catch (error) { console.log("[ERROR JOIN & CONFIRMATION EVENT]", error); } finally { setIsLoading(false); } }; return ( <> handlerSubmit()} isLoading={isLoading}> Konfirmasi ); };