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.

ProximoMainScreen.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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/Lists/WebSectionList";
  6. import {List, withTheme} from 'react-native-paper';
  7. import HeaderButton from "../../components/Custom/HeaderButton";
  8. const DATA_URL = "https://etud.insa-toulouse.fr/~proximo/data/stock-v2.json";
  9. const LIST_ITEM_HEIGHT = 84;
  10. type Props = {
  11. navigation: Object,
  12. }
  13. type State = {
  14. fetchedData: Object,
  15. }
  16. /**
  17. * Class defining the main proximo screen.
  18. * This screen shows the different categories of articles offered by proximo.
  19. */
  20. class ProximoMainScreen extends React.Component<Props, State> {
  21. articles: Object;
  22. onPressSearchBtn: Function;
  23. onPressAboutBtn: Function;
  24. getRenderItem: Function;
  25. createDataset: Function;
  26. colors: Object;
  27. constructor(props) {
  28. super(props);
  29. this.onPressSearchBtn = this.onPressSearchBtn.bind(this);
  30. this.onPressAboutBtn = this.onPressAboutBtn.bind(this);
  31. this.getRenderItem = this.getRenderItem.bind(this);
  32. this.createDataset = this.createDataset.bind(this);
  33. this.colors = props.theme.colors;
  34. }
  35. /**
  36. * Function used to sort items in the list.
  37. * Makes the All category stick to the top and sorts the others by name ascending
  38. *
  39. * @param a
  40. * @param b
  41. * @return {number}
  42. */
  43. static sortFinalData(a: Object, b: Object) {
  44. let str1 = a.type.name.toLowerCase();
  45. let str2 = b.type.name.toLowerCase();
  46. // Make 'All' category with id -1 stick to the top
  47. if (a.type.id === -1)
  48. return -1;
  49. if (b.type.id === -1)
  50. return 1;
  51. // Sort others by name ascending
  52. if (str1 < str2)
  53. return -1;
  54. if (str1 > str2)
  55. return 1;
  56. return 0;
  57. }
  58. /**
  59. * Creates header button
  60. */
  61. componentDidMount() {
  62. const rightButton = this.getHeaderButtons.bind(this);
  63. this.props.navigation.setOptions({
  64. headerRight: rightButton,
  65. });
  66. }
  67. /**
  68. * Callback used when the search button is pressed.
  69. * This will open a new ProximoListScreen with all items displayed
  70. */
  71. onPressSearchBtn() {
  72. let searchScreenData = {
  73. shouldFocusSearchBar: true,
  74. data: {
  75. type: {
  76. id: "0",
  77. name: i18n.t('proximoScreen.all'),
  78. icon: 'star'
  79. },
  80. data: this.articles !== undefined ?
  81. this.getAvailableArticles(this.articles, undefined) : []
  82. },
  83. };
  84. this.props.navigation.navigate('ProximoListScreen', searchScreenData);
  85. }
  86. /**
  87. * Callback used when the about button is pressed.
  88. * This will open the ProximoAboutScreen
  89. */
  90. onPressAboutBtn() {
  91. this.props.navigation.navigate('ProximoAboutScreen');
  92. }
  93. /**
  94. * Gets the header buttons
  95. * @return {*}
  96. */
  97. getHeaderButtons() {
  98. return (
  99. <View
  100. style={{
  101. flexDirection: 'row',
  102. }}>
  103. <HeaderButton icon={'magnify'} onPress={this.onPressSearchBtn}/>
  104. <HeaderButton icon={'information'} onPress={this.onPressAboutBtn}/>
  105. </View>
  106. );
  107. }
  108. /**
  109. * Extracts a key for the given category
  110. *
  111. * @param item The category to extract the key from
  112. * @return {*} The extracted key
  113. */
  114. getKeyExtractor(item: Object) {
  115. return item !== undefined ? item.type['id'] : undefined;
  116. }
  117. /**
  118. * Creates the dataset to be used in the FlatList
  119. *
  120. * @param fetchedData
  121. * @return {*}
  122. * */
  123. createDataset(fetchedData: Object) {
  124. return [
  125. {
  126. title: '',
  127. data: this.generateData(fetchedData),
  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. style={{
  206. height: LIST_ITEM_HEIGHT,
  207. justifyContent: 'center',
  208. }}
  209. />
  210. );
  211. } else
  212. return <View/>;
  213. }
  214. render() {
  215. const nav = this.props.navigation;
  216. return (
  217. <WebSectionList
  218. createDataset={this.createDataset}
  219. navigation={nav}
  220. autoRefreshTime={0}
  221. refreshOnFocus={false}
  222. fetchUrl={DATA_URL}
  223. renderItem={this.getRenderItem}/>
  224. );
  225. }
  226. }
  227. export default withTheme(ProximoMainScreen);