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.

GroupSelectionScreen.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // @flow
  2. import * as React from 'react';
  3. import {Platform} from 'react-native';
  4. import i18n from 'i18n-js';
  5. import {Searchbar} from 'react-native-paper';
  6. import {StackNavigationProp} from '@react-navigation/stack';
  7. import {stringMatchQuery} from '../../utils/Search';
  8. import WebSectionList from '../../components/Screens/WebSectionList';
  9. import GroupListAccordion from '../../components/Lists/PlanexGroups/GroupListAccordion';
  10. import AsyncStorageManager from '../../managers/AsyncStorageManager';
  11. const LIST_ITEM_HEIGHT = 70;
  12. export type PlanexGroupType = {
  13. name: string,
  14. id: number,
  15. };
  16. export type PlanexGroupCategoryType = {
  17. name: string,
  18. id: number,
  19. content: Array<PlanexGroupType>,
  20. };
  21. type PropsType = {
  22. navigation: StackNavigationProp,
  23. };
  24. type StateType = {
  25. currentSearchString: string,
  26. favoriteGroups: Array<PlanexGroupType>,
  27. };
  28. function sortName(
  29. a: PlanexGroupType | PlanexGroupCategoryType,
  30. b: PlanexGroupType | PlanexGroupCategoryType,
  31. ): number {
  32. if (a.name.toLowerCase() < b.name.toLowerCase()) return -1;
  33. if (a.name.toLowerCase() > b.name.toLowerCase()) return 1;
  34. return 0;
  35. }
  36. const GROUPS_URL = 'http://planex.insa-toulouse.fr/wsAdeGrp.php?projectId=1';
  37. /**
  38. * Class defining planex group selection screen.
  39. */
  40. class GroupSelectionScreen extends React.Component<PropsType, StateType> {
  41. constructor(props: PropsType) {
  42. super(props);
  43. this.state = {
  44. currentSearchString: '',
  45. favoriteGroups: AsyncStorageManager.getObject(
  46. AsyncStorageManager.PREFERENCES.planexFavoriteGroups.key,
  47. ),
  48. };
  49. }
  50. /**
  51. * Creates the header content
  52. */
  53. componentDidMount() {
  54. const {navigation} = this.props;
  55. navigation.setOptions({
  56. headerTitle: this.getSearchBar,
  57. headerBackTitleVisible: false,
  58. headerTitleContainerStyle:
  59. Platform.OS === 'ios'
  60. ? {marginHorizontal: 0, width: '70%'}
  61. : {marginHorizontal: 0, right: 50, left: 50},
  62. });
  63. }
  64. /**
  65. * Gets the header search bar
  66. *
  67. * @return {*}
  68. */
  69. getSearchBar = (): React.Node => {
  70. return (
  71. <Searchbar
  72. placeholder={i18n.t('screens.proximo.search')}
  73. onChangeText={this.onSearchStringChange}
  74. />
  75. );
  76. };
  77. /**
  78. * Gets a render item for the given article
  79. *
  80. * @param item The article to render
  81. * @return {*}
  82. */
  83. getRenderItem = ({item}: {item: PlanexGroupCategoryType}): React.Node => {
  84. const {currentSearchString, favoriteGroups} = this.state;
  85. if (
  86. this.shouldDisplayAccordion(item) ||
  87. (item.id === 0 && item.content.length === 0)
  88. ) {
  89. return (
  90. <GroupListAccordion
  91. item={item}
  92. favorites={[...favoriteGroups]}
  93. onGroupPress={this.onListItemPress}
  94. onFavoritePress={this.onListFavoritePress}
  95. currentSearchString={currentSearchString}
  96. height={LIST_ITEM_HEIGHT}
  97. />
  98. );
  99. }
  100. return null;
  101. };
  102. /**
  103. * Creates the dataset to be used in the FlatList
  104. *
  105. * @param fetchedData
  106. * @return {*}
  107. * */
  108. createDataset = (fetchedData: {
  109. [key: string]: PlanexGroupCategoryType,
  110. }): Array<{title: string, data: Array<PlanexGroupCategoryType>}> => {
  111. return [
  112. {
  113. title: '',
  114. data: this.generateData(fetchedData),
  115. },
  116. ];
  117. };
  118. /**
  119. * Callback used when the search changes
  120. *
  121. * @param str The new search string
  122. */
  123. onSearchStringChange = (str: string) => {
  124. this.setState({currentSearchString: str});
  125. };
  126. /**
  127. * Callback used when clicking an article in the list.
  128. * It opens the modal to show detailed information about the article
  129. *
  130. * @param item The article pressed
  131. */
  132. onListItemPress = (item: PlanexGroupType) => {
  133. const {navigation} = this.props;
  134. navigation.navigate('planex', {
  135. screen: 'index',
  136. params: {group: item},
  137. });
  138. };
  139. /**
  140. * Callback used when the user clicks on the favorite button
  141. *
  142. * @param item The item to add/remove from favorites
  143. */
  144. onListFavoritePress = (item: PlanexGroupType) => {
  145. this.updateGroupFavorites(item);
  146. };
  147. /**
  148. * Checks if the given group is in the favorites list
  149. *
  150. * @param group The group to check
  151. * @returns {boolean}
  152. */
  153. isGroupInFavorites(group: PlanexGroupType): boolean {
  154. let isFav = false;
  155. const {favoriteGroups} = this.state;
  156. favoriteGroups.forEach((favGroup: PlanexGroupType) => {
  157. if (group.id === favGroup.id) isFav = true;
  158. });
  159. return isFav;
  160. }
  161. /**
  162. * Adds or removes the given group to the favorites list, depending on whether it is already in it or not.
  163. * Favorites are then saved in user preferences
  164. *
  165. * @param group The group to add/remove to favorites
  166. */
  167. updateGroupFavorites(group: PlanexGroupType) {
  168. if (this.isGroupInFavorites(group)) this.removeGroupFromFavorites(group);
  169. else this.addGroupToFavorites(group);
  170. }
  171. /**
  172. * Checks whether to display the given group category, depending on user search query
  173. *
  174. * @param item The group category
  175. * @returns {boolean}
  176. */
  177. shouldDisplayAccordion(item: PlanexGroupCategoryType): boolean {
  178. const {currentSearchString} = this.state;
  179. let shouldDisplay = false;
  180. for (let i = 0; i < item.content.length; i += 1) {
  181. if (stringMatchQuery(item.content[i].name, currentSearchString)) {
  182. shouldDisplay = true;
  183. break;
  184. }
  185. }
  186. return shouldDisplay;
  187. }
  188. /**
  189. * Generates the dataset to be used in the FlatList.
  190. * This improves formatting of group names, sorts alphabetically the categories, and adds favorites at the top.
  191. *
  192. * @param fetchedData The raw data fetched from the server
  193. * @returns {[]}
  194. */
  195. generateData(fetchedData: {
  196. [key: string]: PlanexGroupCategoryType,
  197. }): Array<PlanexGroupCategoryType> {
  198. const {favoriteGroups} = this.state;
  199. const data = [];
  200. // eslint-disable-next-line flowtype/no-weak-types
  201. (Object.values(fetchedData): Array<any>).forEach(
  202. (category: PlanexGroupCategoryType) => {
  203. data.push(category);
  204. },
  205. );
  206. data.sort(sortName);
  207. data.unshift({
  208. name: i18n.t('screens.planex.favorites'),
  209. id: 0,
  210. content: favoriteGroups,
  211. });
  212. return data;
  213. }
  214. /**
  215. * Removes the given group from the favorites
  216. *
  217. * @param group The group to remove from the array
  218. */
  219. removeGroupFromFavorites(group: PlanexGroupType) {
  220. this.setState((prevState: StateType): {
  221. favoriteGroups: Array<PlanexGroupType>,
  222. } => {
  223. const {favoriteGroups} = prevState;
  224. for (let i = 0; i < favoriteGroups.length; i += 1) {
  225. if (group.id === favoriteGroups[i].id) {
  226. favoriteGroups.splice(i, 1);
  227. break;
  228. }
  229. }
  230. AsyncStorageManager.set(
  231. AsyncStorageManager.PREFERENCES.planexFavoriteGroups.key,
  232. favoriteGroups,
  233. );
  234. return {favoriteGroups};
  235. });
  236. }
  237. /**
  238. * Adds the given group to favorites
  239. *
  240. * @param group The group to add to the array
  241. */
  242. addGroupToFavorites(group: PlanexGroupType) {
  243. this.setState((prevState: StateType): {
  244. favoriteGroups: Array<PlanexGroupType>,
  245. } => {
  246. const {favoriteGroups} = prevState;
  247. favoriteGroups.push(group);
  248. favoriteGroups.sort(sortName);
  249. AsyncStorageManager.set(
  250. AsyncStorageManager.PREFERENCES.planexFavoriteGroups.key,
  251. favoriteGroups,
  252. );
  253. return {favoriteGroups};
  254. });
  255. }
  256. render(): React.Node {
  257. const {props, state} = this;
  258. return (
  259. <WebSectionList
  260. navigation={props.navigation}
  261. createDataset={this.createDataset}
  262. autoRefreshTime={0}
  263. refreshOnFocus={false}
  264. fetchUrl={GROUPS_URL}
  265. renderItem={this.getRenderItem}
  266. updateData={state.currentSearchString + state.favoriteGroups.length}
  267. itemHeight={LIST_ITEM_HEIGHT}
  268. />
  269. );
  270. }
  271. }
  272. export default GroupSelectionScreen;