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

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