Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
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.6KB

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