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.

ClubListScreen.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // @flow
  2. import * as React from 'react';
  3. import {Animated, Platform} from "react-native";
  4. import {Chip, 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<string>,
  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('proximoScreen.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) => {
  89. return item.id.toString();
  90. };
  91. itemLayout = (data, index) => ({length: LIST_ITEM_HEIGHT, offset: LIST_ITEM_HEIGHT * index, index});
  92. getScreen = (data: Array<{categories: Array<category>, clubs: Array<club>} | null>) => {
  93. let categoryList = [];
  94. let clubList = [];
  95. if (data[0] != null) {
  96. categoryList = data[0].categories;
  97. clubList = data[0].clubs;
  98. }
  99. this.categories = categoryList;
  100. const {containerPaddingTop, scrollIndicatorInsetTop, onScroll} = this.props.collapsibleStack;
  101. return (
  102. <Animated.FlatList
  103. data={clubList}
  104. keyExtractor={this.keyExtractor}
  105. renderItem={this.getRenderItem}
  106. ListHeaderComponent={this.getListHeader()}
  107. // Performance props, see https://reactnative.dev/docs/optimizing-flatlist-configuration
  108. removeClippedSubviews={true}
  109. getItemLayout={this.itemLayout}
  110. // Animations
  111. onScroll={onScroll}
  112. contentContainerStyle={{paddingTop: containerPaddingTop}}
  113. scrollIndicatorInsets={{top: scrollIndicatorInsetTop}}
  114. />
  115. )
  116. };
  117. onChipSelect(id: string) {
  118. this.updateFilteredData(null, id);
  119. }
  120. updateFilteredData(filterStr: string | null, categoryId: string | null) {
  121. let newCategoriesState = [...this.state.currentlySelectedCategories];
  122. let newStrState = this.state.currentSearchString;
  123. if (filterStr !== null)
  124. newStrState = filterStr;
  125. if (categoryId !== null) {
  126. let index = newCategoriesState.indexOf(categoryId);
  127. if (index === -1)
  128. newCategoriesState.push(categoryId);
  129. else
  130. newCategoriesState.splice(index, 1);
  131. }
  132. if (filterStr !== null || categoryId !== null)
  133. this.setState({
  134. currentSearchString: newStrState,
  135. currentlySelectedCategories: newCategoriesState,
  136. })
  137. }
  138. getChipRender = (category: category, key: string) => {
  139. const onPress = this.onChipSelect.bind(this, category.id);
  140. return <Chip
  141. selected={isItemInCategoryFilter(this.state.currentlySelectedCategories, [category.id])}
  142. mode={'outlined'}
  143. onPress={onPress}
  144. style={{marginRight: 5, marginBottom: 5}}
  145. key={key}
  146. >
  147. {category.name}
  148. </Chip>;
  149. };
  150. getListHeader() {
  151. return <ClubListHeader
  152. categories={this.categories}
  153. categoryRender={this.getChipRender}
  154. />;
  155. }
  156. getCategoryOfId = (id: number) => {
  157. for (let i = 0; i < this.categories.length; i++) {
  158. if (id === this.categories[i].id)
  159. return this.categories[i];
  160. }
  161. };
  162. shouldRenderItem(item: club) {
  163. let shouldRender = this.state.currentlySelectedCategories.length === 0
  164. || isItemInCategoryFilter(this.state.currentlySelectedCategories, item.category);
  165. if (shouldRender)
  166. shouldRender = stringMatchQuery(item.name, this.state.currentSearchString);
  167. return shouldRender;
  168. }
  169. getRenderItem = ({item}: {item: club}) => {
  170. const onPress = this.onListItemPress.bind(this, item);
  171. if (this.shouldRenderItem(item)) {
  172. return (
  173. <ClubListItem
  174. categoryTranslator={this.getCategoryOfId}
  175. item={item}
  176. onPress={onPress}
  177. height={LIST_ITEM_HEIGHT}
  178. />
  179. );
  180. } else
  181. return null;
  182. };
  183. /**
  184. * Callback used when clicking an article in the list.
  185. * It opens the modal to show detailed information about the article
  186. *
  187. * @param item The article pressed
  188. */
  189. onListItemPress(item: club) {
  190. this.props.navigation.navigate("club-information", {data: item, categories: this.categories});
  191. }
  192. render() {
  193. return (
  194. <AuthenticatedScreen
  195. {...this.props}
  196. requests={[
  197. {
  198. link: 'clubs/list',
  199. params: {},
  200. mandatory: true,
  201. }
  202. ]}
  203. renderFunction={this.getScreen}
  204. />
  205. );
  206. }
  207. }
  208. export default withCollapsible(ClubListScreen);