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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // @flow
  2. import * as React from 'react';
  3. import {View} from 'react-native';
  4. import {Text, H2, H3, Card, CardItem} from 'native-base';
  5. import ThemeManager from "../utils/ThemeManager";
  6. import i18n from "i18n-js";
  7. import FetchedDataSectionList from "../components/FetchedDataSectionList";
  8. import LocaleManager from "../utils/LocaleManager";
  9. const DATA_URL = "https://srv-falcon.etud.insa-toulouse.fr/~amicale_app/menu/menu_data.json";
  10. /**
  11. * Class defining the app's menu screen.
  12. * This screen fetches data from etud to render the RU menu
  13. */
  14. export default class SelfMenuScreen extends FetchedDataSectionList {
  15. // Hard code strings as toLocaleDateString does not work on current android JS engine
  16. daysOfWeek = [];
  17. monthsOfYear = [];
  18. constructor() {
  19. super(DATA_URL, 0);
  20. this.daysOfWeek.push(i18n.t("date.daysOfWeek.monday"));
  21. this.daysOfWeek.push(i18n.t("date.daysOfWeek.tuesday"));
  22. this.daysOfWeek.push(i18n.t("date.daysOfWeek.wednesday"));
  23. this.daysOfWeek.push(i18n.t("date.daysOfWeek.thursday"));
  24. this.daysOfWeek.push(i18n.t("date.daysOfWeek.friday"));
  25. this.daysOfWeek.push(i18n.t("date.daysOfWeek.saturday"));
  26. this.daysOfWeek.push(i18n.t("date.daysOfWeek.sunday"));
  27. this.monthsOfYear.push(i18n.t("date.monthsOfYear.january"));
  28. this.monthsOfYear.push(i18n.t("date.monthsOfYear.february"));
  29. this.monthsOfYear.push(i18n.t("date.monthsOfYear.march"));
  30. this.monthsOfYear.push(i18n.t("date.monthsOfYear.april"));
  31. this.monthsOfYear.push(i18n.t("date.monthsOfYear.may"));
  32. this.monthsOfYear.push(i18n.t("date.monthsOfYear.june"));
  33. this.monthsOfYear.push(i18n.t("date.monthsOfYear.july"));
  34. this.monthsOfYear.push(i18n.t("date.monthsOfYear.august"));
  35. this.monthsOfYear.push(i18n.t("date.monthsOfYear.september"));
  36. this.monthsOfYear.push(i18n.t("date.monthsOfYear.october"));
  37. this.monthsOfYear.push(i18n.t("date.monthsOfYear.november"));
  38. this.monthsOfYear.push(i18n.t("date.monthsOfYear.december"));
  39. }
  40. getHeaderTranslation() {
  41. return i18n.t("screens.menuSelf");
  42. }
  43. getUpdateToastTranslations() {
  44. return [i18n.t("homeScreen.listUpdated"), i18n.t("homeScreen.listUpdateFail")];
  45. }
  46. getKeyExtractor(item: Object) {
  47. return item !== undefined ? item.name : undefined;
  48. }
  49. hasBackButton() {
  50. return true;
  51. }
  52. hasStickyHeader(): boolean {
  53. return true;
  54. }
  55. createDataset(fetchedData: Object) {
  56. let result = [];
  57. // Prevent crash by giving a default value when fetchedData is empty (not yet available)
  58. if (Object.keys(fetchedData).length === 0) {
  59. result = [
  60. {
  61. title: '',
  62. data: {},
  63. extraData: super.state,
  64. keyExtractor: this.getKeyExtractor
  65. }
  66. ];
  67. }
  68. // fetched data is an array here
  69. for (let i = 0; i < fetchedData.length; i++) {
  70. result.push(
  71. {
  72. title: this.getFormattedDate(fetchedData[i].date),
  73. data: fetchedData[i].meal[0].foodcategory,
  74. extraData: super.state,
  75. keyExtractor: this.getKeyExtractor
  76. }
  77. );
  78. }
  79. return result
  80. }
  81. getFormattedDate(dateString: string) {
  82. let dateArray = dateString.split('-');
  83. let date = new Date();
  84. date.setFullYear(parseInt(dateArray[0]), parseInt(dateArray[1]) - 1, parseInt(dateArray[2]));
  85. return this.daysOfWeek[date.getDay() - 1] + " " + date.getDate() + " " + this.monthsOfYear[date.getMonth()] + " " + date.getFullYear();
  86. }
  87. getRenderSectionHeader(title: string) {
  88. return (
  89. <Card style={{
  90. marginLeft: 10,
  91. marginRight: 10,
  92. marginTop: 10,
  93. marginBottom: 10,
  94. }}>
  95. <H2 style={{
  96. textAlign: 'center',
  97. marginTop: 10,
  98. marginBottom: 10
  99. }}>{title}</H2>
  100. </Card>
  101. );
  102. }
  103. getRenderItem(item: Object, section: Object, data: Object) {
  104. return (
  105. <Card style={{
  106. flex: 0,
  107. marginLeft: 20,
  108. marginRight: 20
  109. }}>
  110. <CardItem style={{
  111. paddingBottom: 0,
  112. flexDirection: 'column'
  113. }}>
  114. <H3 style={{
  115. marginTop: 10,
  116. marginBottom: 0,
  117. color: ThemeManager.getCurrentThemeVariables().listNoteColor
  118. }}>{item.name}</H3>
  119. <View style={{
  120. width: '80%',
  121. marginLeft: 'auto',
  122. marginRight: 'auto',
  123. borderBottomWidth: 1,
  124. borderBottomColor: ThemeManager.getCurrentThemeVariables().listBorderColor,
  125. marginTop: 10,
  126. marginBottom: 5,
  127. }}/>
  128. </CardItem>
  129. <CardItem style={{
  130. flexDirection: 'column',
  131. paddingTop: 0,
  132. }}>
  133. {item.dishes.map((object, i) =>
  134. <View>
  135. {object.name !== "" ?
  136. <Text style={{
  137. marginTop: 5,
  138. marginBottom: 5
  139. }}>{object.name.toLowerCase()}</Text>
  140. : <View/>}
  141. </View>)}
  142. </CardItem>
  143. </Card>
  144. );
  145. }
  146. }