upd: view file pada list banner
This commit is contained in:
@@ -9,16 +9,29 @@ import { apiDeleteBanner, apiGetBanner } from "@/lib/api"
|
|||||||
import { setEntities } from "@/lib/bannerSlice"
|
import { setEntities } from "@/lib/bannerSlice"
|
||||||
import { useAuthSession } from "@/providers/AuthProvider"
|
import { useAuthSession } from "@/providers/AuthProvider"
|
||||||
import { Ionicons, MaterialCommunityIcons } from "@expo/vector-icons"
|
import { Ionicons, MaterialCommunityIcons } from "@expo/vector-icons"
|
||||||
|
import * as FileSystem from 'expo-file-system'
|
||||||
|
import { startActivityAsync } from 'expo-intent-launcher'
|
||||||
import { router, Stack } from "expo-router"
|
import { router, Stack } from "expo-router"
|
||||||
|
import * as Sharing from 'expo-sharing'
|
||||||
import { useState } from "react"
|
import { useState } from "react"
|
||||||
import { Image, RefreshControl, SafeAreaView, ScrollView, ToastAndroid, View } from "react-native"
|
import { Alert, Image, Platform, RefreshControl, SafeAreaView, ScrollView, ToastAndroid, View } from "react-native"
|
||||||
|
import * as mime from 'react-native-mime-types'
|
||||||
import { useDispatch, useSelector } from "react-redux"
|
import { useDispatch, useSelector } from "react-redux"
|
||||||
|
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
id: string
|
||||||
|
name: string
|
||||||
|
extension: string
|
||||||
|
image: string
|
||||||
|
}
|
||||||
|
|
||||||
export default function BannerList() {
|
export default function BannerList() {
|
||||||
const { decryptToken, token } = useAuthSession()
|
const { decryptToken, token } = useAuthSession()
|
||||||
const [isModal, setModal] = useState(false)
|
const [isModal, setModal] = useState(false)
|
||||||
const entities = useSelector((state: any) => state.banner)
|
const entities = useSelector((state: any) => state.banner)
|
||||||
const [dataId, setDataId] = useState('')
|
const [dataId, setDataId] = useState('')
|
||||||
|
const [selectFile, setSelectFile] = useState<Props | null>(null)
|
||||||
const dispatch = useDispatch()
|
const dispatch = useDispatch()
|
||||||
const [refreshing, setRefreshing] = useState(false)
|
const [refreshing, setRefreshing] = useState(false)
|
||||||
|
|
||||||
@@ -52,6 +65,39 @@ export default function BannerList() {
|
|||||||
setRefreshing(false)
|
setRefreshing(false)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const openFile = () => {
|
||||||
|
let remoteUrl = 'https://wibu-storage.wibudev.com/api/files/' + selectFile?.image;
|
||||||
|
const fileName = selectFile?.name + '.' + selectFile?.extension;
|
||||||
|
let localPath = `${FileSystem.documentDirectory}/${fileName}`;
|
||||||
|
const mimeType = mime.lookup(fileName)
|
||||||
|
|
||||||
|
FileSystem.downloadAsync(remoteUrl, localPath).then(async ({ uri }) => {
|
||||||
|
const contentURL = await FileSystem.getContentUriAsync(uri);
|
||||||
|
try {
|
||||||
|
|
||||||
|
if (Platform.OS == 'android') {
|
||||||
|
// open with android intent
|
||||||
|
await startActivityAsync(
|
||||||
|
'android.intent.action.VIEW',
|
||||||
|
{
|
||||||
|
data: contentURL,
|
||||||
|
flags: 1,
|
||||||
|
type: mimeType as string,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
// or
|
||||||
|
// Sharing.shareAsync(localPath);
|
||||||
|
|
||||||
|
} else if (Platform.OS == 'ios') {
|
||||||
|
Sharing.shareAsync(localPath);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
Alert.alert('INFO', 'Gagal membuka file, tidak ada aplikasi yang dapat membuka file ini');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SafeAreaView>
|
<SafeAreaView>
|
||||||
<Stack.Screen
|
<Stack.Screen
|
||||||
@@ -77,6 +123,7 @@ export default function BannerList() {
|
|||||||
key={key}
|
key={key}
|
||||||
onPress={() => {
|
onPress={() => {
|
||||||
setDataId(index.id)
|
setDataId(index.id)
|
||||||
|
setSelectFile(index)
|
||||||
setModal(true)
|
setModal(true)
|
||||||
}}
|
}}
|
||||||
borderType="all"
|
borderType="all"
|
||||||
@@ -105,7 +152,7 @@ export default function BannerList() {
|
|||||||
<MenuItemRow
|
<MenuItemRow
|
||||||
icon={<MaterialCommunityIcons name="file-eye" color="black" size={25} />}
|
icon={<MaterialCommunityIcons name="file-eye" color="black" size={25} />}
|
||||||
title="Lihat File"
|
title="Lihat File"
|
||||||
onPress={() => { }}
|
onPress={() => { openFile() }}
|
||||||
/>
|
/>
|
||||||
<MenuItemRow
|
<MenuItemRow
|
||||||
icon={<Ionicons name="trash" color="black" size={25} />}
|
icon={<Ionicons name="trash" color="black" size={25} />}
|
||||||
|
|||||||
Reference in New Issue
Block a user