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.

ProfileScreen.tsx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import * as React from 'react';
  20. import {FlatList, StyleSheet, View} from 'react-native';
  21. import {
  22. Avatar,
  23. Button,
  24. Card,
  25. Divider,
  26. List,
  27. Paragraph,
  28. withTheme,
  29. } from 'react-native-paper';
  30. import i18n from 'i18n-js';
  31. import {StackNavigationProp} from '@react-navigation/stack';
  32. import AuthenticatedScreen from '../../components/Amicale/AuthenticatedScreen';
  33. import LogoutDialog from '../../components/Amicale/LogoutDialog';
  34. import MaterialHeaderButtons, {
  35. Item,
  36. } from '../../components/Overrides/CustomHeaderButton';
  37. import CardList from '../../components/Lists/CardList/CardList';
  38. import AvailableWebsites from '../../constants/AvailableWebsites';
  39. import Mascot, {MASCOT_STYLE} from '../../components/Mascot/Mascot';
  40. import ServicesManager, {SERVICES_KEY} from '../../managers/ServicesManager';
  41. import CollapsibleFlatList from '../../components/Collapsible/CollapsibleFlatList';
  42. import type {ServiceItemType} from '../../managers/ServicesManager';
  43. type PropsType = {
  44. navigation: StackNavigationProp<any>;
  45. theme: ReactNativePaper.Theme;
  46. };
  47. type StateType = {
  48. dialogVisible: boolean;
  49. };
  50. type ClubType = {
  51. id: number;
  52. name: string;
  53. is_manager: boolean;
  54. };
  55. type ProfileDataType = {
  56. first_name: string;
  57. last_name: string;
  58. email: string;
  59. birthday: string;
  60. phone: string;
  61. branch: string;
  62. link: string;
  63. validity: boolean;
  64. clubs: Array<ClubType>;
  65. };
  66. const styles = StyleSheet.create({
  67. card: {
  68. margin: 10,
  69. },
  70. icon: {
  71. backgroundColor: 'transparent',
  72. },
  73. editButton: {
  74. marginLeft: 'auto',
  75. },
  76. });
  77. class ProfileScreen extends React.Component<PropsType, StateType> {
  78. data: ProfileDataType | null;
  79. flatListData: Array<{id: string}>;
  80. amicaleDataset: Array<ServiceItemType>;
  81. constructor(props: PropsType) {
  82. super(props);
  83. this.data = null;
  84. this.flatListData = [{id: '0'}, {id: '1'}, {id: '2'}, {id: '3'}];
  85. const services = new ServicesManager(props.navigation);
  86. this.amicaleDataset = services.getAmicaleServices([SERVICES_KEY.PROFILE]);
  87. this.state = {
  88. dialogVisible: false,
  89. };
  90. }
  91. componentDidMount() {
  92. const {navigation} = this.props;
  93. navigation.setOptions({
  94. headerRight: this.getHeaderButton,
  95. });
  96. }
  97. /**
  98. * Gets the logout header button
  99. *
  100. * @returns {*}
  101. */
  102. getHeaderButton = () => (
  103. <MaterialHeaderButtons>
  104. <Item
  105. title="logout"
  106. iconName="logout"
  107. onPress={this.showDisconnectDialog}
  108. />
  109. </MaterialHeaderButtons>
  110. );
  111. /**
  112. * Gets the main screen component with the fetched data
  113. *
  114. * @param data The data fetched from the server
  115. * @returns {*}
  116. */
  117. getScreen = (data: Array<ProfileDataType | null>) => {
  118. const {dialogVisible} = this.state;
  119. this.data = data[0];
  120. return (
  121. <View style={{flex: 1}}>
  122. <CollapsibleFlatList
  123. renderItem={this.getRenderItem}
  124. data={this.flatListData}
  125. />
  126. <LogoutDialog
  127. visible={dialogVisible}
  128. onDismiss={this.hideDisconnectDialog}
  129. />
  130. </View>
  131. );
  132. };
  133. getRenderItem = ({item}: {item: {id: string}}) => {
  134. switch (item.id) {
  135. case '0':
  136. return this.getWelcomeCard();
  137. case '1':
  138. return this.getPersonalCard();
  139. case '2':
  140. return this.getClubCard();
  141. default:
  142. return this.getMembershipCar();
  143. }
  144. };
  145. /**
  146. * Gets the list of services available with the Amicale account
  147. *
  148. * @returns {*}
  149. */
  150. getServicesList() {
  151. return <CardList dataset={this.amicaleDataset} isHorizontal />;
  152. }
  153. /**
  154. * Gets a card welcoming the user to his account
  155. *
  156. * @returns {*}
  157. */
  158. getWelcomeCard() {
  159. const {navigation} = this.props;
  160. return (
  161. <Card style={styles.card}>
  162. <Card.Title
  163. title={i18n.t('screens.profile.welcomeTitle', {
  164. name: this.data?.first_name,
  165. })}
  166. left={() => (
  167. <Mascot
  168. style={{
  169. width: 60,
  170. }}
  171. emotion={MASCOT_STYLE.COOL}
  172. animated
  173. entryAnimation={{
  174. animation: 'bounceIn',
  175. duration: 1000,
  176. }}
  177. />
  178. )}
  179. titleStyle={{marginLeft: 10}}
  180. />
  181. <Card.Content>
  182. <Divider />
  183. <Paragraph>{i18n.t('screens.profile.welcomeDescription')}</Paragraph>
  184. {this.getServicesList()}
  185. <Paragraph>{i18n.t('screens.profile.welcomeFeedback')}</Paragraph>
  186. <Divider />
  187. <Card.Actions>
  188. <Button
  189. icon="bug"
  190. mode="contained"
  191. onPress={() => {
  192. navigation.navigate('feedback');
  193. }}
  194. style={styles.editButton}>
  195. {i18n.t('screens.feedback.homeButtonTitle')}
  196. </Button>
  197. </Card.Actions>
  198. </Card.Content>
  199. </Card>
  200. );
  201. }
  202. /**
  203. * Gets the given field value.
  204. * If the field does not have a value, returns a placeholder text
  205. *
  206. * @param field The field to get the value from
  207. * @return {*}
  208. */
  209. static getFieldValue(field?: string): string {
  210. return field ? field : i18n.t('screens.profile.noData');
  211. }
  212. /**
  213. * Gets a list item showing personal information
  214. *
  215. * @param field The field to display
  216. * @param icon The icon to use
  217. * @return {*}
  218. */
  219. getPersonalListItem(field: string | undefined, icon: string) {
  220. const {theme} = this.props;
  221. const title = field != null ? ProfileScreen.getFieldValue(field) : ':(';
  222. const subtitle = field != null ? '' : ProfileScreen.getFieldValue(field);
  223. return (
  224. <List.Item
  225. title={title}
  226. description={subtitle}
  227. left={(props) => (
  228. <List.Icon
  229. style={props.style}
  230. icon={icon}
  231. color={field != null ? props.color : theme.colors.textDisabled}
  232. />
  233. )}
  234. />
  235. );
  236. }
  237. /**
  238. * Gets a card containing user personal information
  239. *
  240. * @return {*}
  241. */
  242. getPersonalCard() {
  243. const {theme, navigation} = this.props;
  244. return (
  245. <Card style={styles.card}>
  246. <Card.Title
  247. title={`${this.data?.first_name} ${this.data?.last_name}`}
  248. subtitle={this.data?.email}
  249. left={(iconProps) => (
  250. <Avatar.Icon
  251. size={iconProps.size}
  252. icon="account"
  253. color={theme.colors.primary}
  254. style={styles.icon}
  255. />
  256. )}
  257. />
  258. <Card.Content>
  259. <Divider />
  260. <List.Section>
  261. <List.Subheader>
  262. {i18n.t('screens.profile.personalInformation')}
  263. </List.Subheader>
  264. {this.getPersonalListItem(this.data?.birthday, 'cake-variant')}
  265. {this.getPersonalListItem(this.data?.phone, 'phone')}
  266. {this.getPersonalListItem(this.data?.email, 'email')}
  267. {this.getPersonalListItem(this.data?.branch, 'school')}
  268. </List.Section>
  269. <Divider />
  270. <Card.Actions>
  271. <Button
  272. icon="account-edit"
  273. mode="contained"
  274. onPress={() => {
  275. navigation.navigate('website', {
  276. host: AvailableWebsites.websites.AMICALE,
  277. path: this.data?.link,
  278. title: i18n.t('screens.websites.amicale'),
  279. });
  280. }}
  281. style={styles.editButton}>
  282. {i18n.t('screens.profile.editInformation')}
  283. </Button>
  284. </Card.Actions>
  285. </Card.Content>
  286. </Card>
  287. );
  288. }
  289. /**
  290. * Gets a cars containing clubs the user is part of
  291. *
  292. * @return {*}
  293. */
  294. getClubCard() {
  295. const {theme} = this.props;
  296. return (
  297. <Card style={styles.card}>
  298. <Card.Title
  299. title={i18n.t('screens.profile.clubs')}
  300. subtitle={i18n.t('screens.profile.clubsSubtitle')}
  301. left={(iconProps) => (
  302. <Avatar.Icon
  303. size={iconProps.size}
  304. icon="account-group"
  305. color={theme.colors.primary}
  306. style={styles.icon}
  307. />
  308. )}
  309. />
  310. <Card.Content>
  311. <Divider />
  312. {this.getClubList(this.data?.clubs)}
  313. </Card.Content>
  314. </Card>
  315. );
  316. }
  317. /**
  318. * Gets a card showing if the user has payed his membership
  319. *
  320. * @return {*}
  321. */
  322. getMembershipCar() {
  323. const {theme} = this.props;
  324. return (
  325. <Card style={styles.card}>
  326. <Card.Title
  327. title={i18n.t('screens.profile.membership')}
  328. subtitle={i18n.t('screens.profile.membershipSubtitle')}
  329. left={(iconProps) => (
  330. <Avatar.Icon
  331. size={iconProps.size}
  332. icon="credit-card"
  333. color={theme.colors.primary}
  334. style={styles.icon}
  335. />
  336. )}
  337. />
  338. <Card.Content>
  339. <List.Section>
  340. {this.getMembershipItem(this.data?.validity === true)}
  341. </List.Section>
  342. </Card.Content>
  343. </Card>
  344. );
  345. }
  346. /**
  347. * Gets the item showing if the user has payed his membership
  348. *
  349. * @return {*}
  350. */
  351. getMembershipItem(state: boolean) {
  352. const {theme} = this.props;
  353. return (
  354. <List.Item
  355. title={
  356. state
  357. ? i18n.t('screens.profile.membershipPayed')
  358. : i18n.t('screens.profile.membershipNotPayed')
  359. }
  360. left={(props) => (
  361. <List.Icon
  362. style={props.style}
  363. color={state ? theme.colors.success : theme.colors.danger}
  364. icon={state ? 'check' : 'close'}
  365. />
  366. )}
  367. />
  368. );
  369. }
  370. /**
  371. * Gets a list item for the club list
  372. *
  373. * @param item The club to render
  374. * @return {*}
  375. */
  376. getClubListItem = ({item}: {item: ClubType}) => {
  377. const {theme} = this.props;
  378. const onPress = () => {
  379. this.openClubDetailsScreen(item.id);
  380. };
  381. let description = i18n.t('screens.profile.isMember');
  382. let icon = (props: {
  383. color: string;
  384. style: {
  385. marginLeft: number;
  386. marginRight: number;
  387. marginVertical?: number;
  388. };
  389. }) => (
  390. <List.Icon color={props.color} style={props.style} icon="chevron-right" />
  391. );
  392. if (item.is_manager) {
  393. description = i18n.t('screens.profile.isManager');
  394. icon = (props) => (
  395. <List.Icon
  396. style={props.style}
  397. icon="star"
  398. color={theme.colors.primary}
  399. />
  400. );
  401. }
  402. return (
  403. <List.Item
  404. title={item.name}
  405. description={description}
  406. left={icon}
  407. onPress={onPress}
  408. />
  409. );
  410. };
  411. /**
  412. * Renders the list of clubs the user is part of
  413. *
  414. * @param list The club list
  415. * @return {*}
  416. */
  417. getClubList(list: Array<ClubType> | undefined) {
  418. if (!list) {
  419. return null;
  420. }
  421. list.sort(this.sortClubList);
  422. return (
  423. <FlatList
  424. renderItem={this.getClubListItem}
  425. keyExtractor={this.clubKeyExtractor}
  426. data={list}
  427. />
  428. );
  429. }
  430. clubKeyExtractor = (item: ClubType): string => item.name;
  431. sortClubList = (a: ClubType): number => (a.is_manager ? -1 : 1);
  432. showDisconnectDialog = () => {
  433. this.setState({dialogVisible: true});
  434. };
  435. hideDisconnectDialog = () => {
  436. this.setState({dialogVisible: false});
  437. };
  438. /**
  439. * Opens the club details screen for the club of given ID
  440. * @param id The club's id to open
  441. */
  442. openClubDetailsScreen(id: number) {
  443. const {navigation} = this.props;
  444. navigation.navigate('club-information', {clubId: id});
  445. }
  446. render() {
  447. const {navigation} = this.props;
  448. return (
  449. <AuthenticatedScreen
  450. navigation={navigation}
  451. requests={[
  452. {
  453. link: 'user/profile',
  454. params: {},
  455. mandatory: true,
  456. },
  457. ]}
  458. renderFunction={this.getScreen}
  459. />
  460. );
  461. }
  462. }
  463. export default withTheme(ProfileScreen);