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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // @flow
  2. import * as React from 'react';
  3. import {FlatList, Image, Platform, ScrollView, View} from "react-native";
  4. import i18n from "i18n-js";
  5. import CustomModal from "../../components/Custom/CustomModal";
  6. import {IconButton, RadioButton, Searchbar, Subheading, Text, Title, withTheme} from "react-native-paper";
  7. import {stringMatchQuery} from "../../utils/Search";
  8. import ProximoListItem from "../../components/Lists/ProximoListItem";
  9. function sortPrice(a, b) {
  10. return a.price - b.price;
  11. }
  12. function sortPriceReverse(a, b) {
  13. return b.price - a.price;
  14. }
  15. function sortName(a, b) {
  16. if (a.name.toLowerCase() < b.name.toLowerCase())
  17. return -1;
  18. if (a.name.toLowerCase() > b.name.toLowerCase())
  19. return 1;
  20. return 0;
  21. }
  22. function sortNameReverse(a, b) {
  23. if (a.name.toLowerCase() < b.name.toLowerCase())
  24. return 1;
  25. if (a.name.toLowerCase() > b.name.toLowerCase())
  26. return -1;
  27. return 0;
  28. }
  29. const LIST_ITEM_HEIGHT = 84;
  30. type Props = {
  31. navigation: Object,
  32. route: Object,
  33. }
  34. type State = {
  35. currentSortMode: number,
  36. modalCurrentDisplayItem: React.Node,
  37. currentSearchString: string,
  38. };
  39. /**
  40. * Class defining proximo's article list of a certain category.
  41. */
  42. class ProximoListScreen extends React.Component<Props, State> {
  43. modalRef: Object;
  44. listData: Array<Object>;
  45. shouldFocusSearchBar: boolean;
  46. colors: Object;
  47. constructor(props) {
  48. super(props);
  49. this.listData = this.props.route.params['data']['data'];
  50. this.shouldFocusSearchBar = this.props.route.params['shouldFocusSearchBar'];
  51. this.state = {
  52. currentSearchString: '',
  53. currentSortMode: 3,
  54. modalCurrentDisplayItem: null,
  55. };
  56. this.colors = props.theme.colors;
  57. }
  58. /**
  59. * Creates the header content
  60. */
  61. componentDidMount() {
  62. this.props.navigation.setOptions({
  63. headerRight: this.getSortMenuButton,
  64. headerTitle: this.getSearchBar,
  65. headerBackTitleVisible: false,
  66. headerTitleContainerStyle: Platform.OS === 'ios' ?
  67. {marginHorizontal: 0, width: '70%'} :
  68. {marginHorizontal: 0, right: 50, left: 50},
  69. });
  70. }
  71. /**
  72. * Gets the header search bar
  73. *
  74. * @return {*}
  75. */
  76. getSearchBar = () => {
  77. return (
  78. <Searchbar
  79. placeholder={i18n.t('proximoScreen.search')}
  80. onChangeText={this.onSearchStringChange}
  81. />
  82. );
  83. };
  84. /**
  85. * Gets the sort menu header button
  86. *
  87. * @return {*}
  88. */
  89. getSortMenuButton = () => {
  90. return (
  91. <IconButton
  92. icon="sort"
  93. color={this.colors.text}
  94. size={26}
  95. onPress={this.onSortMenuPress}
  96. />
  97. );
  98. };
  99. /**
  100. * Callback used when clicking on the sort menu button.
  101. * It will open the modal to show a sort selection
  102. */
  103. onSortMenuPress = () => {
  104. this.setState({
  105. modalCurrentDisplayItem: this.getModalSortMenu()
  106. });
  107. if (this.modalRef) {
  108. this.modalRef.open();
  109. }
  110. };
  111. /**
  112. * Sets the current sort mode.
  113. *
  114. * @param mode The number representing the mode
  115. */
  116. setSortMode(mode: number) {
  117. this.setState({
  118. currentSortMode: mode,
  119. });
  120. switch (mode) {
  121. case 1:
  122. this.listData.sort(sortPrice);
  123. break;
  124. case 2:
  125. this.listData.sort(sortPriceReverse);
  126. break;
  127. case 3:
  128. this.listData.sort(sortName);
  129. break;
  130. case 4:
  131. this.listData.sort(sortNameReverse);
  132. break;
  133. }
  134. if (this.modalRef && mode !== this.state.currentSortMode) {
  135. this.modalRef.close();
  136. }
  137. }
  138. /**
  139. * Gets a color depending on the quantity available
  140. *
  141. * @param availableStock The quantity available
  142. * @return
  143. */
  144. getStockColor(availableStock: number) {
  145. let color: string;
  146. if (availableStock > 3)
  147. color = this.colors.success;
  148. else if (availableStock > 0)
  149. color = this.colors.warning;
  150. else
  151. color = this.colors.danger;
  152. return color;
  153. }
  154. /**
  155. * Callback used when the search changes
  156. *
  157. * @param str The new search string
  158. */
  159. onSearchStringChange = (str: string) => {
  160. this.setState({currentSearchString: str})
  161. };
  162. /**
  163. * Gets the modal content depending on the given article
  164. *
  165. * @param item The article to display
  166. * @return {*}
  167. */
  168. getModalItemContent(item: Object) {
  169. return (
  170. <View style={{
  171. flex: 1,
  172. padding: 20
  173. }}>
  174. <Title>{item.name}</Title>
  175. <View style={{
  176. flexDirection: 'row',
  177. width: '100%',
  178. marginTop: 10,
  179. }}>
  180. <Subheading style={{
  181. color: this.getStockColor(parseInt(item.quantity)),
  182. }}>
  183. {item.quantity + ' ' + i18n.t('proximoScreen.inStock')}
  184. </Subheading>
  185. <Subheading style={{marginLeft: 'auto'}}>{item.price}€</Subheading>
  186. </View>
  187. <ScrollView>
  188. <View style={{width: '100%', height: 150, marginTop: 20, marginBottom: 20}}>
  189. <Image style={{flex: 1, resizeMode: "contain"}}
  190. source={{uri: item.image}}/>
  191. </View>
  192. <Text>{item.description}</Text>
  193. </ScrollView>
  194. </View>
  195. );
  196. }
  197. /**
  198. * Gets the modal content to display a sort menu
  199. *
  200. * @return {*}
  201. */
  202. getModalSortMenu() {
  203. return (
  204. <View style={{
  205. flex: 1,
  206. padding: 20
  207. }}>
  208. <Title style={{marginBottom: 10}}>{i18n.t('proximoScreen.sortOrder')}</Title>
  209. <RadioButton.Group
  210. onValueChange={value => this.setSortMode(value)}
  211. value={this.state.currentSortMode}
  212. >
  213. <View style={{
  214. flexDirection: 'row',
  215. justifyContent: 'flex-start',
  216. alignItems: 'center'
  217. }}>
  218. <RadioButton value={1}/>
  219. <Text>{i18n.t('proximoScreen.sortPrice')}</Text>
  220. </View>
  221. <View style={{
  222. flexDirection: 'row',
  223. justifyContent: 'flex-start',
  224. alignItems: 'center'
  225. }}>
  226. <RadioButton value={2}/>
  227. <Text>{i18n.t('proximoScreen.sortPriceReverse')}</Text>
  228. </View>
  229. <View style={{
  230. flexDirection: 'row',
  231. justifyContent: 'flex-start',
  232. alignItems: 'center'
  233. }}>
  234. <RadioButton value={3}/>
  235. <Text>{i18n.t('proximoScreen.sortName')}</Text>
  236. </View>
  237. <View style={{
  238. flexDirection: 'row',
  239. justifyContent: 'flex-start',
  240. alignItems: 'center'
  241. }}>
  242. <RadioButton value={4}/>
  243. <Text>{i18n.t('proximoScreen.sortNameReverse')}</Text>
  244. </View>
  245. </RadioButton.Group>
  246. </View>
  247. );
  248. }
  249. /**
  250. * Callback used when clicking an article in the list.
  251. * It opens the modal to show detailed information about the article
  252. *
  253. * @param item The article pressed
  254. */
  255. onListItemPress(item: Object) {
  256. this.setState({
  257. modalCurrentDisplayItem: this.getModalItemContent(item)
  258. });
  259. if (this.modalRef) {
  260. this.modalRef.open();
  261. }
  262. }
  263. /**
  264. * Gets a render item for the given article
  265. *
  266. * @param item The article to render
  267. * @return {*}
  268. */
  269. renderItem = ({item}: Object) => {
  270. if (stringMatchQuery(item.name, this.state.currentSearchString)) {
  271. const onPress = this.onListItemPress.bind(this, item);
  272. const color = this.getStockColor(parseInt(item.quantity));
  273. return (
  274. <ProximoListItem
  275. item={item}
  276. onPress={onPress}
  277. color={color}
  278. height={LIST_ITEM_HEIGHT}
  279. />
  280. );
  281. } else
  282. return null;
  283. };
  284. /**
  285. * Extracts a key for the given article
  286. *
  287. * @param item The article to extract the key from
  288. * @return {*} The extracted key
  289. */
  290. keyExtractor(item: Object) {
  291. return item.name + item.code;
  292. }
  293. /**
  294. * Callback used when receiving the modal ref
  295. *
  296. * @param ref
  297. */
  298. onModalRef = (ref: Object) => {
  299. this.modalRef = ref;
  300. };
  301. itemLayout = (data, index) => ({length: LIST_ITEM_HEIGHT, offset: LIST_ITEM_HEIGHT * index, index});
  302. render() {
  303. return (
  304. <View style={{
  305. height: '100%'
  306. }}>
  307. <CustomModal onRef={this.onModalRef}>
  308. {this.state.modalCurrentDisplayItem}
  309. </CustomModal>
  310. {/*$FlowFixMe*/}
  311. <FlatList
  312. data={this.listData}
  313. extraData={this.state.currentSearchString + this.state.currentSortMode}
  314. keyExtractor={this.keyExtractor}
  315. renderItem={this.renderItem}
  316. // Performance props, see https://reactnative.dev/docs/optimizing-flatlist-configuration
  317. removeClippedSubviews={true}
  318. getItemLayout={this.itemLayout}
  319. initialNumToRender={10}
  320. />
  321. </View>
  322. );
  323. }
  324. }
  325. export default withTheme(ProximoListScreen);