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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. * This screen fetches data from etud to render the RU menu
  15. */
  16. class SelfMenuScreen extends React.Component<Props> {
  17. getRenderItem: Function;
  18. getRenderSectionHeader: Function;
  19. createDataset: Function;
  20. colors: Object;
  21. constructor(props) {
  22. super(props);
  23. this.getRenderItem = this.getRenderItem.bind(this);
  24. this.getRenderSectionHeader = this.getRenderSectionHeader.bind(this);
  25. this.createDataset = this.createDataset.bind(this);
  26. this.colors = props.theme.colors;
  27. }
  28. getKeyExtractor(item: Object) {
  29. return item !== undefined ? item['name'] : undefined;
  30. }
  31. createDataset(fetchedData: Object) {
  32. let result = [];
  33. // Prevent crash by giving a default value when fetchedData is empty (not yet available)
  34. if (Object.keys(fetchedData).length === 0) {
  35. result = [
  36. {
  37. title: '',
  38. data: [],
  39. extraData: super.state,
  40. keyExtractor: this.getKeyExtractor
  41. }
  42. ];
  43. }
  44. if (AprilFoolsManager.getInstance().isAprilFoolsEnabled() && fetchedData.length > 0)
  45. fetchedData[0].meal[0].foodcategory = AprilFoolsManager.getFakeMenuItem(fetchedData[0].meal[0].foodcategory);
  46. // fetched data is an array here
  47. for (let i = 0; i < fetchedData.length; i++) {
  48. result.push(
  49. {
  50. title: DateManager.getInstance().getTranslatedDate(fetchedData[i].date),
  51. data: fetchedData[i].meal[0].foodcategory,
  52. extraData: super.state,
  53. keyExtractor: this.getKeyExtractor,
  54. }
  55. );
  56. }
  57. return result
  58. }
  59. getRenderSectionHeader({section}: Object) {
  60. return (
  61. <Card style={{
  62. width: '95%',
  63. marginLeft: 'auto',
  64. marginRight: 'auto',
  65. marginTop: 5,
  66. marginBottom: 5,
  67. elevation: 4,
  68. }}>
  69. <Card.Title
  70. title={section.title}
  71. titleStyle={{
  72. textAlign: 'center'
  73. }}
  74. subtitleStyle={{
  75. textAlign: 'center'
  76. }}
  77. style={{
  78. paddingLeft: 0,
  79. }}
  80. />
  81. </Card>
  82. );
  83. }
  84. getRenderItem({item}: Object) {
  85. return (
  86. <Card style={{
  87. flex: 0,
  88. marginHorizontal: 10,
  89. marginVertical: 5,
  90. }}>
  91. <Card.Title
  92. style={{marginTop: 5}}
  93. title={item.name}
  94. />
  95. <View style={{
  96. width: '80%',
  97. marginLeft: 'auto',
  98. marginRight: 'auto',
  99. borderBottomWidth: 1,
  100. borderBottomColor: this.colors.primary,
  101. marginTop: 5,
  102. marginBottom: 5,
  103. }}/>
  104. <Card.Content>
  105. {item.dishes.map((object) =>
  106. <View>
  107. {object.name !== "" ?
  108. <Text style={{
  109. marginTop: 5,
  110. marginBottom: 5,
  111. textAlign: 'center'
  112. }}>{this.formatName(object.name)}</Text>
  113. : <View/>}
  114. </View>)}
  115. </Card.Content>
  116. </Card>
  117. );
  118. }
  119. formatName(name: String) {
  120. return name.charAt(0) + name.substr(1).toLowerCase();
  121. }
  122. render() {
  123. const nav = this.props.navigation;
  124. return (
  125. <WebSectionList
  126. createDataset={this.createDataset}
  127. navigation={nav}
  128. autoRefreshTime={0}
  129. refreshOnFocus={false}
  130. fetchUrl={DATA_URL}
  131. renderItem={this.getRenderItem}
  132. renderSectionHeader={this.getRenderSectionHeader}
  133. stickyHeader={true}/>
  134. );
  135. }
  136. }
  137. export default withTheme(SelfMenuScreen);