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.tsx 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 React, { useLayoutEffect, useState } from 'react';
  20. import { View } from 'react-native';
  21. import LogoutDialog from '../../components/Amicale/LogoutDialog';
  22. import MaterialHeaderButtons, {
  23. Item,
  24. } from '../../components/Overrides/CustomHeaderButton';
  25. import CollapsibleFlatList from '../../components/Collapsible/CollapsibleFlatList';
  26. import GENERAL_STYLES from '../../constants/Styles';
  27. import RequestScreen from '../../components/Screens/RequestScreen';
  28. import ProfileWelcomeCard from '../../components/Amicale/Profile/ProfileWelcomeCard';
  29. import ProfilePersonalCard from '../../components/Amicale/Profile/ProfilePersonalCard';
  30. import ProfileClubCard from '../../components/Amicale/Profile/ProfileClubCard';
  31. import ProfileMembershipCard from '../../components/Amicale/Profile/ProfileMembershipCard';
  32. import { useNavigation } from '@react-navigation/core';
  33. import { useAuthenticatedRequest } from '../../context/loginContext';
  34. export type ProfileClubType = {
  35. id: number;
  36. name: string;
  37. is_manager: boolean;
  38. };
  39. export 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<ProfileClubType>;
  49. };
  50. function ProfileScreen() {
  51. const navigation = useNavigation();
  52. const [dialogVisible, setDialogVisible] = useState(false);
  53. const request = useAuthenticatedRequest<ProfileDataType>('user/profile');
  54. useLayoutEffect(() => {
  55. const getHeaderButton = () => (
  56. <MaterialHeaderButtons>
  57. <Item
  58. title={'logout'}
  59. iconName={'logout'}
  60. onPress={showDisconnectDialog}
  61. />
  62. </MaterialHeaderButtons>
  63. );
  64. navigation.setOptions({
  65. headerRight: getHeaderButton,
  66. });
  67. }, [navigation]);
  68. const getScreen = (data: ProfileDataType | undefined) => {
  69. if (data) {
  70. const flatListData: Array<{
  71. id: string;
  72. render: () => React.ReactElement;
  73. }> = [];
  74. for (let i = 0; i < 4; i++) {
  75. switch (i) {
  76. case 0:
  77. flatListData.push({
  78. id: i.toString(),
  79. render: () => <ProfileWelcomeCard firstname={data?.first_name} />,
  80. });
  81. break;
  82. case 1:
  83. flatListData.push({
  84. id: i.toString(),
  85. render: () => <ProfilePersonalCard profile={data} />,
  86. });
  87. break;
  88. case 2:
  89. flatListData.push({
  90. id: i.toString(),
  91. render: () => <ProfileClubCard clubs={data?.clubs} />,
  92. });
  93. break;
  94. default:
  95. flatListData.push({
  96. id: i.toString(),
  97. render: () => <ProfileMembershipCard valid={data?.validity} />,
  98. });
  99. }
  100. }
  101. return (
  102. <View style={GENERAL_STYLES.flex}>
  103. <CollapsibleFlatList renderItem={getRenderItem} data={flatListData} />
  104. <LogoutDialog
  105. visible={dialogVisible}
  106. onDismiss={hideDisconnectDialog}
  107. />
  108. </View>
  109. );
  110. } else {
  111. return <View />;
  112. }
  113. };
  114. const getRenderItem = ({
  115. item,
  116. }: {
  117. item: { id: string; render: () => React.ReactElement };
  118. }) => item.render();
  119. const showDisconnectDialog = () => setDialogVisible(true);
  120. const hideDisconnectDialog = () => setDialogVisible(false);
  121. return <RequestScreen request={request} render={getScreen} />;
  122. }
  123. export default ProfileScreen;