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.tsx 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import * as React from 'react';
  20. import i18n from 'i18n-js';
  21. import { List, withTheme } from 'react-native-paper';
  22. import { StackNavigationProp } from '@react-navigation/stack';
  23. import WebSectionList from '../../../components/Screens/WebSectionList';
  24. import MaterialHeaderButtons, {
  25. Item,
  26. } from '../../../components/Overrides/CustomHeaderButton';
  27. import type { SectionListDataType } from '../../../components/Screens/WebSectionList';
  28. import { StyleSheet } from 'react-native';
  29. import Urls from '../../../constants/Urls';
  30. const LIST_ITEM_HEIGHT = 84;
  31. export type ProximoCategoryType = {
  32. name: string;
  33. icon: string;
  34. id: string;
  35. };
  36. export type ProximoArticleType = {
  37. name: string;
  38. description: string;
  39. quantity: string;
  40. price: string;
  41. code: string;
  42. id: string;
  43. type: Array<string>;
  44. image: string;
  45. };
  46. export type ProximoMainListItemType = {
  47. type: ProximoCategoryType;
  48. data: Array<ProximoArticleType>;
  49. };
  50. export type ProximoDataType = {
  51. types: Array<ProximoCategoryType>;
  52. articles: Array<ProximoArticleType>;
  53. };
  54. type PropsType = {
  55. navigation: StackNavigationProp<any>;
  56. theme: ReactNativePaper.Theme;
  57. };
  58. const styles = StyleSheet.create({
  59. item: {
  60. justifyContent: 'center',
  61. },
  62. });
  63. /**
  64. * Class defining the main proximo screen.
  65. * This screen shows the different categories of articles offered by proximo.
  66. */
  67. class ProximoMainScreen extends React.Component<PropsType> {
  68. /**
  69. * Function used to sort items in the list.
  70. * Makes the All category sticks to the top and sorts the others by name ascending
  71. *
  72. * @param a
  73. * @param b
  74. * @return {number}
  75. */
  76. static sortFinalData(
  77. a: ProximoMainListItemType,
  78. b: ProximoMainListItemType
  79. ): number {
  80. const str1 = a.type.name.toLowerCase();
  81. const str2 = b.type.name.toLowerCase();
  82. // Make 'All' category with id -1 stick to the top
  83. if (a.type.id === '-1') {
  84. return -1;
  85. }
  86. if (b.type.id === '-1') {
  87. return 1;
  88. }
  89. // Sort others by name ascending
  90. if (str1 < str2) {
  91. return -1;
  92. }
  93. if (str1 > str2) {
  94. return 1;
  95. }
  96. return 0;
  97. }
  98. /**
  99. * Get an array of available articles (in stock) of the given type
  100. *
  101. * @param articles The list of all articles
  102. * @param type The type of articles to find (undefined for any type)
  103. * @return {Array} The array of available articles
  104. */
  105. static getAvailableArticles(
  106. articles: Array<ProximoArticleType> | null,
  107. type?: ProximoCategoryType
  108. ): Array<ProximoArticleType> {
  109. const availableArticles: Array<ProximoArticleType> = [];
  110. if (articles != null) {
  111. articles.forEach((article: ProximoArticleType) => {
  112. if (
  113. ((type != null && article.type.includes(type.id)) || type == null) &&
  114. parseInt(article.quantity, 10) > 0
  115. ) {
  116. availableArticles.push(article);
  117. }
  118. });
  119. }
  120. return availableArticles;
  121. }
  122. articles: Array<ProximoArticleType> | null;
  123. constructor(props: PropsType) {
  124. super(props);
  125. this.articles = null;
  126. }
  127. /**
  128. * Creates header button
  129. */
  130. componentDidMount() {
  131. const { navigation } = this.props;
  132. navigation.setOptions({
  133. headerRight: () => this.getHeaderButtons(),
  134. });
  135. }
  136. /**
  137. * Callback used when the search button is pressed.
  138. * This will open a new ProximoListScreen with all items displayed
  139. */
  140. onPressSearchBtn = () => {
  141. const { navigation } = this.props;
  142. const searchScreenData = {
  143. shouldFocusSearchBar: true,
  144. data: {
  145. type: {
  146. id: '0',
  147. name: i18n.t('screens.proximo.all'),
  148. icon: 'star',
  149. },
  150. data:
  151. this.articles != null
  152. ? ProximoMainScreen.getAvailableArticles(this.articles)
  153. : [],
  154. },
  155. };
  156. navigation.navigate('proximo-list', searchScreenData);
  157. };
  158. /**
  159. * Callback used when the about button is pressed.
  160. * This will open the ProximoAboutScreen
  161. */
  162. onPressAboutBtn = () => {
  163. const { navigation } = this.props;
  164. navigation.navigate('proximo-about');
  165. };
  166. /**
  167. * Gets the header buttons
  168. * @return {*}
  169. */
  170. getHeaderButtons() {
  171. return (
  172. <MaterialHeaderButtons>
  173. <Item
  174. title="magnify"
  175. iconName="magnify"
  176. onPress={this.onPressSearchBtn}
  177. />
  178. <Item
  179. title="information"
  180. iconName="information"
  181. onPress={this.onPressAboutBtn}
  182. />
  183. </MaterialHeaderButtons>
  184. );
  185. }
  186. /**
  187. * Extracts a key for the given category
  188. *
  189. * @param item The category to extract the key from
  190. * @return {*} The extracted key
  191. */
  192. getKeyExtractor = (item: ProximoMainListItemType): string => item.type.id;
  193. /**
  194. * Gets the given category render item
  195. *
  196. * @param item The category to render
  197. * @return {*}
  198. */
  199. getRenderItem = ({ item }: { item: ProximoMainListItemType }) => {
  200. const { navigation, theme } = this.props;
  201. const dataToSend = {
  202. shouldFocusSearchBar: false,
  203. data: item,
  204. };
  205. const subtitle = `${item.data.length} ${
  206. item.data.length > 1
  207. ? i18n.t('screens.proximo.articles')
  208. : i18n.t('screens.proximo.article')
  209. }`;
  210. const onPress = () => {
  211. navigation.navigate('proximo-list', dataToSend);
  212. };
  213. if (item.data.length > 0) {
  214. return (
  215. <List.Item
  216. title={item.type.name}
  217. description={subtitle}
  218. onPress={onPress}
  219. left={(props) => (
  220. <List.Icon
  221. style={props.style}
  222. icon={item.type.icon}
  223. color={theme.colors.primary}
  224. />
  225. )}
  226. right={(props) => (
  227. <List.Icon
  228. color={props.color}
  229. style={props.style}
  230. icon="chevron-right"
  231. />
  232. )}
  233. style={{
  234. height: LIST_ITEM_HEIGHT,
  235. ...styles.item,
  236. }}
  237. />
  238. );
  239. }
  240. return null;
  241. };
  242. /**
  243. * Creates the dataset to be used in the FlatList
  244. *
  245. * @param fetchedData
  246. * @return {*}
  247. * */
  248. createDataset = (
  249. fetchedData: ProximoDataType | null
  250. ): SectionListDataType<ProximoMainListItemType> => {
  251. return [
  252. {
  253. title: '',
  254. data: this.generateData(fetchedData),
  255. keyExtractor: this.getKeyExtractor,
  256. },
  257. ];
  258. };
  259. /**
  260. * Generate the data using types and FetchedData.
  261. * This will group items under the same type.
  262. *
  263. * @param fetchedData The array of articles represented by objects
  264. * @returns {Array} The formatted dataset
  265. */
  266. generateData(
  267. fetchedData: ProximoDataType | null
  268. ): Array<ProximoMainListItemType> {
  269. const finalData: Array<ProximoMainListItemType> = [];
  270. this.articles = null;
  271. if (fetchedData != null) {
  272. const { types } = fetchedData;
  273. this.articles = fetchedData.articles;
  274. finalData.push({
  275. type: {
  276. id: '-1',
  277. name: i18n.t('screens.proximo.all'),
  278. icon: 'star',
  279. },
  280. data: ProximoMainScreen.getAvailableArticles(this.articles),
  281. });
  282. types.forEach((type: ProximoCategoryType) => {
  283. finalData.push({
  284. type,
  285. data: ProximoMainScreen.getAvailableArticles(this.articles, type),
  286. });
  287. });
  288. }
  289. finalData.sort(ProximoMainScreen.sortFinalData);
  290. return finalData;
  291. }
  292. render() {
  293. const { navigation } = this.props;
  294. return (
  295. <WebSectionList
  296. createDataset={this.createDataset}
  297. navigation={navigation}
  298. autoRefreshTime={0}
  299. refreshOnFocus={false}
  300. fetchUrl={Urls.proximo}
  301. renderItem={this.getRenderItem}
  302. />
  303. );
  304. }
  305. }
  306. export default withTheme(ProximoMainScreen);