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.

ProximoMainScreen.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // @flow
  2. import * as React from 'react';
  3. import {Platform, View} from 'react-native'
  4. import {Badge, Body, Left, ListItem, Right, Text} from 'native-base';
  5. import i18n from "i18n-js";
  6. import CustomMaterialIcon from "../../components/CustomMaterialIcon";
  7. import FetchedDataSectionList from "../../components/FetchedDataSectionList";
  8. import ThemeManager from "../../utils/ThemeManager";
  9. import Touchable from "react-native-platform-touchable";
  10. const DATA_URL = "https://srv-falcon.etud.insa-toulouse.fr/~proximo/data/stock-v2.json";
  11. /**
  12. * Class defining the main proximo screen. This screen shows the different categories of articles
  13. * offered by proximo.
  14. */
  15. export default class ProximoMainScreen extends FetchedDataSectionList {
  16. constructor() {
  17. super(DATA_URL, 0);
  18. }
  19. getHeaderTranslation() {
  20. return i18n.t("screens.proximo");
  21. }
  22. getUpdateToastTranslations() {
  23. return [i18n.t("proximoScreen.listUpdated"), i18n.t("proximoScreen.listUpdateFail")];
  24. }
  25. getKeyExtractor(item: Object) {
  26. return item !== undefined ? item.type['id'] : undefined;
  27. }
  28. createDataset(fetchedData: Object) {
  29. return [
  30. {
  31. title: '',
  32. data: this.generateData(fetchedData),
  33. extraData: super.state,
  34. keyExtractor: this.getKeyExtractor
  35. }
  36. ];
  37. }
  38. /**
  39. * Generate the data using types and FetchedData.
  40. * This will group items under the same type.
  41. *
  42. * @param fetchedData The array of articles represented by objects
  43. * @returns {Array} The formatted dataset
  44. */
  45. generateData(fetchedData: Object) {
  46. let finalData = [];
  47. if (fetchedData.types !== undefined && fetchedData.articles !== undefined) {
  48. let types = fetchedData.types;
  49. let articles = fetchedData.articles;
  50. finalData.push({
  51. type: {
  52. id: "0",
  53. name: i18n.t('proximoScreen.all'),
  54. icon: 'star'
  55. },
  56. data: this.getAvailableArticles(articles, undefined)
  57. });
  58. for (let i = 0; i < types.length; i++) {
  59. finalData.push({
  60. type: types[i],
  61. data: this.getAvailableArticles(articles, types[i])
  62. });
  63. }
  64. }
  65. finalData.sort(ProximoMainScreen.sortFinalData);
  66. return finalData;
  67. }
  68. /**
  69. * Get an array of available articles (in stock) of the given type
  70. *
  71. * @param articles The list of all articles
  72. * @param type The type of articles to find (undefined for any type)
  73. * @return {Array} The array of available articles
  74. */
  75. getAvailableArticles(articles: Array<Object>, type: ?Object) {
  76. let availableArticles = [];
  77. for (let k = 0; k < articles.length; k++) {
  78. if ((type !== undefined && type !== null && articles[k]['type'].includes(type['id'])
  79. || type === undefined)
  80. && parseInt(articles[k]['quantity']) > 0) {
  81. availableArticles.push(articles[k]);
  82. }
  83. }
  84. return availableArticles;
  85. }
  86. static sortFinalData(a: Object, b: Object) {
  87. return a.type.id - b.type.id;
  88. }
  89. getRightButton() {
  90. let searchScreenData = {
  91. shouldFocusSearchBar: true,
  92. data: {
  93. type: {
  94. id: "0",
  95. name: i18n.t('proximoScreen.all'),
  96. icon: 'star'
  97. },
  98. data: this.state.fetchedData.articles !== undefined ?
  99. this.getAvailableArticles(this.state.fetchedData.articles, undefined) : []
  100. },
  101. };
  102. return (
  103. <View
  104. style={{
  105. flexDirection: 'row'
  106. }}>
  107. <Touchable
  108. style={{padding: 6}}
  109. onPress={() => this.props.navigation.navigate('ProximoListScreen', searchScreenData)}>
  110. <CustomMaterialIcon
  111. color={Platform.OS === 'ios' ? ThemeManager.getCurrentThemeVariables().brandPrimary : "#fff"}
  112. icon="magnify"/>
  113. </Touchable>
  114. <Touchable
  115. style={{padding: 6}}
  116. onPress={() => this.props.navigation.navigate('ProximoAboutScreen')}>
  117. <CustomMaterialIcon
  118. color={Platform.OS === 'ios' ? ThemeManager.getCurrentThemeVariables().brandPrimary : "#fff"}
  119. icon="information"/>
  120. </Touchable>
  121. </View>
  122. );
  123. }
  124. getRenderItem(item: Object, section: Object, data: Object) {
  125. let dataToSend = {
  126. shouldFocusSearchBar: false,
  127. data: item,
  128. };
  129. if (item.data.length > 0) {
  130. return (
  131. <ListItem
  132. button
  133. thumbnail
  134. onPress={() => {
  135. this.props.navigation.navigate('ProximoListScreen', dataToSend);
  136. }}
  137. >
  138. <Left>
  139. <CustomMaterialIcon
  140. icon={item.type.icon}
  141. fontSize={30}
  142. color={ThemeManager.getCurrentThemeVariables().brandPrimary}
  143. />
  144. </Left>
  145. <Body>
  146. <Text>
  147. {item.type.name}
  148. </Text>
  149. <Text note>
  150. {item.data.length} {item.data.length > 1 ? i18n.t('proximoScreen.articles') : i18n.t('proximoScreen.article')}
  151. </Text>
  152. </Body>
  153. <Right>
  154. <CustomMaterialIcon icon="chevron-right"/>
  155. </Right>
  156. </ListItem>
  157. );
  158. } else {
  159. return <View/>;
  160. }
  161. }
  162. }