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.6KB

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