2021-02-15 12:33:47 +00:00
|
|
|
import { useApi } from '@skolplattformen/api-hooks'
|
2021-02-20 08:38:08 +00:00
|
|
|
import { Text } from '@ui-kitten/components'
|
2021-01-26 11:02:24 +00:00
|
|
|
import URI from 'jsuri'
|
2021-03-11 16:45:45 +00:00
|
|
|
import React, { useEffect, useState } from 'react'
|
2021-02-20 08:38:08 +00:00
|
|
|
import { Modal, StyleSheet, TouchableOpacity, View } from 'react-native'
|
2021-02-15 12:33:47 +00:00
|
|
|
import { SafeAreaView } from 'react-native-safe-area-context'
|
|
|
|
import { WebView } from 'react-native-webview'
|
2021-02-20 08:38:08 +00:00
|
|
|
import { CloseIcon } from './icon.component'
|
2021-01-26 11:02:24 +00:00
|
|
|
|
2021-03-26 08:38:15 +00:00
|
|
|
interface ModalWebViewProps {
|
|
|
|
url: string
|
2021-04-01 19:04:10 +00:00
|
|
|
sharedCookiesEnabled: boolean
|
2021-03-26 08:38:15 +00:00
|
|
|
onClose: () => void
|
|
|
|
}
|
|
|
|
|
2021-04-01 19:04:10 +00:00
|
|
|
export const ModalWebView = ({
|
|
|
|
url,
|
|
|
|
onClose,
|
|
|
|
sharedCookiesEnabled,
|
|
|
|
}: ModalWebViewProps) => {
|
2021-02-20 08:38:08 +00:00
|
|
|
const [modalVisible, setModalVisible] = React.useState(true)
|
2021-02-15 12:33:47 +00:00
|
|
|
const { api } = useApi()
|
2021-03-11 16:45:45 +00:00
|
|
|
const [headers, setHeaders] = useState()
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-04-01 19:04:10 +00:00
|
|
|
const getHeaders = async (urlToGetSessionFor: string) => {
|
2021-03-20 17:57:34 +00:00
|
|
|
if (sharedCookiesEnabled) return
|
|
|
|
const { headers: newHeaders } = await api.getSession(urlToGetSessionFor)
|
|
|
|
setHeaders(newHeaders)
|
|
|
|
}
|
|
|
|
|
|
|
|
getHeaders(url)
|
|
|
|
}, [url, sharedCookiesEnabled, api])
|
2021-03-11 16:45:45 +00:00
|
|
|
|
2021-01-26 11:02:24 +00:00
|
|
|
const uri = new URI(url)
|
2021-02-15 12:33:47 +00:00
|
|
|
|
2021-02-18 10:43:16 +00:00
|
|
|
const closeModal = () => {
|
|
|
|
setModalVisible(false)
|
|
|
|
onClose()
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:02:24 +00:00
|
|
|
return (
|
2021-02-18 10:43:16 +00:00
|
|
|
<Modal
|
|
|
|
animationType="slide"
|
|
|
|
statusBarTranslucent={true}
|
|
|
|
visible={modalVisible}
|
2021-02-20 08:38:08 +00:00
|
|
|
onRequestClose={closeModal}
|
|
|
|
>
|
2021-01-26 11:02:24 +00:00
|
|
|
<SafeAreaView style={styles.container}>
|
|
|
|
<View style={styles.headerWrapper}>
|
|
|
|
<View style={styles.header}>
|
|
|
|
<Text category="s1">{uri.host()}</Text>
|
2021-02-18 10:43:16 +00:00
|
|
|
<TouchableOpacity onPress={closeModal}>
|
2021-02-20 08:38:08 +00:00
|
|
|
<CloseIcon style={styles.icon} fill="#333333" />
|
2021-01-26 11:02:24 +00:00
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
|
|
|
</View>
|
2021-03-20 13:03:57 +00:00
|
|
|
{(headers || sharedCookiesEnabled) && (
|
|
|
|
<WebView
|
|
|
|
style={styles.webview}
|
|
|
|
source={{ uri: url, headers }}
|
|
|
|
sharedCookiesEnabled={sharedCookiesEnabled}
|
|
|
|
/>
|
2021-03-11 16:45:45 +00:00
|
|
|
)}
|
2021-01-26 11:02:24 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
2021-02-07 16:14:45 +00:00
|
|
|
flex: 1,
|
2021-01-26 11:02:24 +00:00
|
|
|
},
|
|
|
|
headerWrapper: {
|
|
|
|
backgroundColor: '#333333',
|
|
|
|
padding: 5,
|
|
|
|
},
|
|
|
|
header: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
alignItems: 'center',
|
|
|
|
paddingHorizontal: 10,
|
|
|
|
paddingVertical: 4,
|
|
|
|
backgroundColor: '#ffffff',
|
|
|
|
borderRadius: 5,
|
|
|
|
},
|
|
|
|
icon: {
|
|
|
|
width: 30,
|
|
|
|
height: 30,
|
|
|
|
},
|
2021-02-07 16:14:45 +00:00
|
|
|
webview: {},
|
2021-01-26 11:02:24 +00:00
|
|
|
})
|