Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WebSectionList.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // @flow
  2. import * as React from 'react';
  3. import WebDataManager from "../utils/WebDataManager";
  4. import i18n from "i18n-js";
  5. import {Snackbar} from 'react-native-paper';
  6. import {RefreshControl, SectionList, View} from "react-native";
  7. import EmptyWebSectionListItem from "./EmptyWebSectionListItem";
  8. type Props = {
  9. navigation: Object,
  10. fetchUrl: string,
  11. autoRefreshTime: number,
  12. refreshOnFocus: boolean,
  13. renderItem: React.Node,
  14. renderSectionHeader: React.Node,
  15. stickyHeader: boolean,
  16. createDataset: Function,
  17. updateData: number,
  18. }
  19. type State = {
  20. refreshing: boolean,
  21. firstLoading: boolean,
  22. fetchedData: Object,
  23. snackbarVisible: boolean
  24. };
  25. const MIN_REFRESH_TIME = 5 * 1000;
  26. /**
  27. * This is a pure component, meaning it will only update if a shallow comparison of state and props is different.
  28. * To force the component to update, change the value of updateData.
  29. */
  30. export default class WebSectionList extends React.PureComponent<Props, State> {
  31. static defaultProps = {
  32. renderSectionHeader: null,
  33. stickyHeader: false,
  34. updateData: 0,
  35. };
  36. webDataManager: WebDataManager;
  37. refreshInterval: IntervalID;
  38. lastRefresh: Date;
  39. state = {
  40. refreshing: false,
  41. firstLoading: true,
  42. fetchedData: {},
  43. snackbarVisible: false
  44. };
  45. onRefresh: Function;
  46. onFetchSuccess: Function;
  47. onFetchError: Function;
  48. getEmptyRenderItem: Function;
  49. getEmptySectionHeader: Function;
  50. showSnackBar: Function;
  51. hideSnackBar: Function;
  52. constructor() {
  53. super();
  54. // creating references to functions used in render()
  55. this.onRefresh = this.onRefresh.bind(this);
  56. this.onFetchSuccess = this.onFetchSuccess.bind(this);
  57. this.onFetchError = this.onFetchError.bind(this);
  58. this.getEmptyRenderItem = this.getEmptyRenderItem.bind(this);
  59. this.getEmptySectionHeader = this.getEmptySectionHeader.bind(this);
  60. this.showSnackBar = this.showSnackBar.bind(this);
  61. this.hideSnackBar = this.hideSnackBar.bind(this);
  62. }
  63. /**
  64. * Register react navigation events on first screen load.
  65. * Allows to detect when the screen is focused
  66. */
  67. componentDidMount() {
  68. this.webDataManager = new WebDataManager(this.props.fetchUrl);
  69. const onScreenFocus = this.onScreenFocus.bind(this);
  70. const onScreenBlur = this.onScreenBlur.bind(this);
  71. this.props.navigation.addListener('focus', onScreenFocus);
  72. this.props.navigation.addListener('blur', onScreenBlur);
  73. this.onRefresh();
  74. }
  75. /**
  76. * Refresh data when focusing the screen and setup a refresh interval if asked to
  77. */
  78. onScreenFocus() {
  79. if (this.props.refreshOnFocus && this.lastRefresh !== undefined)
  80. this.onRefresh();
  81. if (this.props.autoRefreshTime > 0)
  82. this.refreshInterval = setInterval(this.onRefresh, this.props.autoRefreshTime)
  83. }
  84. /**
  85. * Remove any interval on un-focus
  86. */
  87. onScreenBlur() {
  88. clearInterval(this.refreshInterval);
  89. }
  90. onFetchSuccess(fetchedData: Object) {
  91. this.setState({
  92. fetchedData: fetchedData,
  93. refreshing: false,
  94. firstLoading: false
  95. });
  96. this.lastRefresh = new Date();
  97. }
  98. onFetchError() {
  99. this.setState({
  100. fetchedData: {},
  101. refreshing: false,
  102. firstLoading: false
  103. });
  104. this.showSnackBar();
  105. // this.webDataManager.showUpdateToast(this.props.updateErrorText);
  106. }
  107. /**
  108. * Refresh data and show a toast if any error occurred
  109. * @private
  110. */
  111. onRefresh() {
  112. let canRefresh;
  113. if (this.lastRefresh !== undefined)
  114. canRefresh = (new Date().getTime() - this.lastRefresh.getTime()) > MIN_REFRESH_TIME;
  115. else
  116. canRefresh = true;
  117. if (canRefresh) {
  118. this.setState({refreshing: true});
  119. this.webDataManager.readData()
  120. .then(this.onFetchSuccess)
  121. .catch(this.onFetchError);
  122. }
  123. }
  124. getEmptySectionHeader({section}: Object) {
  125. return <View/>;
  126. }
  127. getEmptyRenderItem({item}: Object) {
  128. return (
  129. <EmptyWebSectionListItem
  130. text={item.text}
  131. icon={item.icon}
  132. refreshing={this.state.refreshing}
  133. />
  134. );
  135. }
  136. createEmptyDataset() {
  137. return [
  138. {
  139. title: '',
  140. data: [
  141. {
  142. text: this.state.refreshing ?
  143. i18n.t('general.loading') :
  144. i18n.t('general.networkError'),
  145. isSpinner: this.state.refreshing,
  146. icon: this.state.refreshing ?
  147. 'refresh' :
  148. 'access-point-network-off'
  149. }
  150. ],
  151. keyExtractor: this.datasetKeyExtractor,
  152. }
  153. ];
  154. }
  155. datasetKeyExtractor(item: Object) {
  156. return item.text
  157. }
  158. showSnackBar() {
  159. this.setState({snackbarVisible: true})
  160. }
  161. hideSnackBar() {
  162. this.setState({snackbarVisible: false})
  163. }
  164. render() {
  165. let dataset = this.props.createDataset(this.state.fetchedData);
  166. const isEmpty = dataset[0].data.length === 0;
  167. const shouldRenderHeader = !isEmpty && (this.props.renderSectionHeader !== null);
  168. if (isEmpty)
  169. dataset = this.createEmptyDataset();
  170. return (
  171. <View>
  172. <Snackbar
  173. visible={this.state.snackbarVisible}
  174. onDismiss={this.hideSnackBar}
  175. action={{
  176. label: 'OK',
  177. onPress: this.hideSnackBar,
  178. }}
  179. duration={4000}
  180. >
  181. {i18n.t("homeScreen.listUpdateFail")}
  182. </Snackbar>
  183. <SectionList
  184. sections={dataset}
  185. refreshControl={
  186. <RefreshControl
  187. refreshing={this.state.refreshing}
  188. onRefresh={this.onRefresh}
  189. />
  190. }
  191. //$FlowFixMe
  192. renderSectionHeader={shouldRenderHeader ? this.props.renderSectionHeader : this.getEmptySectionHeader}
  193. //$FlowFixMe
  194. renderItem={isEmpty ? this.getEmptyRenderItem : this.props.renderItem}
  195. style={{minHeight: 300, width: '100%'}}
  196. stickySectionHeadersEnabled={this.props.stickyHeader}
  197. contentContainerStyle={
  198. isEmpty ?
  199. {flexGrow: 1, justifyContent: 'center', alignItems: 'center'} : {}
  200. }
  201. />
  202. </View>
  203. );
  204. }
  205. }