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.

SelfMenuScreen.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // @flow
  2. import * as React from 'react';
  3. import {View} from 'react-native';
  4. import DateManager from "../managers/DateManager";
  5. import WebSectionList from "../components/Lists/WebSectionList";
  6. import {Card, Text, withTheme} from 'react-native-paper';
  7. import AprilFoolsManager from "../managers/AprilFoolsManager";
  8. const DATA_URL = "https://etud.insa-toulouse.fr/~amicale_app/menu/menu_data.json";
  9. type Props = {
  10. navigation: Object,
  11. }
  12. /**
  13. * Class defining the app's menu screen.
  14. */
  15. class SelfMenuScreen extends React.Component<Props> {
  16. getRenderItem: Function;
  17. getRenderSectionHeader: Function;
  18. createDataset: Function;
  19. colors: Object;
  20. constructor(props) {
  21. super(props);
  22. this.getRenderItem = this.getRenderItem.bind(this);
  23. this.getRenderSectionHeader = this.getRenderSectionHeader.bind(this);
  24. this.createDataset = this.createDataset.bind(this);
  25. this.colors = props.theme.colors;
  26. }
  27. /**
  28. * Extract a key for the given item
  29. *
  30. * @param item The item to extract the key from
  31. * @return {*} The extracted key
  32. */
  33. getKeyExtractor(item: Object) {
  34. return item !== undefined ? item['name'] : undefined;
  35. }
  36. /**
  37. * Creates the dataset to be used in the FlatList
  38. *
  39. * @param fetchedData
  40. * @return {[]}
  41. */
  42. createDataset(fetchedData: Object) {
  43. let result = [];
  44. // Prevent crash by giving a default value when fetchedData is empty (not yet available)
  45. if (Object.keys(fetchedData).length === 0) {
  46. result = [
  47. {
  48. title: '',
  49. data: [],
  50. keyExtractor: this.getKeyExtractor
  51. }
  52. ];
  53. }
  54. if (AprilFoolsManager.getInstance().isAprilFoolsEnabled() && fetchedData.length > 0)
  55. fetchedData[0].meal[0].foodcategory = AprilFoolsManager.getFakeMenuItem(fetchedData[0].meal[0].foodcategory);
  56. // fetched data is an array here
  57. for (let i = 0; i < fetchedData.length; i++) {
  58. result.push(
  59. {
  60. title: DateManager.getInstance().getTranslatedDate(fetchedData[i].date),
  61. data: fetchedData[i].meal[0].foodcategory,
  62. keyExtractor: this.getKeyExtractor,
  63. }
  64. );
  65. }
  66. return result
  67. }
  68. /**
  69. * Gets the render section header
  70. *
  71. * @param section The section to render the header from
  72. * @return {*}
  73. */
  74. getRenderSectionHeader({section}: Object) {
  75. return (
  76. <Card style={{
  77. width: '95%',
  78. marginLeft: 'auto',
  79. marginRight: 'auto',
  80. marginTop: 5,
  81. marginBottom: 5,
  82. elevation: 4,
  83. }}>
  84. <Card.Title
  85. title={section.title}
  86. titleStyle={{
  87. textAlign: 'center'
  88. }}
  89. subtitleStyle={{
  90. textAlign: 'center'
  91. }}
  92. style={{
  93. paddingLeft: 0,
  94. }}
  95. />
  96. </Card>
  97. );
  98. }
  99. /**
  100. * Gets a FlatList render item
  101. *
  102. * @param item The item to render
  103. * @return {*}
  104. */
  105. getRenderItem({item}: Object) {
  106. return (
  107. <Card style={{
  108. flex: 0,
  109. marginHorizontal: 10,
  110. marginVertical: 5,
  111. }}>
  112. <Card.Title
  113. style={{marginTop: 5}}
  114. title={item.name}
  115. />
  116. <View style={{
  117. width: '80%',
  118. marginLeft: 'auto',
  119. marginRight: 'auto',
  120. borderBottomWidth: 1,
  121. borderBottomColor: this.colors.primary,
  122. marginTop: 5,
  123. marginBottom: 5,
  124. }}/>
  125. <Card.Content>
  126. {item.dishes.map((object) =>
  127. <View>
  128. {object.name !== "" ?
  129. <Text style={{
  130. marginTop: 5,
  131. marginBottom: 5,
  132. textAlign: 'center'
  133. }}>{this.formatName(object.name)}</Text>
  134. : <View/>}
  135. </View>)}
  136. </Card.Content>
  137. </Card>
  138. );
  139. }
  140. /**
  141. * Formats the given string to make sure it starts with a capital letter
  142. *
  143. * @param name The string to format
  144. * @return {string} The formatted string
  145. */
  146. formatName(name: String) {
  147. return name.charAt(0) + name.substr(1).toLowerCase();
  148. }
  149. render() {
  150. const nav = this.props.navigation;
  151. return (
  152. <WebSectionList
  153. createDataset={this.createDataset}
  154. navigation={nav}
  155. autoRefreshTime={0}
  156. refreshOnFocus={false}
  157. fetchUrl={DATA_URL}
  158. renderItem={this.getRenderItem}
  159. renderSectionHeader={this.getRenderSectionHeader}
  160. stickyHeader={true}/>
  161. );
  162. }
  163. }
  164. export default withTheme(SelfMenuScreen);