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.

ProximoListScreen.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // @flow
  2. import * as React from 'react';
  3. import {Container, Text, Content, ListItem, Left, Thumbnail, Right, Body, Icon} from 'native-base';
  4. import CustomHeader from "../../components/CustomHeader";
  5. import {FlatList} from "react-native";
  6. import Touchable from 'react-native-platform-touchable';
  7. import Menu, {MenuItem} from 'react-native-material-menu';
  8. import i18n from "i18n-js";
  9. const IMG_URL = "https://etud.insa-toulouse.fr/~vergnet/appli-amicale/img/";
  10. const defaultImage = require('../../assets/image-missing.png');
  11. const sortMode = {
  12. price: "0",
  13. name: '1',
  14. };
  15. function sortPrice(a, b) {
  16. return a.price - b.price;
  17. }
  18. function sortPriceReverse(a, b) {
  19. return b.price - a.price;
  20. }
  21. function sortName(a, b) {
  22. if (a.name < b.name)
  23. return -1;
  24. if (a.name > b.name)
  25. return 1;
  26. return 0;
  27. }
  28. function sortNameReverse(a, b) {
  29. if (a.name < b.name)
  30. return 1;
  31. if (a.name > b.name)
  32. return -1;
  33. return 0;
  34. }
  35. type Props = {
  36. navigation: Object
  37. }
  38. type State = {
  39. navData: Array<Object>,
  40. currentSortMode: string,
  41. isSortReversed: boolean,
  42. sortPriceIcon: React.Node,
  43. sortNameIcon: React.Node,
  44. };
  45. export default class ProximoMainScreen extends React.Component<Props, State> {
  46. state = {
  47. navData: this.props.navigation.getParam('data', []).sort(sortPrice),
  48. currentSortMode: sortMode.price,
  49. isSortReversed: false,
  50. sortPriceIcon: '',
  51. sortNameIcon: '',
  52. };
  53. _menu: Menu;
  54. setMenuRef = (ref: Menu) => {
  55. this._menu = ref;
  56. };
  57. toggleSortMode(mode: string) {
  58. let isReverse = this.state.isSortReversed;
  59. if (mode === this.state.currentSortMode) // reverse mode
  60. isReverse = !isReverse; // this.state not updating on this function cycle
  61. else
  62. isReverse = false;
  63. this.setSortMode(mode, isReverse);
  64. }
  65. setSortMode(mode: string, isReverse: boolean) {
  66. this.setState({
  67. currentSortMode: mode,
  68. isSortReversed: isReverse
  69. });
  70. let data = this.state.navData;
  71. switch (mode) {
  72. case sortMode.price:
  73. if (isReverse) {
  74. data.sort(sortPriceReverse);
  75. } else {
  76. data.sort(sortPrice);
  77. }
  78. break;
  79. case sortMode.name:
  80. if (isReverse) {
  81. data.sort(sortNameReverse);
  82. } else {
  83. data.sort(sortName);
  84. }
  85. break;
  86. }
  87. this.setState({
  88. navData: data,
  89. });
  90. this.setupSortIcons(mode, isReverse);
  91. this._menu.hide();
  92. }
  93. componentDidMount() {
  94. this.setSortMode(this.state.currentSortMode, this.state.isSortReversed);
  95. }
  96. setupSortIcons(mode: string, isReverse: boolean) {
  97. const downSortIcon =
  98. <Icon
  99. active
  100. name={'sort-descending'}
  101. type={'MaterialCommunityIcons'}/>;
  102. const upSortIcon =
  103. <Icon
  104. active
  105. name={'sort-ascending'}
  106. type={'MaterialCommunityIcons'}/>;
  107. switch (mode) {
  108. case sortMode.price:
  109. this.setState({sortNameIcon: ''});
  110. if (isReverse) {
  111. this.setState({sortPriceIcon: upSortIcon});
  112. } else {
  113. this.setState({sortPriceIcon: downSortIcon});
  114. }
  115. break;
  116. case sortMode.name:
  117. this.setState({sortPriceIcon: ''});
  118. if (isReverse) {
  119. this.setState({sortNameIcon: upSortIcon});
  120. } else {
  121. this.setState({sortNameIcon: downSortIcon});
  122. }
  123. break;
  124. }
  125. }
  126. render() {
  127. const nav = this.props.navigation;
  128. const navType = nav.getParam('type', 'Empty');
  129. return (
  130. <Container>
  131. <CustomHeader backButton={true} navigation={nav} title={navType} rightMenu={
  132. <Right>
  133. <Menu
  134. ref={this.setMenuRef}
  135. button={
  136. <Touchable
  137. style={{padding: 6}}
  138. onPress={() =>
  139. this._menu.show()
  140. }>
  141. <Icon
  142. style={{color: "#fff"}}
  143. name="sort"
  144. type={'MaterialCommunityIcons'}/>
  145. </Touchable>
  146. }
  147. >
  148. <MenuItem
  149. onPress={() => this.toggleSortMode(sortMode.name)}>
  150. {this.state.sortNameIcon}
  151. {i18n.t('proximoScreen.sortName')}
  152. </MenuItem>
  153. <MenuItem
  154. onPress={() => this.toggleSortMode(sortMode.price)}>
  155. {this.state.sortPriceIcon}
  156. {i18n.t('proximoScreen.sortPrice')}
  157. </MenuItem>
  158. </Menu>
  159. </Right>
  160. }/>
  161. <Content>
  162. <FlatList
  163. data={this.state.navData}
  164. extraData={this.state.navData}
  165. keyExtractor={(item, index) => item.name}
  166. style={{minHeight: 300, width: '100%'}}
  167. renderItem={({item}) =>
  168. <ListItem
  169. thumbnail
  170. onPress={() => {
  171. console.log(IMG_URL + item.name + '.jpg')
  172. }}
  173. >
  174. <Left>
  175. <Thumbnail square source={{uri: IMG_URL + item.name + '.jpg'}}/>
  176. </Left>
  177. <Body>
  178. <Text style={{marginLeft: 20}}>
  179. {item.name}
  180. </Text>
  181. </Body>
  182. <Right style={{flex: 1}}>
  183. <Text>
  184. {item.price}€
  185. </Text>
  186. </Right>
  187. </ListItem>}
  188. />
  189. </Content>
  190. </Container>
  191. );
  192. }
  193. }