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.

SelfMenuScreen.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // @flow
  2. import * as React from 'react';
  3. import {View} from 'react-native';
  4. import DateManager from "../utils/DateManager";
  5. import WebSectionList from "../components/WebSectionList";
  6. import {Card, Text, withTheme} from 'react-native-paper';
  7. import AprilFoolsManager from "../utils/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. extraData: super.state,
  51. keyExtractor: this.getKeyExtractor
  52. }
  53. ];
  54. }
  55. if (AprilFoolsManager.getInstance().isAprilFoolsEnabled() && fetchedData.length > 0)
  56. fetchedData[0].meal[0].foodcategory = AprilFoolsManager.getFakeMenuItem(fetchedData[0].meal[0].foodcategory);
  57. // fetched data is an array here
  58. for (let i = 0; i < fetchedData.length; i++) {
  59. result.push(
  60. {
  61. title: DateManager.getInstance().getTranslatedDate(fetchedData[i].date),
  62. data: fetchedData[i].meal[0].foodcategory,
  63. extraData: super.state,
  64. keyExtractor: this.getKeyExtractor,
  65. }
  66. );
  67. }
  68. return result
  69. }
  70. /**
  71. * Gets the render section header
  72. *
  73. * @param section The section to render the header from
  74. * @return {*}
  75. */
  76. getRenderSectionHeader({section}: Object) {
  77. return (
  78. <Card style={{
  79. width: '95%',
  80. marginLeft: 'auto',
  81. marginRight: 'auto',
  82. marginTop: 5,
  83. marginBottom: 5,
  84. elevation: 4,
  85. }}>
  86. <Card.Title
  87. title={section.title}
  88. titleStyle={{
  89. textAlign: 'center'
  90. }}
  91. subtitleStyle={{
  92. textAlign: 'center'
  93. }}
  94. style={{
  95. paddingLeft: 0,
  96. }}
  97. />
  98. </Card>
  99. );
  100. }
  101. /**
  102. * Gets a FlatList render item
  103. *
  104. * @param item The item to render
  105. * @return {*}
  106. */
  107. getRenderItem({item}: Object) {
  108. return (
  109. <Card style={{
  110. flex: 0,
  111. marginHorizontal: 10,
  112. marginVertical: 5,
  113. }}>
  114. <Card.Title
  115. style={{marginTop: 5}}
  116. title={item.name}
  117. />
  118. <View style={{
  119. width: '80%',
  120. marginLeft: 'auto',
  121. marginRight: 'auto',
  122. borderBottomWidth: 1,
  123. borderBottomColor: this.colors.primary,
  124. marginTop: 5,
  125. marginBottom: 5,
  126. }}/>
  127. <Card.Content>
  128. {item.dishes.map((object) =>
  129. <View>
  130. {object.name !== "" ?
  131. <Text style={{
  132. marginTop: 5,
  133. marginBottom: 5,
  134. textAlign: 'center'
  135. }}>{this.formatName(object.name)}</Text>
  136. : <View/>}
  137. </View>)}
  138. </Card.Content>
  139. </Card>
  140. );
  141. }
  142. /**
  143. * Formats the given string to make sure it starts with a capital letter
  144. *
  145. * @param name The string to format
  146. * @return {string} The formatted string
  147. */
  148. formatName(name: String) {
  149. return name.charAt(0) + name.substr(1).toLowerCase();
  150. }
  151. render() {
  152. const nav = this.props.navigation;
  153. return (
  154. <WebSectionList
  155. createDataset={this.createDataset}
  156. navigation={nav}
  157. autoRefreshTime={0}
  158. refreshOnFocus={false}
  159. fetchUrl={DATA_URL}
  160. renderItem={this.getRenderItem}
  161. renderSectionHeader={this.getRenderSectionHeader}
  162. stickyHeader={true}/>
  163. );
  164. }
  165. }
  166. export default withTheme(SelfMenuScreen);