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

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