2020-03-31 14:21:01 +02:00
|
|
|
import * as React from 'react';
|
2020-03-31 19:00:47 +02:00
|
|
|
import {StyleSheet, View} from "react-native";
|
|
|
|
import {ActivityIndicator, Subheading, withTheme} from 'react-native-paper';
|
|
|
|
import ConnectionManager, {ERROR_TYPE} from "../managers/ConnectionManager";
|
|
|
|
import {MaterialCommunityIcons} from "@expo/vector-icons";
|
2020-03-31 14:21:01 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
navigation: Object,
|
|
|
|
theme: Object,
|
|
|
|
link: string,
|
|
|
|
renderFunction: Function,
|
|
|
|
}
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
loading: boolean,
|
|
|
|
}
|
|
|
|
|
2020-03-31 18:40:06 +02:00
|
|
|
class AuthenticatedScreen extends React.Component<Props, State> {
|
2020-03-31 14:21:01 +02:00
|
|
|
|
|
|
|
state = {
|
|
|
|
loading: true,
|
|
|
|
};
|
|
|
|
|
2020-03-31 18:40:06 +02:00
|
|
|
currentUserToken: string;
|
2020-03-31 14:21:01 +02:00
|
|
|
connectionManager: ConnectionManager;
|
2020-03-31 19:00:47 +02:00
|
|
|
errorCode: number;
|
2020-03-31 18:40:06 +02:00
|
|
|
data: Object;
|
|
|
|
colors: Object;
|
2020-03-31 14:21:01 +02:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-03-31 18:40:06 +02:00
|
|
|
this.colors = props.theme.colors;
|
2020-03-31 14:21:01 +02:00
|
|
|
this.connectionManager = ConnectionManager.getInstance();
|
2020-03-31 18:40:06 +02:00
|
|
|
this.props.navigation.addListener('focus', this.onScreenFocus.bind(this));
|
|
|
|
|
|
|
|
this.fetchData();
|
|
|
|
}
|
|
|
|
|
|
|
|
onScreenFocus() {
|
|
|
|
if (this.currentUserToken !== this.connectionManager.getToken())
|
|
|
|
this.fetchData();
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchData() {
|
|
|
|
if (!this.state.loading)
|
|
|
|
this.setState({loading: true});
|
2020-03-31 14:21:01 +02:00
|
|
|
this.connectionManager.isLoggedIn()
|
|
|
|
.then(() => {
|
|
|
|
this.connectionManager.authenticatedRequest(this.props.link)
|
|
|
|
.then((data) => {
|
2020-03-31 18:40:06 +02:00
|
|
|
this.onFinishedLoading(data);
|
2020-03-31 14:21:01 +02:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2020-03-31 19:00:47 +02:00
|
|
|
this.onFinishedLoading(undefined, error);
|
2020-03-31 14:21:01 +02:00
|
|
|
});
|
|
|
|
})
|
2020-03-31 19:00:47 +02:00
|
|
|
.catch((error) => {
|
|
|
|
this.onFinishedLoading(undefined, ERROR_TYPE.BAD_CREDENTIALS);
|
2020-03-31 14:21:01 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 19:00:47 +02:00
|
|
|
onFinishedLoading(data: Object, error: number) {
|
2020-03-31 18:40:06 +02:00
|
|
|
this.data = data;
|
|
|
|
this.currentUserToken = data !== undefined
|
|
|
|
? this.connectionManager.getToken()
|
|
|
|
: '';
|
2020-03-31 19:00:47 +02:00
|
|
|
this.errorCode = error;
|
2020-03-31 18:40:06 +02:00
|
|
|
this.setState({loading: false});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the loading indicator
|
|
|
|
*
|
|
|
|
* @return {*}
|
|
|
|
*/
|
|
|
|
getRenderLoading() {
|
|
|
|
return (
|
|
|
|
<View style={{
|
|
|
|
backgroundColor: this.colors.background,
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
right: 0,
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
flex: 1,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
}}>
|
|
|
|
<ActivityIndicator
|
|
|
|
animating={true}
|
|
|
|
size={'large'}
|
|
|
|
color={this.colors.primary}/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getErrorRender() {
|
2020-03-31 19:00:47 +02:00
|
|
|
let message;
|
|
|
|
let icon;
|
|
|
|
switch (this.errorCode) {
|
|
|
|
case ERROR_TYPE.BAD_CREDENTIALS:
|
|
|
|
message = "BAD_CREDENTIALS";
|
|
|
|
icon = "account-alert-outline";
|
|
|
|
break;
|
|
|
|
case ERROR_TYPE.CONNECTION_ERROR:
|
|
|
|
message = "CONNECTION_ERROR";
|
|
|
|
icon = "access-point-network-off";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
message = "UNKNOWN";
|
|
|
|
icon = "alert-circle-outline";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={styles.outer}>
|
|
|
|
<View style={styles.inner}>
|
|
|
|
<View style={styles.iconContainer}>
|
|
|
|
<MaterialCommunityIcons
|
|
|
|
name={icon}
|
|
|
|
size={150}
|
|
|
|
color={this.colors.textDisabled}/>
|
|
|
|
</View>
|
|
|
|
<Subheading style={{
|
|
|
|
...styles.subheading,
|
|
|
|
color: this.colors.textDisabled
|
|
|
|
}}>
|
|
|
|
{message}
|
|
|
|
</Subheading>
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
);
|
2020-03-31 18:40:06 +02:00
|
|
|
}
|
|
|
|
|
2020-03-31 14:21:01 +02:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
this.state.loading
|
2020-03-31 18:40:06 +02:00
|
|
|
? this.getRenderLoading()
|
|
|
|
: (this.data !== undefined
|
|
|
|
? this.props.renderFunction(this.data)
|
|
|
|
: this.getErrorRender())
|
2020-03-31 14:21:01 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-03-31 18:40:06 +02:00
|
|
|
|
2020-03-31 19:00:47 +02:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
outer: {
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
inner: {
|
|
|
|
marginTop: 'auto',
|
|
|
|
marginBottom: 'auto',
|
|
|
|
},
|
|
|
|
iconContainer: {
|
|
|
|
marginLeft: 'auto',
|
|
|
|
marginRight: 'auto',
|
|
|
|
marginBottom: 20
|
|
|
|
},
|
|
|
|
subheading: {
|
|
|
|
textAlign: 'center',
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-03-31 18:40:06 +02:00
|
|
|
export default withTheme(AuthenticatedScreen);
|