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.tsx 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import * as React from 'react';
  20. import { SectionListData, StyleSheet, View } from 'react-native';
  21. import { Card, Text, withTheme } from 'react-native-paper';
  22. import { StackNavigationProp } from '@react-navigation/stack';
  23. import i18n from 'i18n-js';
  24. import DateManager from '../../managers/DateManager';
  25. import WebSectionList from '../../components/Screens/WebSectionList';
  26. import type { SectionListDataType } from '../../components/Screens/WebSectionList';
  27. import Urls from '../../constants/Urls';
  28. import { readData } from '../../utils/WebData';
  29. import { REQUEST_STATUS } from '../../utils/Requests';
  30. type PropsType = {
  31. navigation: StackNavigationProp<any>;
  32. theme: ReactNativePaper.Theme;
  33. };
  34. export type RuFoodCategoryType = {
  35. name: string;
  36. dishes: Array<{ name: string }>;
  37. };
  38. type RuMealType = {
  39. name: string;
  40. foodcategory: Array<RuFoodCategoryType>;
  41. };
  42. type RawRuMenuType = {
  43. restaurant_id: number;
  44. id: number;
  45. date: string;
  46. meal: Array<RuMealType>;
  47. };
  48. const styles = StyleSheet.create({
  49. itemCard: {
  50. flex: 0,
  51. marginHorizontal: 10,
  52. marginVertical: 5,
  53. },
  54. headerCard: {
  55. width: '95%',
  56. marginLeft: 'auto',
  57. marginRight: 'auto',
  58. marginTop: 5,
  59. marginBottom: 5,
  60. elevation: 4,
  61. },
  62. text: {
  63. textAlign: 'center',
  64. },
  65. title: {
  66. paddingLeft: 0,
  67. },
  68. itemTitle: {
  69. marginTop: 5,
  70. },
  71. item: {
  72. width: '80%',
  73. marginLeft: 'auto',
  74. marginRight: 'auto',
  75. borderBottomWidth: 1,
  76. marginTop: 5,
  77. marginBottom: 5,
  78. },
  79. itemText: {
  80. marginTop: 5,
  81. marginBottom: 5,
  82. textAlign: 'center',
  83. },
  84. });
  85. /**
  86. * Class defining the app's menu screen.
  87. */
  88. class SelfMenuScreen extends React.Component<PropsType> {
  89. /**
  90. * Formats the given string to make sure it starts with a capital letter
  91. *
  92. * @param name The string to format
  93. * @return {string} The formatted string
  94. */
  95. static formatName(name: string): string {
  96. return name.charAt(0) + name.substr(1).toLowerCase();
  97. }
  98. /**
  99. * Creates the dataset to be used in the FlatList
  100. *
  101. * @param fetchedData
  102. * @return {[]}
  103. */
  104. createDataset = (
  105. fetchedData: Array<RawRuMenuType> | undefined,
  106. _loading: boolean,
  107. _lastRefreshDate: Date | undefined,
  108. _refreshData: (newRequest?: () => Promise<Array<RawRuMenuType>>) => void,
  109. status: REQUEST_STATUS
  110. ): SectionListDataType<RuFoodCategoryType> => {
  111. let result: SectionListDataType<RuFoodCategoryType> = [];
  112. if (status === REQUEST_STATUS.SUCCESS) {
  113. if (fetchedData == null || fetchedData.length === 0) {
  114. result = [
  115. {
  116. title: i18n.t('general.notAvailable'),
  117. data: [],
  118. keyExtractor: this.getKeyExtractor,
  119. },
  120. ];
  121. } else {
  122. fetchedData.forEach((item: RawRuMenuType) => {
  123. result.push({
  124. title: DateManager.getInstance().getTranslatedDate(item.date),
  125. data: item.meal[0].foodcategory,
  126. keyExtractor: this.getKeyExtractor,
  127. });
  128. });
  129. }
  130. }
  131. return result;
  132. };
  133. /**
  134. * Gets the render section header
  135. *
  136. * @param section The section to render the header from
  137. * @return {*}
  138. */
  139. getRenderSectionHeader = ({
  140. section,
  141. }: {
  142. section: SectionListData<RuFoodCategoryType>;
  143. }) => {
  144. return (
  145. <Card style={styles.headerCard}>
  146. <Card.Title
  147. title={section.title}
  148. titleStyle={styles.text}
  149. subtitleStyle={styles.text}
  150. style={styles.title}
  151. />
  152. </Card>
  153. );
  154. };
  155. /**
  156. * Gets a FlatList render item
  157. *
  158. * @param item The item to render
  159. * @return {*}
  160. */
  161. getRenderItem = ({ item }: { item: RuFoodCategoryType }) => {
  162. const { theme } = this.props;
  163. return (
  164. <Card style={styles.itemCard}>
  165. <Card.Title style={styles.itemTitle} title={item.name} />
  166. <View
  167. style={{
  168. borderBottomColor: theme.colors.primary,
  169. ...styles.item,
  170. }}
  171. />
  172. <Card.Content>
  173. {item.dishes.map((object: { name: string }) =>
  174. object.name !== '' ? (
  175. <Text style={styles.itemText}>
  176. {SelfMenuScreen.formatName(object.name)}
  177. </Text>
  178. ) : null
  179. )}
  180. </Card.Content>
  181. </Card>
  182. );
  183. };
  184. /**
  185. * Extract a key for the given item
  186. *
  187. * @param item The item to extract the key from
  188. * @return {*} The extracted key
  189. */
  190. getKeyExtractor = (item: RuFoodCategoryType): string => item.name;
  191. render() {
  192. return (
  193. <WebSectionList
  194. request={() => readData<Array<RawRuMenuType>>(Urls.app.menu)}
  195. createDataset={this.createDataset}
  196. refreshOnFocus={true}
  197. renderItem={this.getRenderItem}
  198. renderSectionHeader={this.getRenderSectionHeader}
  199. stickySectionHeadersEnabled={true}
  200. />
  201. );
  202. }
  203. }
  204. export default withTheme(SelfMenuScreen);