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.

ProfileScreen.js 12KB

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