skolplattformen-backup/packages/app/components/modalWebView.component.tsx

121 lines
3.0 KiB
TypeScript
Raw Normal View History

import { useApi } from '@skolplattformen/api-hooks'
2021-02-20 08:38:08 +00:00
import { Text } from '@ui-kitten/components'
2021-03-11 16:45:45 +00:00
import React, { useEffect, useState } from 'react'
import {
Linking,
Modal,
StyleSheet,
TouchableOpacity,
View,
} from 'react-native'
import { SafeAreaView } from 'react-native-safe-area-context'
import { WebView } from 'react-native-webview'
import { WebViewNavigationEvent } from 'react-native-webview/lib/WebViewTypes'
import { BackIcon, ExternalLinkIcon } from './icon.component'
2021-01-26 11:02:24 +00:00
2021-03-26 08:38:15 +00:00
interface ModalWebViewProps {
url: string
sharedCookiesEnabled: boolean
2021-03-26 08:38:15 +00:00
onClose: () => void
}
export const ModalWebView = ({
url,
onClose,
sharedCookiesEnabled,
}: ModalWebViewProps) => {
2021-02-20 08:38:08 +00:00
const [modalVisible, setModalVisible] = React.useState(true)
const { api } = useApi()
const [title, setTitle] = React.useState('...')
2021-03-11 16:45:45 +00:00
const [headers, setHeaders] = useState()
useEffect(() => {
const getHeaders = async (urlToGetSessionFor: string) => {
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
const closeModal = () => {
setModalVisible(false)
onClose()
}
const openInApp = () => {
Linking.openURL(url)
}
2021-01-26 11:02:24 +00:00
return (
<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}>
<TouchableOpacity onPress={closeModal}>
<BackIcon style={styles.backIcon} fill="#333333" />
</TouchableOpacity>
<Text category="s1" style={styles.headerText} numberOfLines={1}>
{title}
</Text>
<TouchableOpacity onPress={openInApp}>
<ExternalLinkIcon style={styles.shareIcon} fill="#333333" />
2021-01-26 11:02:24 +00:00
</TouchableOpacity>
</View>
</View>
{(headers || sharedCookiesEnabled) && (
<WebView
style={styles.webview}
source={{ uri: url, headers }}
sharedCookiesEnabled={sharedCookiesEnabled}
thirdPartyCookiesEnabled={sharedCookiesEnabled}
onLoad={(event: WebViewNavigationEvent) => {
setTitle(event.nativeEvent.title)
}}
/>
2021-03-11 16:45:45 +00:00
)}
2021-01-26 11:02:24 +00:00
</SafeAreaView>
</Modal>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
2021-01-26 11:02:24 +00:00
},
headerWrapper: {
marginTop: 5,
backgroundColor: '#ffffff',
2021-01-26 11:02:24 +00:00
padding: 5,
borderRadius: 2,
borderColor: '#6B7280',
borderBottomWidth: 1,
},
headerText: {
overflow: 'hidden',
width: '85%',
paddingRight: 2,
2021-01-26 11:02:24 +00:00
},
header: {
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: 10,
paddingVertical: 4,
backgroundColor: '#ffffff',
},
shareIcon: {
width: 24,
height: 24,
},
backIcon: {
width: 24,
height: 24,
marginRight: 15,
2021-01-26 11:02:24 +00:00
},
webview: {},
2021-01-26 11:02:24 +00:00
})