import AlertKonfirmasi from "@/components/alertKonfirmasi"; import BorderBottomItem from "@/components/borderBottomItem"; import { ButtonForm } from "@/components/buttonForm"; import ButtonTab from "@/components/buttonTab"; import DrawerBottom from "@/components/drawerBottom"; import { InputForm } from "@/components/inputForm"; import InputSearch from "@/components/inputSearch"; import MenuItemRow from "@/components/menuItemRow"; import SkeletonTwoItem from "@/components/skeletonTwoItem"; import Text from "@/components/Text"; import { ColorsStatus } from "@/constants/ColorsStatus"; import Styles from "@/constants/Styles"; import { apiDeletePosition, apiEditPosition, apiGetPosition } from "@/lib/api"; import { setUpdatePosition } from "@/lib/positionSlice"; import { useAuthSession } from "@/providers/AuthProvider"; import { AntDesign, Feather, MaterialCommunityIcons } from "@expo/vector-icons"; import { useLocalSearchParams } from "expo-router"; import { useEffect, useState } from "react"; import { RefreshControl, View, VirtualizedList } from "react-native"; import Toast from "react-native-toast-message"; import { useDispatch, useSelector } from "react-redux"; type Props = { name: string; idGroup: string; group: string; id: string; isActive: boolean } export default function Index() { const arrSkeleton = Array.from({ length: 5 }, (_, index) => index) const [loading, setLoading] = useState(true) const { token, decryptToken } = useAuthSession() const [status, setStatus] = useState<'true' | 'false'>('true') const entityUser = useSelector((state: any) => state.user) const { active, group } = useLocalSearchParams<{ active?: string, group?: string }>() const [isModal, setModal] = useState(false) const [isVisibleEdit, setVisibleEdit] = useState(false) const [data, setData] = useState([]) const [search, setSearch] = useState('') const [nameGroup, setNameGroup] = useState('') const [loadingSubmit, setLoadingSubmit] = useState(false) const [chooseData, setChooseData] = useState({ name: '', id: '', active: false, idGroup: '' }) const [error, setError] = useState({ name: false, }); const [refreshing, setRefreshing] = useState(false) const dispatch = useDispatch() const update = useSelector((state: any) => state.positionUpdate) async function handleLoad(loading: boolean) { try { setLoading(loading) const hasil = await decryptToken(String(token?.current)) const response = await apiGetPosition({ user: hasil, active: status, search: search, group: String(group) }) setData(response.data) setNameGroup(response.filter.name) } catch (error) { console.error(error) } finally { setLoading(false) } } useEffect(() => { handleLoad(false) }, [update]) useEffect(() => { handleLoad(true) }, [status, search, group]) function handleChooseData(id: string, name: string, active: boolean, group: string) { setChooseData({ id, name, active, idGroup: group }) setModal(true) } async function handleDelete() { try { const hasil = await decryptToken(String(token?.current)) const response = await apiDeletePosition({ user: hasil, isActive: chooseData.active }, chooseData.id) dispatch(setUpdatePosition(!update)) } catch (error) { console.error(error) } finally { setModal(false) Toast.show({ type: 'small', text1: 'Berhasil mengupdate data', }) } } async function handleEdit() { try { setLoadingSubmit(true) const hasil = await decryptToken(String(token?.current)) const response = await apiEditPosition({ user: hasil, name: chooseData.name, idGroup: chooseData.idGroup }, chooseData.id) if (response.success) { dispatch(setUpdatePosition(!update)) } else { Toast.show({ type: 'small', text1: response.message, }) } } catch (error) { console.error(error) } finally { setLoadingSubmit(false) setVisibleEdit(false) setModal(false) } } function validationForm(value: string) { setChooseData({ ...chooseData, name: value }) if (value == "") { setError({ ...error, name: true }) } else { setError({ ...error, name: false }) } } function checkForm() { if (Object.values(error).some((v) => v == true)) return false handleEdit() } const handleRefresh = async () => { setRefreshing(true) handleLoad(false) await new Promise(resolve => setTimeout(resolve, 2000)); setRefreshing(false) }; const getItem = (_data: unknown, index: number): Props => ({ id: data[index].id, name: data[index].name, idGroup: data[index].idGroup, group: data[index].group, isActive: data[index].isActive, }); return ( { setStatus("true") }} label="Aktif" icon={} n={2} /> { setStatus("false") }} label="Tidak Aktif" icon={} n={2} /> { (entityUser.role == "supadmin" || entityUser.role == "developer") && Filter : {nameGroup} } { loading ? arrSkeleton.map((item, index) => { return ( ) }) : data.length > 0 ? data.length} getItem={getItem} renderItem={({ item, index }: { item: Props, index: number }) => { return ( { handleChooseData(item.id, item.name, item.isActive, item.idGroup) }} borderType="all" icon={ } title={item.name} subtitle={item.group} /> ) }} keyExtractor={(item, index) => String(index)} showsVerticalScrollIndicator={false} refreshControl={ } /> : Tidak ada data } setModal(false)} title={chooseData.name}> } title={chooseData.active ? 'Non Aktifkan' : "Aktifkan"} onPress={() => { setModal(false) AlertKonfirmasi({ title: 'Konfirmasi', desc: chooseData.active ? 'Apakah anda yakin ingin menonaktifkan data?' : 'Apakah anda yakin ingin mengaktifkan data?', onPress: () => { handleDelete() } }) }} /> } title="Edit" onPress={() => { setModal(false) setTimeout(() => { setVisibleEdit(true) }, 600) }} /> setVisibleEdit(false)} title="Edit Jabatan"> { validationForm(val) }} error={error.name} errorText="Nama jabatan tidak boleh kosong" /> { handleEdit() }} disabled={Object.values(error).some((v) => v == true) || chooseData.name == "" || loadingSubmit} /> ) }