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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. hasSideMenu() : boolean {
  56. return false;
  57. }
  58. createDataset(fetchedData: Object) {
  59. let result = [];
  60. // Prevent crash by giving a default value when fetchedData is empty (not yet available)
  61. if (Object.keys(fetchedData).length === 0) {
  62. result = [
  63. {
  64. title: '',
  65. data: [],
  66. extraData: super.state,
  67. keyExtractor: this.getKeyExtractor
  68. }
  69. ];
  70. }
  71. // fetched data is an array here
  72. for (let i = 0; i < fetchedData.length; i++) {
  73. result.push(
  74. {
  75. title: this.getFormattedDate(fetchedData[i].date),
  76. data: fetchedData[i].meal[0].foodcategory,
  77. extraData: super.state,
  78. keyExtractor: this.getKeyExtractor,
  79. }
  80. );
  81. }
  82. return result
  83. }
  84. getFormattedDate(dateString: string) {
  85. let dateArray = dateString.split('-');
  86. let date = new Date();
  87. date.setFullYear(parseInt(dateArray[0]), parseInt(dateArray[1]) - 1, parseInt(dateArray[2]));
  88. return this.daysOfWeek[date.getDay() - 1] + " " + date.getDate() + " " + this.monthsOfYear[date.getMonth()] + " " + date.getFullYear();
  89. }
  90. getRenderSectionHeader(title: string) {
  91. return (
  92. <Card style={{
  93. marginLeft: 10,
  94. marginRight: 10,
  95. marginTop: 10,
  96. marginBottom: 10,
  97. borderRadius: 50
  98. }}>
  99. <H2 style={{
  100. textAlign: 'center',
  101. marginTop: 10,
  102. marginBottom: 10
  103. }}>{title}</H2>
  104. </Card>
  105. );
  106. }
  107. getRenderItem(item: Object, section: Object, data: Object) {
  108. return (
  109. <Card style={{
  110. flex: 0,
  111. marginLeft: 20,
  112. marginRight: 20
  113. }}>
  114. <CardItem style={{
  115. paddingBottom: 0,
  116. flexDirection: 'column'
  117. }}>
  118. <H3 style={{
  119. marginTop: 10,
  120. marginBottom: 0,
  121. color: ThemeManager.getCurrentThemeVariables().listNoteColor
  122. }}>{item.name}</H3>
  123. <View style={{
  124. width: '80%',
  125. marginLeft: 'auto',
  126. marginRight: 'auto',
  127. borderBottomWidth: 1,
  128. borderBottomColor: ThemeManager.getCurrentThemeVariables().listBorderColor,
  129. marginTop: 10,
  130. marginBottom: 5,
  131. }}/>
  132. </CardItem>
  133. <CardItem style={{
  134. flexDirection: 'column',
  135. paddingTop: 0,
  136. }}>
  137. {item.dishes.map((object, i) =>
  138. <View>
  139. {object.name !== "" ?
  140. <Text style={{
  141. marginTop: 5,
  142. marginBottom: 5
  143. }}>{this.formatName(object.name)}</Text>
  144. : <View/>}
  145. </View>)}
  146. </CardItem>
  147. </Card>
  148. );
  149. }
  150. formatName(name: String) {
  151. return name.charAt(0) + name.substr(1).toLowerCase();
  152. }
  153. }