import * as React from 'react'; import {View} from "react-native"; import {ActivityIndicator, Text, withTheme} from 'react-native-paper'; import ConnectionManager from "../managers/ConnectionManager"; type Props = { navigation: Object, theme: Object, link: string, renderFunction: Function, } type State = { loading: boolean, } class AuthenticatedScreen extends React.Component { state = { loading: true, }; currentUserToken: string; connectionManager: ConnectionManager; data: Object; colors: Object; constructor(props) { super(props); this.colors = props.theme.colors; this.connectionManager = ConnectionManager.getInstance(); 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}); this.connectionManager.isLoggedIn() .then(() => { this.connectionManager.authenticatedRequest(this.props.link) .then((data) => { this.onFinishedLoading(data); }) .catch((error) => { this.onFinishedLoading(undefined); }); }) .catch(() => { this.onFinishedLoading(undefined); }); } onFinishedLoading(data: Object) { this.data = data; this.currentUserToken = data !== undefined ? this.connectionManager.getToken() : ''; this.setState({loading: false}); } /** * Gets the loading indicator * * @return {*} */ getRenderLoading() { return ( ); } getErrorRender() { return ERROR; } render() { return ( this.state.loading ? this.getRenderLoading() : (this.data !== undefined ? this.props.renderFunction(this.data) : this.getErrorRender()) ); } } export default withTheme(AuthenticatedScreen);