-
Notifications
You must be signed in to change notification settings - Fork 5k
/
user-info-overview.tsx
172 lines (161 loc) · 9.43 KB
/
user-info-overview.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import * as React from 'react';
import {FormField, NotificationType, SlidingPanel} from 'argo-ui/src/index';
import * as PropTypes from 'prop-types';
import {Form, FormApi, FormValue, Nested, Text} from 'react-form';
import {RouteComponentProps} from 'react-router';
import {DataLoader, ErrorNotification, Page, Spinner} from '../../../shared/components';
import {AppContext} from '../../../shared/context';
import {services} from '../../../shared/services';
require('./user-info-overview.scss');
export class UserInfoOverview extends React.Component<RouteComponentProps<any>, {connecting: boolean}> {
public static contextTypes = {
router: PropTypes.object,
apis: PropTypes.object,
history: PropTypes.object
};
private formApiPassword: FormApi;
constructor(props: RouteComponentProps<any>) {
super(props);
this.state = {connecting: false};
}
public render() {
return (
<DataLoader key='userInfo' load={() => services.users.get()}>
{userInfo => (
<Page
title='User Info'
toolbar={{
breadcrumbs: [{title: 'User Info'}],
actionMenu:
userInfo.loggedIn && userInfo.iss === 'argocd'
? {
items: [
{
iconClassName: 'fa fa-lock',
title: 'Update Password',
action: () => (this.showChangePassword = true)
}
]
}
: {items: []}
}}>
<div>
<div className='user-info'>
<div className='argo-container'>
<div className='user-info-overview__panel white-box'>
{userInfo.loggedIn ? (
<React.Fragment key='userInfoInner'>
<p key='username'>Username: {userInfo.username}</p>
<p key='iss'>Issuer: {userInfo.iss}</p>
{userInfo.groups && (
<React.Fragment key='userInfo4'>
<p>Groups:</p>
<ul>
{userInfo.groups.map(group => (
<li key={group}>{group}</li>
))}
</ul>
</React.Fragment>
)}
</React.Fragment>
) : (
<p key='loggedOutMessage'>You are not logged in</p>
)}
</div>
</div>
</div>
{userInfo.loggedIn && userInfo.iss === 'argocd' ? (
<SlidingPanel
isShown={this.showChangePassword}
onClose={() => (this.showChangePassword = false)}
header={
<div>
<button
className='argo-button argo-button--base'
onClick={() => {
this.formApiPassword.submitForm(null);
}}>
<Spinner show={this.state.connecting} style={{marginRight: '5px'}} />
Save New Password
</button>{' '}
<button onClick={() => (this.showChangePassword = false)} className='argo-button argo-button--base-o'>
Cancel
</button>
</div>
}>
<h4>Update account password</h4>
<Form
onSubmit={params => this.changePassword(userInfo.username, params.currentPassword, params.newPassword)}
getApi={api => (this.formApiPassword = api)}
defaultValues={{type: 'git'}}
validateError={(params: {currentPassword: string; newPassword: string; confirmNewPassword: string}) => ({
currentPassword: !params.currentPassword && 'Current password is required.',
newPassword:
(!params.newPassword && 'New password is required.') ||
(params.newPassword !== params.confirmNewPassword && 'Confirm your new password.'),
confirmNewPassword: (!params.confirmNewPassword || params.confirmNewPassword !== params.newPassword) && 'Confirm your new password.'
})}>
{formApi => (
<form onSubmit={formApi.submitForm} role='form' className='change-password width-control'>
<div className='argo-form-row'>
<FormField
formApi={formApi}
label='Current Password'
field='currentPassword'
component={Text}
componentProps={{type: 'password'}}
/>
</div>
<div className='argo-form-row'>
<FormField formApi={formApi} label='New Password' field='newPassword' component={Text} componentProps={{type: 'password'}} />
</div>
<div className='argo-form-row'>
<FormField
formApi={formApi}
label='Confirm New Password'
field='confirmNewPassword'
component={Text}
componentProps={{type: 'password'}}
/>
</div>
</form>
)}
</Form>
</SlidingPanel>
) : (
<div />
)}
</div>
</Page>
)}
</DataLoader>
);
}
private async changePassword(username: string, currentPassword: Nested<FormValue> | FormValue, newPassword: Nested<FormValue> | FormValue) {
try {
await services.accounts.changePassword(username, currentPassword, newPassword);
this.appContext.apis.notifications.show({type: NotificationType.Success, content: 'Your password has been successfully updated.'});
this.showChangePassword = false;
} catch (e) {
this.appContext.apis.notifications.show({
content: <ErrorNotification title='Unable to update your password.' e={e} />,
type: NotificationType.Error
});
}
}
// Whether to show the HTTPS repository connection dialogue on the page
private get showChangePassword() {
return new URLSearchParams(this.props.location.search).get('changePassword') === 'true';
}
private set showChangePassword(val: boolean) {
this.clearChangePasswordForm();
this.appContext.router.history.push(`${this.props.match.url}?changePassword=${val}`);
}
// Empty all fields in HTTPS repository form
private clearChangePasswordForm() {
this.formApiPassword.resetAll();
}
private get appContext(): AppContext {
return this.context as AppContext;
}
}