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

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