application-amicale/components/Amicale/AuthenticatedScreen.js

114 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-04-02 09:52:43 +02:00
// @flow
2020-03-31 14:21:01 +02:00
import * as React from 'react';
2020-04-02 10:45:42 +02:00
import {withTheme} from 'react-native-paper';
2020-04-02 10:07:20 +02:00
import ConnectionManager, {ERROR_TYPE} from "../../managers/ConnectionManager";
import NetworkErrorComponent from "../Custom/NetworkErrorComponent";
2020-04-02 09:58:25 +02:00
import i18n from 'i18n-js';
2020-04-02 10:45:42 +02:00
import BasicLoadingScreen from "../Custom/BasicLoadingScreen";
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();
}
2020-04-02 09:52:43 +02:00
fetchData = () => {
2020-03-31 18:40:06 +02:00
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-04-02 09:52:43 +02:00
this.onFinishedLoading(data, -1);
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-04-02 09:52:43 +02:00
};
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});
}
getErrorRender() {
2020-03-31 19:00:47 +02:00
let message;
let icon;
switch (this.errorCode) {
case ERROR_TYPE.BAD_CREDENTIALS:
2020-04-02 09:58:25 +02:00
message = i18n.t("loginScreen.errors.credentials");
2020-03-31 19:00:47 +02:00
icon = "account-alert-outline";
break;
case ERROR_TYPE.CONNECTION_ERROR:
2020-04-02 09:58:25 +02:00
message = i18n.t("loginScreen.errors.connection");
2020-03-31 19:00:47 +02:00
icon = "access-point-network-off";
break;
default:
2020-04-02 09:58:25 +02:00
message = i18n.t("loginScreen.errors.unknown");
2020-03-31 19:00:47 +02:00
icon = "alert-circle-outline";
break;
}
return (
2020-04-02 09:52:43 +02:00
<NetworkErrorComponent
{...this.props}
icon={icon}
message={message}
onRefresh={this.fetchData}
/>
2020-03-31 19:00:47 +02:00
);
2020-03-31 18:40:06 +02:00
}
2020-03-31 14:21:01 +02:00
render() {
return (
this.state.loading
2020-04-02 10:45:42 +02:00
? <BasicLoadingScreen/>
2020-03-31 18:40:06 +02:00
: (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
export default withTheme(AuthenticatedScreen);