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.1KB

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