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

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