1+ import React , { Fragment , useEffect , useState } from "react" ;
2+ import { useAppStore } from "../stores/AppStore" ;
3+ import "./Profile.scss" ;
4+ import I18n from "../locale/I18n" ;
5+ import ConfirmationDialog from "../components/ConfirmationDialog.jsx" ;
6+ import DOMPurify from "dompurify" ;
7+ import { Button , ButtonType } from "@surfnet/sds" ;
8+ import { dateFromEpoch } from "../utils/Date.js" ;
9+ import { isEmpty , stopEvent } from "../utils/Utils.js" ;
10+ import { deleteUser , logout } from "../api/index.js" ;
11+ import { SESSION_STORAGE_LOCATION } from "../utils/Login.js" ;
12+ import { useNavigate } from "react-router" ;
13+ import { mainMenuItems } from "../utils/MenuItems.js" ;
14+ import InputField from "../components/InputField.jsx" ;
15+
16+ const Profile = ( { setIsAuthenticated} ) => {
17+ const { user} = useAppStore ( state => state ) ;
18+
19+ const [ confirmation , setConfirmation ] = useState ( { } ) ;
20+ const navigate = useNavigate ( ) ;
21+
22+ useEffect ( ( ) => {
23+ useAppStore . setState ( {
24+ breadcrumbPaths : [
25+ { path : "/home" , value : I18n . t ( "profile.title" ) , menuItemName : mainMenuItems . home } ,
26+ { value : I18n . t ( "breadCrumb.applications" ) }
27+ ]
28+ } ) ;
29+ } , [ ] ) ;
30+
31+ const doDelete = ( e , confirmationRequired ) => {
32+ stopEvent ( e ) ;
33+ if ( confirmationRequired ) {
34+ setConfirmation ( {
35+ open : true ,
36+ cancel : ( ) => setConfirmation ( { open : false } ) ,
37+ action : ( ) => doDelete ( null , false ) ,
38+ question : I18n . t ( "profile.deleteConfirmation" ) ,
39+ okButton : I18n . t ( "forms.delete" )
40+ } ) ;
41+ } else {
42+ deleteUser ( ) . then ( ( ) => {
43+ setConfirmation ( { } ) ;
44+ logout ( )
45+ . then ( ( ) => {
46+ useAppStore . setState ( ( ) => ( {
47+ currentOrganization : { name : "" } ,
48+ breadcrumbPaths : [ ] ,
49+ user : { name : "" }
50+ } ) ) ;
51+ sessionStorage . removeItem ( SESSION_STORAGE_LOCATION ) ;
52+ navigate ( "/authentication-switch" ) ;
53+ setTimeout ( ( ) => {
54+ setIsAuthenticated ( false ) ;
55+ navigate ( "/home" ) ;
56+ } , 150 )
57+ } ) ;
58+ } )
59+ }
60+ }
61+
62+ const { open, cancel, action, question, okButton} = confirmation ;
63+ return (
64+ < div
65+ className = "profile-outer-container" >
66+ { open && < ConfirmationDialog confirm = { action }
67+ cancel = { cancel }
68+ confirmationHeader = { I18n . t ( "forms.delete" ) }
69+ confirmationTxt = { okButton }
70+ isDeleteAction = { true }
71+ question = { question }
72+ /> }
73+ < div className = "profile-header-container" >
74+ < div className = "top-header" >
75+ < h1 > { user . name } </ h1 >
76+ </ div >
77+ < p dangerouslySetInnerHTML = { {
78+ __html : DOMPurify . sanitize ( I18n . t ( "profile.info" ,
79+ { createdAt : dateFromEpoch ( user . createdAt ) } ) )
80+ } } />
81+ </ div >
82+ < div className = "profile" >
83+ { user . superUser &&
84+ < InputField value = { I18n . t ( "profile.superUser" ) }
85+ noInput = { true }
86+ />
87+ }
88+ { user . institutionAdmin &&
89+ < InputField name = { I18n . t ( "profile.institutionAdmin" ) }
90+ noInput = { true }
91+ />
92+ }
93+ < div className = "user-container" >
94+ < h5 > { I18n . t ( "profile.attributes" ) } </ h5 >
95+ { [ "name" , "eduPersonPrincipalName" , "email" , "schacHomeOrganization" ]
96+ . map ( ( attr , index ) =>
97+ < Fragment key = { index } >
98+ < InputField name = { I18n . t ( `profile.${ attr } ` ) }
99+ value = { user [ attr ] }
100+ noInput = { true }
101+ />
102+ </ Fragment >
103+ ) }
104+
105+ { ! isEmpty ( user . organizationMemberships ) &&
106+ < InputField name = { I18n . t ( "profile.organization" ) }
107+ value = { user . organizationMemberships . map ( m => m . organization . name ) . join ( ", " ) }
108+ noInput = { true }
109+ /> }
110+ </ div >
111+ < div className = "delete-container" >
112+ < Button onClick = { e => doDelete ( e , true ) }
113+ type = { ButtonType . DestructiveSecondary }
114+ txt = { I18n . t ( "profile.delete" ) }
115+ />
116+ </ div >
117+ </ div >
118+ </ div >
119+
120+ )
121+
122+ } ;
123+ export default Profile ;
0 commit comments