2020-03-05 19:54:56 +01:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as React from 'react';
|
|
|
|
import WebDataManager from "../utils/WebDataManager";
|
|
|
|
import i18n from "i18n-js";
|
2020-03-07 11:49:32 +01:00
|
|
|
import {Snackbar} from 'react-native-paper';
|
2020-03-06 23:15:01 +01:00
|
|
|
import {RefreshControl, SectionList, View} from "react-native";
|
2020-03-07 11:49:32 +01:00
|
|
|
import EmptyWebSectionListItem from "./EmptyWebSectionListItem";
|
2020-03-05 19:54:56 +01:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
navigation: Object,
|
|
|
|
fetchUrl: string,
|
2020-03-08 15:50:34 +01:00
|
|
|
autoRefreshTime: number,
|
|
|
|
refreshOnFocus: boolean,
|
2020-03-05 19:54:56 +01:00
|
|
|
renderItem: React.Node,
|
|
|
|
renderSectionHeader: React.Node,
|
|
|
|
stickyHeader: boolean,
|
|
|
|
createDataset: Function,
|
|
|
|
}
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
refreshing: boolean,
|
|
|
|
firstLoading: boolean,
|
|
|
|
fetchedData: Object,
|
2020-03-07 09:15:25 +01:00
|
|
|
snackbarVisible: boolean
|
2020-03-05 19:54:56 +01:00
|
|
|
};
|
|
|
|
|
2020-03-08 15:50:34 +01:00
|
|
|
|
|
|
|
const MIN_REFRESH_TIME = 5 * 1000;
|
2020-03-05 19:54:56 +01:00
|
|
|
/**
|
|
|
|
* Custom component defining a material icon using native base
|
|
|
|
*
|
|
|
|
* @prop active {boolean} Whether to set the icon color to active
|
|
|
|
* @prop icon {string} The icon string to use from MaterialCommunityIcons
|
|
|
|
* @prop color {string} The icon color. Use default theme color if unspecified
|
|
|
|
* @prop fontSize {number} The icon size. Use 26 if unspecified
|
|
|
|
* @prop width {number} The icon width. Use 30 if unspecified
|
|
|
|
*/
|
2020-03-09 23:15:13 +01:00
|
|
|
export default class WebSectionList extends React.PureComponent<Props, State> {
|
2020-03-05 19:54:56 +01:00
|
|
|
|
|
|
|
static defaultProps = {
|
2020-03-07 09:15:25 +01:00
|
|
|
renderSectionHeader: null,
|
2020-03-05 19:54:56 +01:00
|
|
|
stickyHeader: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
webDataManager: WebDataManager;
|
|
|
|
|
|
|
|
refreshInterval: IntervalID;
|
|
|
|
lastRefresh: Date;
|
|
|
|
|
|
|
|
state = {
|
|
|
|
refreshing: false,
|
|
|
|
firstLoading: true,
|
|
|
|
fetchedData: {},
|
2020-03-07 09:15:25 +01:00
|
|
|
snackbarVisible: false
|
2020-03-05 19:54:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
onRefresh: Function;
|
|
|
|
onFetchSuccess: Function;
|
|
|
|
onFetchError: Function;
|
|
|
|
getEmptyRenderItem: Function;
|
|
|
|
getEmptySectionHeader: Function;
|
2020-03-07 09:15:25 +01:00
|
|
|
showSnackBar: Function;
|
|
|
|
hideSnackBar: Function;
|
2020-03-05 19:54:56 +01:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
// creating references to functions used in render()
|
|
|
|
this.onRefresh = this.onRefresh.bind(this);
|
|
|
|
this.onFetchSuccess = this.onFetchSuccess.bind(this);
|
|
|
|
this.onFetchError = this.onFetchError.bind(this);
|
|
|
|
this.getEmptyRenderItem = this.getEmptyRenderItem.bind(this);
|
|
|
|
this.getEmptySectionHeader = this.getEmptySectionHeader.bind(this);
|
2020-03-07 09:15:25 +01:00
|
|
|
this.showSnackBar = this.showSnackBar.bind(this);
|
|
|
|
this.hideSnackBar = this.hideSnackBar.bind(this);
|
2020-03-05 19:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register react navigation events on first screen load.
|
|
|
|
* Allows to detect when the screen is focused
|
|
|
|
*/
|
|
|
|
componentDidMount() {
|
|
|
|
this.webDataManager = new WebDataManager(this.props.fetchUrl);
|
|
|
|
const onScreenFocus = this.onScreenFocus.bind(this);
|
|
|
|
const onScreenBlur = this.onScreenBlur.bind(this);
|
|
|
|
this.props.navigation.addListener('focus', onScreenFocus);
|
|
|
|
this.props.navigation.addListener('blur', onScreenBlur);
|
2020-03-08 15:50:34 +01:00
|
|
|
this.onRefresh();
|
2020-03-05 19:54:56 +01:00
|
|
|
}
|
|
|
|
|
2020-03-08 15:50:34 +01:00
|
|
|
|
|
|
|
|
2020-03-05 19:54:56 +01:00
|
|
|
/**
|
|
|
|
* Refresh data when focusing the screen and setup a refresh interval if asked to
|
|
|
|
*/
|
|
|
|
onScreenFocus() {
|
2020-03-08 15:50:34 +01:00
|
|
|
if (this.props.refreshOnFocus && this.lastRefresh !== undefined)
|
|
|
|
this.onRefresh();
|
|
|
|
if (this.props.autoRefreshTime > 0)
|
|
|
|
this.refreshInterval = setInterval(this.onRefresh, this.props.autoRefreshTime)
|
2020-03-05 19:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove any interval on un-focus
|
|
|
|
*/
|
|
|
|
onScreenBlur() {
|
|
|
|
clearInterval(this.refreshInterval);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onFetchSuccess(fetchedData: Object) {
|
|
|
|
this.setState({
|
|
|
|
fetchedData: fetchedData,
|
|
|
|
refreshing: false,
|
|
|
|
firstLoading: false
|
|
|
|
});
|
|
|
|
this.lastRefresh = new Date();
|
|
|
|
}
|
|
|
|
|
|
|
|
onFetchError() {
|
|
|
|
this.setState({
|
|
|
|
fetchedData: {},
|
|
|
|
refreshing: false,
|
|
|
|
firstLoading: false
|
|
|
|
});
|
2020-03-07 09:15:25 +01:00
|
|
|
this.showSnackBar();
|
|
|
|
// this.webDataManager.showUpdateToast(this.props.updateErrorText);
|
2020-03-05 19:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refresh data and show a toast if any error occurred
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
onRefresh() {
|
|
|
|
let canRefresh;
|
|
|
|
if (this.lastRefresh !== undefined)
|
2020-03-08 15:50:34 +01:00
|
|
|
canRefresh = (new Date().getTime() - this.lastRefresh.getTime()) > MIN_REFRESH_TIME;
|
2020-03-05 19:54:56 +01:00
|
|
|
else
|
|
|
|
canRefresh = true;
|
|
|
|
if (canRefresh) {
|
|
|
|
this.setState({refreshing: true});
|
|
|
|
this.webDataManager.readData()
|
|
|
|
.then(this.onFetchSuccess)
|
|
|
|
.catch(this.onFetchError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getEmptySectionHeader({section}: Object) {
|
|
|
|
return <View/>;
|
|
|
|
}
|
|
|
|
|
|
|
|
getEmptyRenderItem({item}: Object) {
|
|
|
|
return (
|
2020-03-07 11:49:32 +01:00
|
|
|
<EmptyWebSectionListItem
|
|
|
|
text={item.text}
|
|
|
|
icon={item.icon}
|
|
|
|
refreshing={this.state.refreshing}
|
|
|
|
/>
|
|
|
|
);
|
2020-03-05 19:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
createEmptyDataset() {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
title: '',
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
text: this.state.refreshing ?
|
|
|
|
i18n.t('general.loading') :
|
|
|
|
i18n.t('general.networkError'),
|
|
|
|
isSpinner: this.state.refreshing,
|
|
|
|
icon: this.state.refreshing ?
|
|
|
|
'refresh' :
|
|
|
|
'access-point-network-off'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
keyExtractor: this.datasetKeyExtractor,
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
datasetKeyExtractor(item: Object) {
|
|
|
|
return item.text
|
|
|
|
}
|
|
|
|
|
2020-03-07 09:15:25 +01:00
|
|
|
showSnackBar() {
|
|
|
|
this.setState({snackbarVisible: true})
|
|
|
|
}
|
|
|
|
|
|
|
|
hideSnackBar() {
|
|
|
|
this.setState({snackbarVisible: false})
|
|
|
|
}
|
|
|
|
|
2020-03-05 19:54:56 +01:00
|
|
|
render() {
|
|
|
|
let dataset = this.props.createDataset(this.state.fetchedData);
|
|
|
|
const isEmpty = dataset[0].data.length === 0;
|
2020-03-08 00:05:41 +01:00
|
|
|
const shouldRenderHeader = !isEmpty && (this.props.renderSectionHeader !== null);
|
2020-03-05 19:54:56 +01:00
|
|
|
if (isEmpty)
|
|
|
|
dataset = this.createEmptyDataset();
|
|
|
|
return (
|
2020-03-07 09:15:25 +01:00
|
|
|
<View>
|
|
|
|
<Snackbar
|
|
|
|
visible={this.state.snackbarVisible}
|
|
|
|
onDismiss={this.hideSnackBar}
|
|
|
|
action={{
|
|
|
|
label: 'OK',
|
|
|
|
onPress: this.hideSnackBar,
|
|
|
|
}}
|
|
|
|
duration={4000}
|
|
|
|
>
|
|
|
|
{i18n.t("homeScreen.listUpdateFail")}
|
|
|
|
</Snackbar>
|
|
|
|
<SectionList
|
|
|
|
sections={dataset}
|
|
|
|
refreshControl={
|
|
|
|
<RefreshControl
|
|
|
|
refreshing={this.state.refreshing}
|
|
|
|
onRefresh={this.onRefresh}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
renderSectionHeader={shouldRenderHeader ? this.props.renderSectionHeader : this.getEmptySectionHeader}
|
|
|
|
renderItem={isEmpty ? this.getEmptyRenderItem : this.props.renderItem}
|
|
|
|
style={{minHeight: 300, width: '100%'}}
|
|
|
|
stickySectionHeadersEnabled={this.props.stickyHeader}
|
|
|
|
contentContainerStyle={
|
|
|
|
isEmpty ?
|
|
|
|
{flexGrow: 1, justifyContent: 'center', alignItems: 'center'} : {}
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</View>
|
2020-03-05 19:54:56 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|