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.

ClubListScreen.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. // @flow
  20. import * as React from 'react';
  21. import {Platform} from 'react-native';
  22. import {Searchbar} from 'react-native-paper';
  23. import i18n from 'i18n-js';
  24. import {StackNavigationProp} from '@react-navigation/stack';
  25. import AuthenticatedScreen from '../../../components/Amicale/AuthenticatedScreen';
  26. import ClubListItem from '../../../components/Lists/Clubs/ClubListItem';
  27. import {isItemInCategoryFilter, stringMatchQuery} from '../../../utils/Search';
  28. import ClubListHeader from '../../../components/Lists/Clubs/ClubListHeader';
  29. import MaterialHeaderButtons, {
  30. Item,
  31. } from '../../../components/Overrides/CustomHeaderButton';
  32. import CollapsibleFlatList from '../../../components/Collapsible/CollapsibleFlatList';
  33. export type ClubCategoryType = {
  34. id: number,
  35. name: string,
  36. };
  37. export type ClubType = {
  38. id: number,
  39. name: string,
  40. description: string,
  41. logo: string,
  42. email: string | null,
  43. category: Array<number | null>,
  44. responsibles: Array<string>,
  45. };
  46. type PropsType = {
  47. navigation: StackNavigationProp,
  48. };
  49. type StateType = {
  50. currentlySelectedCategories: Array<number>,
  51. currentSearchString: string,
  52. };
  53. const LIST_ITEM_HEIGHT = 96;
  54. class ClubListScreen extends React.Component<PropsType, StateType> {
  55. categories: Array<ClubCategoryType>;
  56. constructor() {
  57. super();
  58. this.state = {
  59. currentlySelectedCategories: [],
  60. currentSearchString: '',
  61. };
  62. }
  63. /**
  64. * Creates the header content
  65. */
  66. componentDidMount() {
  67. const {props} = this;
  68. props.navigation.setOptions({
  69. headerTitle: this.getSearchBar,
  70. headerRight: this.getHeaderButtons,
  71. headerBackTitleVisible: false,
  72. headerTitleContainerStyle:
  73. Platform.OS === 'ios'
  74. ? {marginHorizontal: 0, width: '70%'}
  75. : {marginHorizontal: 0, right: 50, left: 50},
  76. });
  77. }
  78. /**
  79. * Callback used when clicking an article in the list.
  80. * It opens the modal to show detailed information about the article
  81. *
  82. * @param item The article pressed
  83. */
  84. onListItemPress(item: ClubType) {
  85. const {props} = this;
  86. props.navigation.navigate('club-information', {
  87. data: item,
  88. categories: this.categories,
  89. });
  90. }
  91. /**
  92. * Callback used when the search changes
  93. *
  94. * @param str The new search string
  95. */
  96. onSearchStringChange = (str: string) => {
  97. this.updateFilteredData(str, null);
  98. };
  99. /**
  100. * Gets the header search bar
  101. *
  102. * @return {*}
  103. */
  104. getSearchBar = (): React.Node => {
  105. return (
  106. <Searchbar
  107. placeholder={i18n.t('screens.proximo.search')}
  108. onChangeText={this.onSearchStringChange}
  109. />
  110. );
  111. };
  112. onChipSelect = (id: number) => {
  113. this.updateFilteredData(null, id);
  114. };
  115. /**
  116. * Gets the header button
  117. * @return {*}
  118. */
  119. getHeaderButtons = (): React.Node => {
  120. const onPress = () => {
  121. const {props} = this;
  122. props.navigation.navigate('club-about');
  123. };
  124. return (
  125. <MaterialHeaderButtons>
  126. <Item title="main" iconName="information" onPress={onPress} />
  127. </MaterialHeaderButtons>
  128. );
  129. };
  130. getScreen = (
  131. data: Array<{
  132. categories: Array<ClubCategoryType>,
  133. clubs: Array<ClubType>,
  134. } | null>,
  135. ): React.Node => {
  136. let categoryList = [];
  137. let clubList = [];
  138. if (data[0] != null) {
  139. categoryList = data[0].categories;
  140. clubList = data[0].clubs;
  141. }
  142. this.categories = categoryList;
  143. return (
  144. <CollapsibleFlatList
  145. data={clubList}
  146. keyExtractor={this.keyExtractor}
  147. renderItem={this.getRenderItem}
  148. ListHeaderComponent={this.getListHeader()}
  149. // Performance props, see https://reactnative.dev/docs/optimizing-flatlist-configuration
  150. removeClippedSubviews
  151. getItemLayout={this.itemLayout}
  152. />
  153. );
  154. };
  155. /**
  156. * Gets the list header, with controls to change the categories filter
  157. *
  158. * @returns {*}
  159. */
  160. getListHeader(): React.Node {
  161. const {state} = this;
  162. return (
  163. <ClubListHeader
  164. categories={this.categories}
  165. selectedCategories={state.currentlySelectedCategories}
  166. onChipSelect={this.onChipSelect}
  167. />
  168. );
  169. }
  170. /**
  171. * Gets the category object of the given ID
  172. *
  173. * @param id The ID of the category to find
  174. * @returns {*}
  175. */
  176. getCategoryOfId = (id: number): ClubCategoryType | null => {
  177. let cat = null;
  178. this.categories.forEach((item: ClubCategoryType) => {
  179. if (id === item.id) cat = item;
  180. });
  181. return cat;
  182. };
  183. getRenderItem = ({item}: {item: ClubType}): React.Node => {
  184. const onPress = () => {
  185. this.onListItemPress(item);
  186. };
  187. if (this.shouldRenderItem(item)) {
  188. return (
  189. <ClubListItem
  190. categoryTranslator={this.getCategoryOfId}
  191. item={item}
  192. onPress={onPress}
  193. height={LIST_ITEM_HEIGHT}
  194. />
  195. );
  196. }
  197. return null;
  198. };
  199. keyExtractor = (item: ClubType): string => item.id.toString();
  200. itemLayout = (
  201. data: {...},
  202. index: number,
  203. ): {length: number, offset: number, index: number} => ({
  204. length: LIST_ITEM_HEIGHT,
  205. offset: LIST_ITEM_HEIGHT * index,
  206. index,
  207. });
  208. /**
  209. * Updates the search string and category filter, saving them to the State.
  210. *
  211. * If the given category is already in the filter, it removes it.
  212. * Otherwise it adds it to the filter.
  213. *
  214. * @param filterStr The new filter string to use
  215. * @param categoryId The category to add/remove from the filter
  216. */
  217. updateFilteredData(filterStr: string | null, categoryId: number | null) {
  218. const {state} = this;
  219. const newCategoriesState = [...state.currentlySelectedCategories];
  220. let newStrState = state.currentSearchString;
  221. if (filterStr !== null) newStrState = filterStr;
  222. if (categoryId !== null) {
  223. const index = newCategoriesState.indexOf(categoryId);
  224. if (index === -1) newCategoriesState.push(categoryId);
  225. else newCategoriesState.splice(index, 1);
  226. }
  227. if (filterStr !== null || categoryId !== null)
  228. this.setState({
  229. currentSearchString: newStrState,
  230. currentlySelectedCategories: newCategoriesState,
  231. });
  232. }
  233. /**
  234. * Checks if the given item should be rendered according to current name and category filters
  235. *
  236. * @param item The club to check
  237. * @returns {boolean}
  238. */
  239. shouldRenderItem(item: ClubType): boolean {
  240. const {state} = this;
  241. let shouldRender =
  242. state.currentlySelectedCategories.length === 0 ||
  243. isItemInCategoryFilter(state.currentlySelectedCategories, item.category);
  244. if (shouldRender)
  245. shouldRender = stringMatchQuery(item.name, state.currentSearchString);
  246. return shouldRender;
  247. }
  248. render(): React.Node {
  249. const {props} = this;
  250. return (
  251. <AuthenticatedScreen
  252. navigation={props.navigation}
  253. requests={[
  254. {
  255. link: 'clubs/list',
  256. params: {},
  257. mandatory: true,
  258. },
  259. ]}
  260. renderFunction={this.getScreen}
  261. />
  262. );
  263. }
  264. }
  265. export default ClubListScreen;