-
Notifications
You must be signed in to change notification settings - Fork 482
/
DeleteAccount.jsx
212 lines (191 loc) · 5.7 KB
/
DeleteAccount.jsx
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import React, {PropTypes} from 'react';
import $ from 'jquery';
import i18n from '@cdo/locale';
import color from '@cdo/apps/util/color';
import {
TeacherWarning,
StudentWarning,
getLabelForCheckbox,
} from './DeleteAccountHelpers';
import {navigateToHref} from '@cdo/apps/utils';
import BootstrapButton from './BootstrapButton';
import PersonalLoginDialog from './PersonalLoginDialog';
import DeleteAccountDialog from './DeleteAccountDialog';
export const DELETE_VERIFICATION_STRING = i18n.deleteAccountDialog_verificationString();
const styles = {
container: {
paddingTop: 20,
},
hr: {
borderColor: color.red,
},
header: {
fontSize: 22,
color: color.red,
},
warning: {
marginTop: 10,
marginBottom: 10,
},
buttonContainer: {
display: 'flex',
justifyContent: 'flex-end',
},
};
export const buildCheckboxMap = () => {
let checkboxMap = {};
for (var i = 1; i <= 5; i++) {
checkboxMap[i] = {
checked: false,
label: getLabelForCheckbox(i)
};
}
return checkboxMap;
};
const DEFAULT_STATE = {
isPersonalLoginDialogOpen: false,
isDeleteAccountDialogOpen: false,
password: '',
passwordError: '',
deleteVerification: '',
deleteError: '',
checkboxes: buildCheckboxMap(),
};
export default class DeleteAccount extends React.Component {
static propTypes = {
isPasswordRequired: PropTypes.bool.isRequired,
isTeacher: PropTypes.bool.isRequired,
dependedUponForLogin: PropTypes.bool,
hasStudents: PropTypes.bool.isRequired,
};
state = DEFAULT_STATE;
togglePersonalLoginDialog = () => {
this.setState(state => {
return {
...DEFAULT_STATE,
isPersonalLoginDialogOpen: !state.isPersonalLoginDialogOpen
};
});
};
toggleDeleteAccountDialog = () => {
this.setState(state => {
return {
...DEFAULT_STATE,
isDeleteAccountDialogOpen: !state.isDeleteAccountDialogOpen
};
});
};
// Closes PersonalLoginDialog and opens DeleteAccountDialog
goToDeleteAccountDialog = () => {
this.setState({
isPersonalLoginDialogOpen: false,
isDeleteAccountDialogOpen: true,
});
};
onCheckboxChange = (id) => {
const checkboxes = {...this.state.checkboxes};
checkboxes[id].checked = !checkboxes[id].checked;
this.setState({checkboxes});
};
onPasswordChange = (event) => {
this.setState({
password: event.target.value
});
};
onDeleteVerificationChange = (event) => {
this.setState({
deleteVerification: event.target.value
});
};
allCheckboxesChecked = () => {
const {checkboxes} = this.state;
return Object.keys(checkboxes).every(id => checkboxes[id].checked);
};
isValid = () => {
const {isPasswordRequired, isTeacher, hasStudents} = this.props;
const {password, deleteVerification} = this.state;
const isPasswordValid = isPasswordRequired ? (password.length > 0) : true;
const areCheckboxesValid = isTeacher && hasStudents ? this.allCheckboxesChecked() : true;
const isDeleteVerificationValid = deleteVerification === DELETE_VERIFICATION_STRING;
return isPasswordValid && areCheckboxesValid && isDeleteVerificationValid;
};
deleteUser = () => {
const payload = {
new_destroy_flow: true,
password_confirmation: this.state.password
};
$.ajax({
url: '/users',
method: 'DELETE',
data: payload
}).done(result => {
navigateToHref('/');
}).fail((jqXhr, _) => {
this.onFailure(jqXhr);
});
};
onFailure = (xhr) => {
const responseJSON = xhr.responseJSON;
let newState;
if (responseJSON && responseJSON.error) {
const passwordErrors = responseJSON.error.current_password;
newState = {passwordError: passwordErrors[0]};
} else {
newState = {deleteError: `Unexpected error: ${xhr.status}`};
}
this.setState(newState);
};
render() {
const {isTeacher, dependedUponForLogin, isPasswordRequired, hasStudents} = this.props;
const {
isPersonalLoginDialogOpen,
isDeleteAccountDialogOpen,
checkboxes,
password,
passwordError,
deleteVerification,
deleteError,
} = this.state;
return (
<div style={styles.container}>
<hr style={styles.hr} />
<h2 style={styles.header}>
{i18n.deleteAccount()}
</h2>
<div style={styles.warning}>
{isTeacher ? <TeacherWarning/> : <StudentWarning/>}
</div>
<div style={styles.buttonContainer}>
{/* This button intentionally uses BootstrapButton to match other account page buttons */}
<BootstrapButton
type="danger"
text={i18n.deleteAccount()}
onClick={(isTeacher && dependedUponForLogin) ? this.togglePersonalLoginDialog : this.toggleDeleteAccountDialog}
/>
</div>
<PersonalLoginDialog
isOpen={isPersonalLoginDialogOpen}
onCancel={this.togglePersonalLoginDialog}
onConfirm={this.goToDeleteAccountDialog}
/>
<DeleteAccountDialog
isOpen={isDeleteAccountDialogOpen}
isTeacher={isTeacher}
isPasswordRequired={isPasswordRequired}
warnAboutDeletingStudents={isTeacher && hasStudents}
checkboxes={checkboxes}
password={password}
passwordError={passwordError}
deleteVerification={deleteVerification}
onCheckboxChange={this.onCheckboxChange}
onPasswordChange={this.onPasswordChange}
onDeleteVerificationChange={this.onDeleteVerificationChange}
onCancel={this.toggleDeleteAccountDialog}
disableConfirm={!this.isValid()}
deleteUser={this.deleteUser}
deleteError={deleteError}
/>
</div>
);
}
}