Deskripsi: - title home align left - nama file pada view banner - modal crash pada group dan jabatan No Issues
220 lines
8.3 KiB
TypeScript
220 lines
8.3 KiB
TypeScript
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 { apiDeleteGroup, apiEditGroup, apiGetGroup } from "@/lib/api";
|
|
import { setUpdateGroup } from "@/lib/groupSlice";
|
|
import { useAuthSession } from "@/providers/AuthProvider";
|
|
import { AntDesign, Feather, MaterialCommunityIcons } from "@expo/vector-icons";
|
|
import { useEffect, useState } from "react";
|
|
import { RefreshControl, SafeAreaView, ScrollView, View } from "react-native";
|
|
import Toast from "react-native-toast-message";
|
|
import { useDispatch, useSelector } from "react-redux";
|
|
|
|
type Props = {
|
|
id: string
|
|
name: string
|
|
isActive: boolean
|
|
}
|
|
|
|
export default function Index() {
|
|
const { token, decryptToken } = useAuthSession()
|
|
const [isModal, setModal] = useState(false)
|
|
const [isVisibleEdit, setVisibleEdit] = useState(false)
|
|
const [data, setData] = useState<Props[]>([])
|
|
const [search, setSearch] = useState('')
|
|
const arrSkeleton = Array.from({ length: 5 }, (_, index) => index)
|
|
const [loading, setLoading] = useState(true)
|
|
const [status, setStatus] = useState<'true' | 'false'>('true')
|
|
|
|
const [idChoose, setIdChoose] = useState('')
|
|
const [activeChoose, setActiveChoose] = useState(true)
|
|
const [titleChoose, setTitleChoose] = useState('')
|
|
const [refreshing, setRefreshing] = useState(false)
|
|
|
|
const dispatch = useDispatch()
|
|
const update = useSelector((state: any) => state.groupUpdate)
|
|
|
|
const [data11, setData1] = useState(Array.from({ length: 20 }, (_, i) => `Item ${i}`));
|
|
|
|
const renderItem = ({ item }: { item: string }) => (
|
|
<View style={{ padding: 20, borderBottomWidth: 1, borderColor: '#ccc' }}>
|
|
<Text>{item}</Text>
|
|
</View>
|
|
);
|
|
|
|
|
|
async function handleEdit() {
|
|
try {
|
|
const hasil = await decryptToken(String(token?.current))
|
|
const response = await apiEditGroup({ user: hasil, name: titleChoose }, idChoose)
|
|
dispatch(setUpdateGroup(!update))
|
|
} catch (error) {
|
|
console.error(error)
|
|
} finally {
|
|
setVisibleEdit(false)
|
|
setModal(false)
|
|
Toast.show({ type: 'small', text1: 'Berhasil mengupdate data', })
|
|
}
|
|
|
|
}
|
|
|
|
async function handleDelete() {
|
|
try {
|
|
const hasil = await decryptToken(String(token?.current))
|
|
const response = await apiDeleteGroup({ user: hasil, isActive: activeChoose }, idChoose)
|
|
dispatch(setUpdateGroup(!update))
|
|
} catch (error) {
|
|
console.error(error)
|
|
} finally {
|
|
setModal(false)
|
|
Toast.show({ type: 'small', text1: 'Berhasil mengupdate data', })
|
|
}
|
|
}
|
|
|
|
async function handleLoad(loading: boolean) {
|
|
try {
|
|
setLoading(loading)
|
|
const hasil = await decryptToken(String(token?.current))
|
|
const response = await apiGetGroup({ user: hasil, active: status, search: search })
|
|
setData(response.data)
|
|
} catch (error) {
|
|
console.error(error)
|
|
} finally {
|
|
setLoading(false)
|
|
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
handleLoad(false)
|
|
}, [update])
|
|
|
|
useEffect(() => {
|
|
handleLoad(true)
|
|
}, [status, search])
|
|
|
|
const handleRefresh = async () => {
|
|
setRefreshing(true)
|
|
handleLoad(false)
|
|
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
setRefreshing(false)
|
|
};
|
|
|
|
|
|
return (
|
|
<SafeAreaView>
|
|
<ScrollView
|
|
refreshControl={
|
|
<RefreshControl
|
|
refreshing={refreshing}
|
|
onRefresh={handleRefresh}
|
|
/>
|
|
}
|
|
>
|
|
<View style={[Styles.p15]}>
|
|
<View style={[Styles.wrapBtnTab]}>
|
|
<ButtonTab
|
|
active={status == "false" ? "false" : "true"}
|
|
value="true"
|
|
onPress={() => { setStatus("true") }}
|
|
label="Aktif"
|
|
icon={<Feather name="check-circle" color={status == "true" ? 'white' : 'black'} size={20} />}
|
|
n={2} />
|
|
<ButtonTab
|
|
active={status == "false" ? "false" : "true"}
|
|
value="false"
|
|
onPress={() => { setStatus("false") }}
|
|
label="Tidak Aktif"
|
|
icon={<AntDesign name="closecircleo" color={status == "false" ? 'white' : 'black'} size={20} />}
|
|
n={2} />
|
|
</View>
|
|
<InputSearch onChange={setSearch} />
|
|
<View>
|
|
{
|
|
|
|
loading ?
|
|
arrSkeleton.map((item, index) => {
|
|
return (
|
|
<SkeletonTwoItem key={index} />
|
|
)
|
|
})
|
|
:
|
|
data.length > 0 ?
|
|
data.map((item, index) => {
|
|
return (
|
|
<BorderBottomItem
|
|
key={index}
|
|
onPress={() => {
|
|
setIdChoose(item.id)
|
|
setActiveChoose(item.isActive)
|
|
setTitleChoose(item.name)
|
|
setModal(true)
|
|
}}
|
|
borderType="all"
|
|
icon={
|
|
<View style={[Styles.iconContent, ColorsStatus.lightGreen]}>
|
|
<MaterialCommunityIcons name="office-building-outline" size={25} color={'#384288'} />
|
|
</View>
|
|
}
|
|
title={item.name}
|
|
/>
|
|
)
|
|
})
|
|
:
|
|
<Text style={[Styles.textDefault, Styles.cGray, { textAlign: 'center' }]}>Tidak ada data</Text>
|
|
}
|
|
</View>
|
|
</View>
|
|
</ScrollView>
|
|
|
|
<DrawerBottom animation="slide" isVisible={isModal} setVisible={() => setModal(false)} title={titleChoose}>
|
|
<View style={Styles.rowItemsCenter}>
|
|
<MenuItemRow
|
|
icon={<MaterialCommunityIcons name="toggle-switch-off-outline" color="black" size={25} />}
|
|
title={activeChoose ? "Non Aktifkan" : "Aktifkan"}
|
|
onPress={() => {
|
|
setModal(false)
|
|
AlertKonfirmasi({
|
|
title: 'Konfirmasi',
|
|
desc: activeChoose ? 'Apakah anda yakin ingin menonaktifkan data?' : 'Apakah anda yakin ingin mengaktifkan data?',
|
|
onPress: () => { handleDelete() }
|
|
})
|
|
}}
|
|
/>
|
|
<MenuItemRow
|
|
icon={<MaterialCommunityIcons name="pencil-outline" color="black" size={25} />}
|
|
title="Edit"
|
|
onPress={() => {
|
|
setModal(false)
|
|
setTimeout(() => {
|
|
setVisibleEdit(true)
|
|
}, 600);
|
|
}}
|
|
/>
|
|
</View>
|
|
</DrawerBottom>
|
|
|
|
<DrawerBottom animation="none" height={30} isVisible={isVisibleEdit} setVisible={() => setVisibleEdit(false)} title="Edit Lembaga Desa">
|
|
<View style={{ flex: 1 }}>
|
|
<View>
|
|
<InputForm type="default" placeholder="Nama Lembaga Desa" required label="Lembaga Desa" value={titleChoose} onChange={setTitleChoose} />
|
|
</View>
|
|
<View>
|
|
<ButtonForm text="SIMPAN" onPress={() => { handleEdit() }} />
|
|
</View>
|
|
</View>
|
|
</DrawerBottom>
|
|
|
|
|
|
</SafeAreaView>
|
|
)
|
|
} |