Application Android et IOS pour l'amicale des élèves
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.

FetchedDataSectionList.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // @flow
  2. import * as React from 'react';
  3. import WebDataManager from "../utils/WebDataManager";
  4. import {Container, H3, Tab, TabHeading, Tabs, Text} from "native-base";
  5. import CustomHeader from "./CustomHeader";
  6. import {RefreshControl, SectionList, View} from "react-native";
  7. import CustomMaterialIcon from "./CustomMaterialIcon";
  8. import i18n from 'i18n-js';
  9. import ThemeManager from "../utils/ThemeManager";
  10. type Props = {
  11. navigation: Object,
  12. }
  13. type State = {
  14. refreshing: boolean,
  15. firstLoading: boolean,
  16. fetchedData: Object,
  17. machinesWatched: Array<Object>,
  18. };
  19. export default class FetchedDataSectionList extends React.Component<Props, State> {
  20. webDataManager: WebDataManager;
  21. constructor(fetchUrl: string) {
  22. super();
  23. this.webDataManager = new WebDataManager(fetchUrl);
  24. }
  25. state = {
  26. refreshing: false,
  27. firstLoading: true,
  28. fetchedData: {},
  29. machinesWatched: [],
  30. };
  31. getHeaderTranslation() {
  32. return "Header";
  33. }
  34. getUpdateToastTranslations() {
  35. return ["whoa", "nah"];
  36. }
  37. /**
  38. * Refresh the FetchedData on first screen load
  39. */
  40. componentDidMount() {
  41. this._onRefresh();
  42. }
  43. _onRefresh = () => {
  44. this.setState({refreshing: true});
  45. this.webDataManager.readData().then((fetchedData) => {
  46. this.setState({
  47. fetchedData: fetchedData,
  48. refreshing: false,
  49. firstLoading: false
  50. });
  51. this.webDataManager.showUpdateToast(this.getUpdateToastTranslations()[0], this.getUpdateToastTranslations()[1]);
  52. });
  53. };
  54. getRenderItem(item: Object, section: Object, data: Object) {
  55. return <View/>;
  56. }
  57. getRenderSectionHeader(title: String) {
  58. return <View/>;
  59. }
  60. getEmptyRenderItem(text: string, icon: string) {
  61. return (
  62. <View>
  63. <View style={{
  64. justifyContent: 'center',
  65. alignItems: 'center',
  66. width: '100%',
  67. height: 100,
  68. marginBottom: 20
  69. }}>
  70. <CustomMaterialIcon
  71. icon={icon}
  72. fontSize={100}
  73. width={100}
  74. color={ThemeManager.getCurrentThemeVariables().fetchedDataSectionListErrorText}/>
  75. </View>
  76. <H3 style={{
  77. textAlign: 'center',
  78. marginRight: 20,
  79. marginLeft: 20,
  80. color: ThemeManager.getCurrentThemeVariables().fetchedDataSectionListErrorText
  81. }}>
  82. {text}
  83. </H3>
  84. </View>);
  85. }
  86. /**
  87. * Create the dataset to be used in the list from the data fetched
  88. * @param fetchedData {Object}
  89. * @return {Array}
  90. */
  91. createDataset(fetchedData: Object): Array<Object> {
  92. return [];
  93. }
  94. createEmptyDataset() {
  95. return [
  96. {
  97. title: '',
  98. data: [
  99. {
  100. text: this.state.refreshing ?
  101. i18n.t('general.loading') :
  102. i18n.t('general.networkError'),
  103. icon: this.state.refreshing ?
  104. 'refresh' :
  105. 'access-point-network-off'
  106. }
  107. ],
  108. keyExtractor: (item: Object) => item.text,
  109. }
  110. ];
  111. }
  112. hasTabs() {
  113. return false;
  114. }
  115. getSectionList(dataset: Array<Object>) {
  116. let isEmpty = dataset[0].data.length === 0;
  117. if (isEmpty)
  118. dataset = this.createEmptyDataset();
  119. return (
  120. <SectionList
  121. sections={dataset}
  122. refreshControl={
  123. <RefreshControl
  124. refreshing={this.state.refreshing}
  125. onRefresh={this._onRefresh}
  126. />
  127. }
  128. renderSectionHeader={({section: {title}}) =>
  129. isEmpty ?
  130. <View/> :
  131. this.getRenderSectionHeader(title)
  132. }
  133. renderItem={({item, section}) =>
  134. isEmpty ?
  135. this.getEmptyRenderItem(item.text, item.icon) :
  136. this.getRenderItem(item, section, dataset)
  137. }
  138. style={{minHeight: 300, width: '100%'}}
  139. contentContainerStyle={
  140. isEmpty ?
  141. {flexGrow: 1, justifyContent: 'center', alignItems: 'center'} : {}
  142. }
  143. />
  144. );
  145. }
  146. getTabbedView(dataset: Array<Object>) {
  147. let tabbedView = [];
  148. for (let i = 0; i < dataset.length; i++) {
  149. tabbedView.push(
  150. <Tab heading={
  151. <TabHeading>
  152. <CustomMaterialIcon icon={dataset[i].icon}
  153. color={'#fff'}
  154. fontSize={20}
  155. />
  156. <Text>{dataset[i].title}</Text>
  157. </TabHeading>}
  158. key={dataset[i].title}>
  159. {this.getSectionList(
  160. [
  161. {
  162. title: dataset[i].title,
  163. data: dataset[i].data,
  164. extraData: dataset[i].extraData,
  165. keyExtractor: dataset[i].keyExtractor
  166. }
  167. ]
  168. )}
  169. </Tab>);
  170. }
  171. return tabbedView;
  172. }
  173. render() {
  174. const nav = this.props.navigation;
  175. const dataset = this.createDataset(this.state.fetchedData);
  176. return (
  177. <Container>
  178. <CustomHeader navigation={nav} title={this.getHeaderTranslation()}/>
  179. {this.hasTabs() ?
  180. <Tabs>
  181. {this.getTabbedView(dataset)}
  182. </Tabs>
  183. :
  184. this.getSectionList(dataset)
  185. }
  186. </Container>
  187. );
  188. }
  189. }