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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // @flow
  2. import * as React from 'react';
  3. import WebDataManager from "../utils/WebDataManager";
  4. import {Container, Content, 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. type Props = {
  9. navigation: Object,
  10. }
  11. type State = {
  12. refreshing: boolean,
  13. firstLoading: boolean,
  14. fetchedData: Object,
  15. machinesWatched: Array<Object>
  16. };
  17. export default class FetchedDataSectionList extends React.Component<Props, State> {
  18. webDataManager: WebDataManager;
  19. constructor() {
  20. super();
  21. this.webDataManager = new WebDataManager(this.getFetchUrl());
  22. }
  23. state = {
  24. refreshing: false,
  25. firstLoading: true,
  26. fetchedData: {},
  27. machinesWatched: [],
  28. };
  29. getFetchUrl() {
  30. return "";
  31. }
  32. getHeaderTranslation() {
  33. return "Header";
  34. }
  35. getUpdateToastTranslations() {
  36. return ["whoa", "nah"];
  37. }
  38. shouldShowUpdateToast() {
  39. return true;
  40. }
  41. /**
  42. * Refresh the FetchedData on first screen load
  43. */
  44. componentDidMount() {
  45. this._onRefresh();
  46. }
  47. _onRefresh = () => {
  48. this.setState({refreshing: true});
  49. this.webDataManager.readData().then((fetchedData) => {
  50. this.setState({
  51. fetchedData: fetchedData,
  52. refreshing: false,
  53. firstLoading: false
  54. });
  55. if (this.shouldShowUpdateToast())
  56. this.webDataManager.showUpdateToast(this.getUpdateToastTranslations()[0], this.getUpdateToastTranslations()[1]);
  57. });
  58. };
  59. getRenderItem(item: Object, section: Object, data: Object) {
  60. return <View/>;
  61. }
  62. getRenderSectionHeader(title: String) {
  63. return <View/>;
  64. }
  65. /**
  66. * Create the dataset to be used in the list from the data fetched
  67. * @param fetchedData {Object}
  68. * @return {Array}
  69. */
  70. createDataset(fetchedData: Object): Array<Object> {
  71. return [];
  72. }
  73. /**
  74. * What item field should be used as a key in the list
  75. * @param item {Object}
  76. * @return {*}
  77. */
  78. getKeyExtractor(item: Object) {
  79. return item.id;
  80. }
  81. hasTabs() {
  82. return false;
  83. }
  84. getSectionList(dataset: Array<Object>) {
  85. return (
  86. <SectionList
  87. sections={dataset}
  88. keyExtractor={(item) => this.getKeyExtractor(item)}
  89. refreshControl={
  90. <RefreshControl
  91. refreshing={this.state.refreshing}
  92. onRefresh={this._onRefresh}
  93. />
  94. }
  95. renderSectionHeader={({section: {title}}) =>
  96. this.getRenderSectionHeader(title)
  97. }
  98. renderItem={({item, section}) =>
  99. this.getRenderItem(item, section, dataset)
  100. }
  101. style={{minHeight: 300, width: '100%'}}
  102. />
  103. );
  104. }
  105. getTabbedView(dataset: Array<Object>) {
  106. let tabbedView = [];
  107. for (let i = 0; i < dataset.length; i++) {
  108. tabbedView.push(
  109. <Tab heading={
  110. <TabHeading>
  111. <CustomMaterialIcon icon={dataset[i].icon}
  112. color={'#fff'}
  113. fontSize={20}
  114. />
  115. <Text>{dataset[i].title}</Text>
  116. </TabHeading>}>
  117. <Content padder>
  118. {this.getSectionList(
  119. [
  120. {
  121. title: dataset[i].title,
  122. data: dataset[i].data,
  123. extraData: dataset[i].extraData,
  124. }
  125. ]
  126. )}
  127. </Content>
  128. </Tab>);
  129. }
  130. return tabbedView;
  131. }
  132. render() {
  133. const nav = this.props.navigation;
  134. const dataset = this.createDataset(this.state.fetchedData);
  135. return (
  136. <Container>
  137. <CustomHeader navigation={nav} title={this.getHeaderTranslation()}/>
  138. {this.hasTabs() ?
  139. <Tabs>
  140. {this.getTabbedView(dataset)}
  141. </Tabs>
  142. :
  143. <Content padder>
  144. {this.getSectionList(dataset)}
  145. </Content>
  146. }
  147. </Container>
  148. );
  149. }
  150. }