import Styles from "@/constants/Styles"; import { apiDeleteLinkProject, apiGetProjectOne } from "@/lib/api"; import { urlCompleted } from "@/lib/fun_urlCompleted"; import { setUpdateProject } from "@/lib/projectUpdate"; import { useAuthSession } from "@/providers/AuthProvider"; import { Feather, Ionicons } from "@expo/vector-icons"; import { useLocalSearchParams } from "expo-router"; import { useEffect, useState } from "react"; import { Linking, View } from "react-native"; import Toast from "react-native-toast-message"; import { useDispatch, useSelector } from "react-redux"; import AlertKonfirmasi from "../alertKonfirmasi"; import BorderBottomItem from "../borderBottomItem"; import DrawerBottom from "../drawerBottom"; import MenuItemRow from "../menuItemRow"; import Text from "../Text"; type Props = { id: string link: string } export default function SectionLink({ status, member, refreshing }: { status: number | undefined, member: boolean, refreshing?: boolean }) { const entityUser = useSelector((state: any) => state.user) const [isModal, setModal] = useState(false) const { token, decryptToken } = useAuthSession(); const { id } = useLocalSearchParams<{ id: string }>(); const [data, setData] = useState([]); const update = useSelector((state: any) => state.projectUpdate) const dispatch = useDispatch() const [selectLink, setSelectLink] = useState(null) async function handleLoad() { try { const hasil = await decryptToken(String(token?.current)); const response = await apiGetProjectOne({ user: hasil, cat: "link", id: id, }); setData(response.data); } catch (error) { console.error(error); } } useEffect(() => { handleLoad(); }, [update.link]); useEffect(() => { if (refreshing) handleLoad(); }, [refreshing]); async function handleDelete() { try { const hasil = await decryptToken(String(token?.current)); const response = await apiDeleteLinkProject({ user: hasil, idLink: String(selectLink?.id) }, String(id)); if (response.success) { Toast.show({ type: 'small', text1: 'Berhasil menghapus link', }) dispatch(setUpdateProject({ ...update, link: !update.link })) } else { Toast.show({ type: 'small', text1: response.message, }) } } catch (error) { console.error(error); Toast.show({ type: 'small', text1: 'Terjadi kesalahan', }) } finally { setModal(false) } } return ( <> { data.length > 0 && <> Link { data.map((item, index) => { return ( } title={item.link} titleWeight="normal" onPress={() => { setSelectLink(item); setModal(true) }} width={65} /> ) }) } } title="Buka Link" onPress={() => { Linking.openURL(urlCompleted(String(selectLink?.link))) }} /> { !member && (entityUser.role == "user" || entityUser.role == "coadmin") ? <> : } title="Hapus" disabled={status == 3} onPress={() => { if (status == 3) return setModal(false) AlertKonfirmasi({ title: 'Konfirmasi', desc: 'Apakah Anda yakin ingin menghapus link ini? Link yang dihapus tidak dapat dikembalikan', onPress: () => { handleDelete() } }) }} /> } } ) }