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.

ProximoMainScreen.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // @flow
  2. import * as React from 'react';
  3. import {View} from 'react-native'
  4. import i18n from "i18n-js";
  5. import WebSectionList from "../../components/WebSectionList";
  6. import {List, withTheme} from 'react-native-paper';
  7. import HeaderButton from "../../components/HeaderButton";
  8. const DATA_URL = "https://etud.insa-toulouse.fr/~proximo/data/stock-v2.json";
  9. type Props = {
  10. navigation: Object,
  11. }
  12. type State = {
  13. fetchedData: Object,
  14. }
  15. /**
  16. * Class defining the main proximo screen.
  17. * This screen shows the different categories of articles offered by proximo.
  18. */
  19. class ProximoMainScreen extends React.Component<Props, State> {
  20. articles: Object;
  21. onPressSearchBtn: Function;
  22. onPressAboutBtn: Function;
  23. getRenderItem: Function;
  24. createDataset: Function;
  25. colors: Object;
  26. constructor(props) {
  27. super(props);
  28. this.onPressSearchBtn = this.onPressSearchBtn.bind(this);
  29. this.onPressAboutBtn = this.onPressAboutBtn.bind(this);
  30. this.getRenderItem = this.getRenderItem.bind(this);
  31. this.createDataset = this.createDataset.bind(this);
  32. this.colors = props.theme.colors;
  33. }
  34. /**
  35. * Function used to sort items in the list.
  36. * Makes the All category stick to the top and sorts the others by name ascending
  37. *
  38. * @param a
  39. * @param b
  40. * @return {number}
  41. */
  42. static sortFinalData(a: Object, b: Object) {
  43. let str1 = a.type.name.toLowerCase();
  44. let str2 = b.type.name.toLowerCase();
  45. // Make 'All' category with id -1 stick to the top
  46. if (a.type.id === -1)
  47. return -1;
  48. if (b.type.id === -1)
  49. return 1;
  50. // Sort others by name ascending
  51. if (str1 < str2)
  52. return -1;
  53. if (str1 > str2)
  54. return 1;
  55. return 0;
  56. }
  57. /**
  58. * Creates header button
  59. */
  60. componentDidMount() {
  61. const rightButton = this.getHeaderButtons.bind(this);
  62. this.props.navigation.setOptions({
  63. headerRight: rightButton,
  64. });
  65. }
  66. /**
  67. * Callback used when the search button is pressed.
  68. * This will open a new ProximoListScreen with all items displayed
  69. */
  70. onPressSearchBtn() {
  71. let searchScreenData = {
  72. shouldFocusSearchBar: true,
  73. data: {
  74. type: {
  75. id: "0",
  76. name: i18n.t('proximoScreen.all'),
  77. icon: 'star'
  78. },
  79. data: this.articles !== undefined ?
  80. this.getAvailableArticles(this.articles, undefined) : []
  81. },
  82. };
  83. this.props.navigation.navigate('ProximoListScreen', searchScreenData);
  84. }
  85. /**
  86. * Callback used when the about button is pressed.
  87. * This will open the ProximoAboutScreen
  88. */
  89. onPressAboutBtn() {
  90. this.props.navigation.navigate('ProximoAboutScreen');
  91. }
  92. /**
  93. * Gets the header buttons
  94. * @return {*}
  95. */
  96. getHeaderButtons() {
  97. return (
  98. <View
  99. style={{
  100. flexDirection: 'row',
  101. }}>
  102. <HeaderButton icon={'magnify'} onPress={this.onPressSearchBtn}/>
  103. <HeaderButton icon={'information'} onPress={this.onPressAboutBtn}/>
  104. </View>
  105. );
  106. }
  107. /**
  108. * Extracts a key for the given category
  109. *
  110. * @param item The category to extract the key from
  111. * @return {*} The extracted key
  112. */
  113. getKeyExtractor(item: Object) {
  114. return item !== undefined ? item.type['id'] : undefined;
  115. }
  116. /**
  117. * Creates the dataset to be used in the FlatList
  118. *
  119. * @param fetchedData
  120. * @return {*}
  121. * */
  122. createDataset(fetchedData: Object) {
  123. return [
  124. {
  125. title: '',
  126. data: this.generateData(fetchedData),
  127. extraData: this.state,
  128. keyExtractor: this.getKeyExtractor
  129. }
  130. ];
  131. }
  132. /**
  133. * Generate the data using types and FetchedData.
  134. * This will group items under the same type.
  135. *
  136. * @param fetchedData The array of articles represented by objects
  137. * @returns {Array} The formatted dataset
  138. */
  139. generateData(fetchedData: Object) {
  140. let finalData = [];
  141. this.articles = undefined;
  142. if (fetchedData.types !== undefined && fetchedData.articles !== undefined) {
  143. let types = fetchedData.types;
  144. this.articles = fetchedData.articles;
  145. finalData.push({
  146. type: {
  147. id: -1,
  148. name: i18n.t('proximoScreen.all'),
  149. icon: 'star'
  150. },
  151. data: this.getAvailableArticles(this.articles, undefined)
  152. });
  153. for (let i = 0; i < types.length; i++) {
  154. finalData.push({
  155. type: types[i],
  156. data: this.getAvailableArticles(this.articles, types[i])
  157. });
  158. }
  159. }
  160. finalData.sort(ProximoMainScreen.sortFinalData);
  161. return finalData;
  162. }
  163. /**
  164. * Get an array of available articles (in stock) of the given type
  165. *
  166. * @param articles The list of all articles
  167. * @param type The type of articles to find (undefined for any type)
  168. * @return {Array} The array of available articles
  169. */
  170. getAvailableArticles(articles: Array<Object>, type: ?Object) {
  171. let availableArticles = [];
  172. for (let k = 0; k < articles.length; k++) {
  173. if ((type !== undefined && type !== null && articles[k]['type'].includes(type['id'])
  174. || type === undefined)
  175. && parseInt(articles[k]['quantity']) > 0) {
  176. availableArticles.push(articles[k]);
  177. }
  178. }
  179. return availableArticles;
  180. }
  181. /**
  182. * Gets the given category render item
  183. *
  184. * @param item The category to render
  185. * @return {*}
  186. */
  187. getRenderItem({item}: Object) {
  188. let dataToSend = {
  189. shouldFocusSearchBar: false,
  190. data: item,
  191. };
  192. const subtitle = item.data.length + " " + (item.data.length > 1 ? i18n.t('proximoScreen.articles') : i18n.t('proximoScreen.article'));
  193. const onPress = this.props.navigation.navigate.bind(this, 'ProximoListScreen', dataToSend);
  194. if (item.data.length > 0) {
  195. return (
  196. <List.Item
  197. title={item.type.name}
  198. description={subtitle}
  199. onPress={onPress}
  200. left={props => <List.Icon
  201. {...props}
  202. icon={item.type.icon}
  203. color={this.colors.primary}/>}
  204. right={props => <List.Icon {...props} icon={'chevron-right'}/>}
  205. />
  206. );
  207. } else
  208. return <View/>;
  209. }
  210. render() {
  211. const nav = this.props.navigation;
  212. return (
  213. <WebSectionList
  214. createDataset={this.createDataset}
  215. navigation={nav}
  216. autoRefreshTime={0}
  217. refreshOnFocus={false}
  218. fetchUrl={DATA_URL}
  219. renderItem={this.getRenderItem}/>
  220. );
  221. }
  222. }
  223. export default withTheme(ProximoMainScreen);