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

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