import BorderBottomItem from "@/components/borderBottomItem"; import ButtonTab from "@/components/buttonTab"; import ImageUser from "@/components/imageNew"; import InputSearch from "@/components/inputSearch"; import LabelStatus from "@/components/labelStatus"; import SkeletonContent from "@/components/skeletonContent"; import Text from "@/components/Text"; import { ConstEnv } from "@/constants/ConstEnv"; import Styles from "@/constants/Styles"; import { apiGetDiscussion } from "@/lib/api"; import { useAuthSession } from "@/providers/AuthProvider"; import { AntDesign, Feather, Ionicons } from "@expo/vector-icons"; import { router, useLocalSearchParams } from "expo-router"; import { useEffect, useState } from "react"; import { RefreshControl, View, VirtualizedList } from "react-native"; import { useSelector } from "react-redux"; type Props = { id: string, title: string, desc: string, status: number, user_name: string, img: string, total_komentar: number, createdAt: string, isActive: boolean } export default function DiscussionDivision() { const { id, active } = useLocalSearchParams<{ id: string, active?: string }>() const [data, setData] = useState([]) const { token, decryptToken } = useAuthSession() const [search, setSearch] = useState('') const update = useSelector((state: any) => state.discussionUpdate) const [loading, setLoading] = useState(true) const arrSkeleton = Array.from({ length: 5 }) const [page, setPage] = useState(1) const [waiting, setWaiting] = useState(false) const [status, setStatus] = useState<'true' | 'false'>('true') const [refreshing, setRefreshing] = useState(false) async function handleLoad(loading: boolean, thisPage: number) { try { setWaiting(true) setLoading(loading) setPage(thisPage) const hasil = await decryptToken(String(token?.current)) const response = await apiGetDiscussion({ user: hasil, search, division: id, active: status, page: thisPage }) if (thisPage == 1) { setData(response.data) } else if (thisPage > 1 && response.data.length > 0) { setData([...data, ...response.data]) } else { return; } } catch (error) { console.error(error) } finally { setLoading(false) setWaiting(false) } } useEffect(() => { handleLoad(false, 1) }, [update.data]) useEffect(() => { handleLoad(true, 1) }, [status, search]) const loadMoreData = () => { if (waiting) return setTimeout(() => { handleLoad(false, page + 1) }, 1000); } const handleRefresh = async () => { setRefreshing(true) handleLoad(false, 1) await new Promise(resolve => setTimeout(resolve, 2000)); setRefreshing(false) }; const getItem = (_data: unknown, index: number): Props => ({ id: data[index].id, title: data[index].title, desc: data[index].desc, status: data[index].status, user_name: data[index].user_name, img: data[index].img, total_komentar: data[index].total_komentar, createdAt: data[index].createdAt, isActive: data[index].isActive, }) return ( { setStatus("true") }} label="Aktif" icon={} n={2} /> { setStatus("false") }} label="Arsip" icon={} n={2} /> { loading ? arrSkeleton.map((item: any, i: number) => { return ( ) }) : data.length > 0 ? data.length} getItem={getItem} renderItem={({ item, index }: { item: Props, index: number }) => { return ( { router.push(`./discussion/${item.id}`) }} borderType="bottom" icon={ } title={item.user_name} subtitle={ status == "true" ? item.status == 1 ? : : <> } rightTopInfo={item.createdAt} desc={item.desc} leftBottomInfo={ Diskusikan } rightBottomInfo={item.total_komentar + ' Komentar'} /> ) }} keyExtractor={(item, index) => String(index)} onEndReached={loadMoreData} onEndReachedThreshold={0.5} showsVerticalScrollIndicator={false} refreshControl={ } /> : (Tidak ada diskusi) } ); }