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.

ProximoSearchScreen.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // @flow
  2. import * as React from 'react';
  3. import {Body, Container, Content, Left, ListItem, Right, Text, Thumbnail,} from 'native-base';
  4. import {FlatList} from "react-native";
  5. import i18n from "i18n-js";
  6. import ThemeManager from "../../utils/ThemeManager";
  7. import SearchHeader from "../../components/SearchHeader";
  8. type Props = {
  9. navigation: Object,
  10. };
  11. type State = {
  12. filteredData: Array<Object>,
  13. };
  14. /**
  15. * Class defining proximo's article list of a certain category.
  16. */
  17. export default class ProximoSearchScreen extends React.Component<Props, State> {
  18. state = {
  19. filteredData: this.props.navigation.getParam('data', {articles: [{name: "Error"}]}).articles,
  20. };
  21. /**
  22. * get color depending on quantity available
  23. *
  24. * @param availableStock
  25. * @return
  26. */
  27. getStockColor(availableStock: number) {
  28. let color: string;
  29. if (availableStock > 3)
  30. color = ThemeManager.getCurrentThemeVariables().brandSuccess;
  31. else if (availableStock > 0)
  32. color = ThemeManager.getCurrentThemeVariables().brandWarning;
  33. else
  34. color = ThemeManager.getCurrentThemeVariables().brandDanger;
  35. return color;
  36. }
  37. showItemDetails(item: Object) {
  38. //TODO: implement onClick function (copy-paste from ProximoListScreen)
  39. }
  40. /**
  41. * Returns only the articles whose name contains str. Case and accents insensitive.
  42. * @param str
  43. * @returns {[]}
  44. */
  45. filterData(str: string) {
  46. let filteredData = [];
  47. const testStr: String = str.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
  48. const articles: Object = this.props.navigation.getParam('data', {articles: [{name: "Error"}]}).articles;
  49. for (const article: Object of articles) {
  50. const name: String = String(article.name.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, ""));
  51. if (name.includes(testStr)) {
  52. filteredData.push(article)
  53. }
  54. }
  55. return filteredData;
  56. }
  57. search(str: string) {
  58. this.setState({
  59. filteredData: this.filterData(str)
  60. })
  61. }
  62. render() {
  63. return (
  64. <Container>
  65. <SearchHeader searchFunction={this.search.bind(this)} navigation={this.props.navigation}/>
  66. <Content>
  67. <FlatList
  68. data={this.state.filteredData}
  69. keyExtractor={(item) => item.name + item.code}
  70. style={{minHeight: 300, width: '100%'}}
  71. renderItem={({item}) =>
  72. <ListItem
  73. thumbnail
  74. onPress={() => {this.showItemDetails(item);}} >
  75. <Left>
  76. <Thumbnail square source={{uri: item.image}}/>
  77. </Left>
  78. <Body>
  79. <Text style={{marginLeft: 20}}>
  80. {item.name}
  81. </Text>
  82. <Text note style={{
  83. marginLeft: 20,
  84. color: this.getStockColor(parseInt(item.quantity))
  85. }}>
  86. {item.quantity + ' ' + i18n.t('proximoScreen.inStock')}
  87. </Text>
  88. </Body>
  89. <Right>
  90. <Text style={{fontWeight: "bold"}}>
  91. {item.price}€
  92. </Text>
  93. </Right>
  94. </ListItem>}
  95. />
  96. </Content>
  97. </Container>
  98. );
  99. }
  100. }