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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // @flow
  2. import * as React from 'react';
  3. import {Platform, View} from 'react-native'
  4. import {Badge, Body, H2, 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: ProximoMainScreen.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. static 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. for (let i = 0; i < types.length; i++) {
  51. finalData.push({
  52. type: types[i],
  53. data: []
  54. });
  55. for (let k = 0; k < articles.length; k++) {
  56. if (articles[k]['type'].includes(types[i].id) && parseInt(articles[k]['quantity']) > 0) {
  57. finalData[i].data.push(articles[k]);
  58. }
  59. }
  60. }
  61. }
  62. finalData.sort(ProximoMainScreen.sortFinalData);
  63. return finalData;
  64. }
  65. static sortFinalData(a: Object, b: Object) {
  66. return a.type.id - b.type.id;
  67. }
  68. getRightButton() {
  69. return (
  70. <Touchable
  71. style={{padding: 6}}
  72. onPress={() => this.props.navigation.navigate('ProximoAboutScreen')}>
  73. <CustomMaterialIcon
  74. color={Platform.OS === 'ios' ? ThemeManager.getCurrentThemeVariables().brandPrimary : "#fff"}
  75. icon="information"/>
  76. </Touchable>
  77. );
  78. }
  79. getRenderItem(item: Object, section: Object, data: Object) {
  80. if (item.data.length > 0) {
  81. return (
  82. <ListItem
  83. button
  84. thumbnail
  85. onPress={() => {
  86. this.props.navigation.navigate('ProximoListScreen', item);
  87. }}
  88. >
  89. <Left>
  90. <CustomMaterialIcon
  91. icon={item.type.icon}
  92. fontSize={30}
  93. />
  94. </Left>
  95. <Body>
  96. <Text>
  97. {item.type.name}
  98. </Text>
  99. <Badge><Text>
  100. {item.data.length} {item.data.length > 1 ? i18n.t('proximoScreen.articles') : i18n.t('proximoScreen.article')}
  101. </Text></Badge>
  102. </Body>
  103. <Right>
  104. <CustomMaterialIcon icon="chevron-right"/>
  105. </Right>
  106. </ListItem>
  107. );
  108. } else {
  109. return <View/>;
  110. }
  111. }
  112. }