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 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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, useTheme, withTheme } from 'react-native-paper';
  22. import WebSectionList from '../../../components/Screens/WebSectionList';
  23. import MaterialHeaderButtons, {
  24. Item,
  25. } from '../../../components/Overrides/CustomHeaderButton';
  26. import type { SectionListDataType } from '../../../components/Screens/WebSectionList';
  27. import { Image, StyleSheet } from 'react-native';
  28. import Urls from '../../../constants/Urls';
  29. import { readData } from '../../../utils/WebData';
  30. import { useNavigation } from '@react-navigation/core';
  31. import { useLayoutEffect } from 'react';
  32. import { useCachedProximoCategories } from '../../../utils/cacheContext';
  33. import GENERAL_STYLES from '../../../constants/Styles';
  34. const LIST_ITEM_HEIGHT = 84;
  35. export type ProximoCategoryType = {
  36. id: number;
  37. name: string;
  38. icon: string;
  39. created_at: string;
  40. updated_at: string;
  41. nb_articles: number;
  42. };
  43. export type ProximoArticleType = {
  44. id: number;
  45. name: string;
  46. description: string;
  47. quantity: number;
  48. price: number;
  49. code: string;
  50. image: string;
  51. category_id: number;
  52. created_at: string;
  53. updated_at: string;
  54. category: ProximoCategoryType;
  55. };
  56. export type CategoriesType = Array<ProximoCategoryType>;
  57. const styles = StyleSheet.create({
  58. item: {
  59. justifyContent: 'center',
  60. },
  61. avatar: {
  62. marginLeft: 5,
  63. ...GENERAL_STYLES.centerVertical,
  64. height: 35,
  65. width: 35,
  66. },
  67. });
  68. function sortCategories(
  69. a: ProximoCategoryType,
  70. b: ProximoCategoryType
  71. ): number {
  72. const str1 = a.name.toLowerCase();
  73. const str2 = b.name.toLowerCase();
  74. // Make 'All' category with id -1 stick to the top
  75. if (a.id === -1) {
  76. return -1;
  77. }
  78. if (b.id === -1) {
  79. return 1;
  80. }
  81. // Sort others by name ascending
  82. if (str1 < str2) {
  83. return -1;
  84. }
  85. if (str1 > str2) {
  86. return 1;
  87. }
  88. return 0;
  89. }
  90. /**
  91. * Class defining the main proximo screen.
  92. * This screen shows the different categories of articles offered by proximo.
  93. */
  94. function ProximoMainScreen() {
  95. const navigation = useNavigation();
  96. const theme = useTheme();
  97. const { categories, setCategories } = useCachedProximoCategories();
  98. useLayoutEffect(() => {
  99. navigation.setOptions({
  100. headerRight: () => getHeaderButtons(),
  101. });
  102. // eslint-disable-next-line react-hooks/exhaustive-deps
  103. }, [navigation]);
  104. /**
  105. * Callback used when the search button is pressed.
  106. * This will open a new ProximoListScreen with all items displayed
  107. */
  108. const onPressSearchBtn = () => {
  109. const searchScreenData = {
  110. shouldFocusSearchBar: true,
  111. category: -1,
  112. };
  113. navigation.navigate('proximo-list', searchScreenData);
  114. };
  115. const onPressAboutBtn = () => navigation.navigate('proximo-about');
  116. const getHeaderButtons = () => {
  117. return (
  118. <MaterialHeaderButtons>
  119. <Item
  120. title={'magnify'}
  121. iconName={'magnify'}
  122. onPress={onPressSearchBtn}
  123. />
  124. <Item
  125. title={'information'}
  126. iconName={'information'}
  127. onPress={onPressAboutBtn}
  128. />
  129. </MaterialHeaderButtons>
  130. );
  131. };
  132. /**
  133. * Extracts a key for the given category
  134. *
  135. * @param item The category to extract the key from
  136. * @return {*} The extracted key
  137. */
  138. const getKeyExtractor = (item: ProximoCategoryType): string =>
  139. item.id.toString();
  140. /**
  141. * Gets the given category render item
  142. *
  143. * @param item The category to render
  144. * @return {*}
  145. */
  146. const getRenderItem = ({ item }: { item: ProximoCategoryType }) => {
  147. const dataToSend = {
  148. shouldFocusSearchBar: false,
  149. category: item.id,
  150. };
  151. const article_number = item.nb_articles;
  152. const subtitle = `${article_number} ${
  153. article_number > 1
  154. ? i18n.t('screens.proximo.articles')
  155. : i18n.t('screens.proximo.article')
  156. }`;
  157. const onPress = () => navigation.navigate('proximo-list', dataToSend);
  158. if (article_number > 0) {
  159. return (
  160. <List.Item
  161. title={item.name}
  162. description={subtitle}
  163. onPress={onPress}
  164. left={(props) =>
  165. item.icon.endsWith('.png') ? (
  166. <Image
  167. style={{ ...props.style, ...styles.avatar }}
  168. source={{ uri: Urls.proximo.icons + item.icon }}
  169. />
  170. ) : (
  171. <List.Icon
  172. style={props.style}
  173. icon={item.icon}
  174. color={theme.colors.primary}
  175. />
  176. )
  177. }
  178. right={(props) => (
  179. <List.Icon
  180. color={props.color}
  181. style={props.style}
  182. icon={'chevron-right'}
  183. />
  184. )}
  185. style={{
  186. height: LIST_ITEM_HEIGHT,
  187. ...styles.item,
  188. }}
  189. />
  190. );
  191. }
  192. return null;
  193. };
  194. /**
  195. * Creates the dataset to be used in the FlatList
  196. *
  197. * @param fetchedData
  198. * @return {*}
  199. * */
  200. const createDataset = (
  201. data: CategoriesType | undefined
  202. ): SectionListDataType<ProximoCategoryType> => {
  203. if (data) {
  204. let totalArticles = 0;
  205. data.forEach((c) => (totalArticles += c.nb_articles));
  206. const finalData: CategoriesType = [
  207. {
  208. id: -1,
  209. name: i18n.t('screens.proximo.all'),
  210. icon: 'star',
  211. created_at: '',
  212. updated_at: '',
  213. nb_articles: totalArticles,
  214. },
  215. ...data,
  216. ];
  217. return [
  218. {
  219. title: '',
  220. data: finalData.filter((c) => c.nb_articles > 0).sort(sortCategories),
  221. keyExtractor: getKeyExtractor,
  222. },
  223. ];
  224. } else {
  225. return [];
  226. }
  227. };
  228. return (
  229. <WebSectionList
  230. request={() => readData<CategoriesType>(Urls.proximo.categories)}
  231. createDataset={createDataset}
  232. refreshOnFocus={true}
  233. renderItem={getRenderItem}
  234. cache={categories}
  235. onCacheUpdate={setCategories}
  236. />
  237. );
  238. }
  239. export default withTheme(ProximoMainScreen);