import { BoxButtonOnFooter, ButtonCustom, DrawerCustom, LoaderCustom, NewWrapper, Spacing, StackCustom, TextAreaCustom, TextCustom, TextInputCustom, ViewWrapper, } from "@/components"; import CustomSkeleton from "@/components/_ShareComponent/SkeletonCustom"; import AlertWarning from "@/components/Alert/AlertWarning"; import { MainColor } from "@/constants/color-palet"; import { useAuth } from "@/hooks/use-auth"; import { usePagination } from "@/hooks/use-pagination"; import { createPaginationComponents } from "@/helpers/paginationHelpers"; import Forum_CommentarBoxSection from "@/screens/Forum/CommentarBoxSection"; import Forum_BoxDetailSection from "@/screens/Forum/DiscussionBoxSection"; import Forum_MenuDrawerBerandaSection from "@/screens/Forum/MenuDrawerSection.tsx/MenuBeranda"; import Forum_MenuDrawerCommentar from "@/screens/Forum/MenuDrawerSection.tsx/MenuCommentar"; import { apiForumCreateComment, apiForumGetComment, apiForumGetOne, apiForumUpdateStatus, } from "@/service/api-client/api-forum"; import { TypeForum_CommentProps } from "@/types/type-forum"; import { censorText, isBadContent } from "@/utils/badWordsIndonesia"; import { useFocusEffect, useLocalSearchParams } from "expo-router"; import _ from "lodash"; import { useCallback, useEffect, useState } from "react"; import { RefreshControl } from "react-native"; import ListSkeletonComponent from "@/components/_ShareComponent/ListSkeletonComponent"; export default function DetailForum2() { const { id } = useLocalSearchParams(); const { user } = useAuth(); const [openDrawer, setOpenDrawer] = useState(false); const [data, setData] = useState(null); const [isLoadingComment, setLoadingComment] = useState(false); // Status const [status, setStatus] = useState(""); const [text, setText] = useState(""); const [authorId, setAuthorId] = useState(""); const [dataId, setDataId] = useState(""); // Comentar const [openDrawerCommentar, setOpenDrawerCommentar] = useState(false); const [commentId, setCommentId] = useState(""); const [commentAuthorId, setCommentAuthorId] = useState(""); // Initialize pagination for comments const commentPagination = usePagination({ fetchFunction: async (page) => { return await apiForumGetComment({ id: id as string, page: String(page), // API expects string }); }, pageSize: 5, dependencies: [id], onError: (error) => console.error("[ERROR] Fetch forum comment:", error), }); useFocusEffect( useCallback(() => { setTimeout(() => { onLoadData(id as string); }, 3000); }, [id]), ); useEffect(() => { // Reset and load first page of comments when id changes commentPagination.reset(); commentPagination.onRefresh(); }, [id]); const onLoadData = async (id: string) => { try { const response = await apiForumGetOne({ id }); setData(response.data); } catch (error) { console.log("[ERROR]", error); } }; // Update Status const handlerUpdateStatus = async (value: any) => { try { const response = await apiForumUpdateStatus({ id: id as string, data: value, }); if (response.success) { setStatus(response.data); setData({ ...data, ForumMaster_StatusPosting: { status: response.data, }, }); } } catch (error) { console.log("[ERROR]", error); } }; // Create Commentar const handlerCreateCommentar = async () => { const cencorContent = censorText(text); const newData = { comment: cencorContent, authorId: user?.id, }; try { setLoadingComment(true); const response = await apiForumCreateComment({ id: id as string, data: newData, }); if (response.success) { setText(""); const newComment = { id: response.data.id, isActive: response.data.isActive, komentar: response.data.komentar, createdAt: response.data.createdAt, authorId: response.data.authorId, Author: response.data.Author, }; // Add new comment to the top of the list commentPagination.setListData((prev) => [newComment, ...prev]); setData({ ...data, count: data.count + 1, }); } } catch (error) { console.log("[ERROR]", error); } finally { setLoadingComment(false); } }; const headerComponent = () => // Box Posting !data ? ( ) : ( <> {/* Area Posting */} { setOpenDrawer(true); setStatus(data.ForumMaster_StatusPosting?.status); setAuthorId(data.Author?.id); setDataId(data.id); }} /> {/* Area Commentar */} {data?.ForumMaster_StatusPosting?.status === "Open" && ( <> { handlerCreateCommentar(); }} > Balas )} ); // Render individual comment item const renderCommentItem = ({ item }: { item: TypeForum_CommentProps }) =>( { setCommentId(value.setCommentId); setOpenDrawerCommentar(value.setOpenDrawer); setCommentAuthorId(value.setCommentAuthorId); }} /> ) // !data || !commentPagination.listData ? ( // // // // ) : ( // { // setCommentId(value.setCommentId); // setOpenDrawerCommentar(value.setOpenDrawer); // setCommentAuthorId(value.setCommentAuthorId); // }} // /> // ); // Generate pagination components using helper const { ListEmptyComponent, ListFooterComponent } = createPaginationComponents({ loading: commentPagination.loading, refreshing: commentPagination.refreshing, listData: commentPagination.listData, isInitialLoad: commentPagination.isInitialLoad, emptyMessage: "Tidak ada komentar", skeletonCount: 3, skeletonHeight: 120, }); return ( <> } onEndReached={commentPagination.loadMore} ListHeaderComponent={ <> {/* Komentar : */} {headerComponent()} } ListFooterComponent={ListFooterComponent} ListEmptyComponent={ListEmptyComponent} /> {/* Posting Drawer */} setOpenDrawer(false)} > { setOpenDrawer(false); }} authorId={authorId} handlerUpdateStatus={(value: any) => { handlerUpdateStatus(value); }} /> {/* Commentar Drawer */} setOpenDrawerCommentar(false)} > { setOpenDrawerCommentar(false); }} listComment={commentPagination.listData} setListComment={commentPagination.setListData} countComment={data?.count} setCountComment={(val: any) => { setData((prev: any) => ({ ...prev, count: val, })); }} /> ); }