snappymail/dev/Settings/User/ChangePassword.js

109 lines
2.9 KiB
JavaScript
Raw Normal View History

2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
import _ from '_';
import ko from 'ko';
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
import {StorageResultType, Notification} from 'Common/Enums';
import {createCommand} from 'Common/Utils';
import {getNotificationFromResponse, i18n} from 'Common/Translator';
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
import Remote from 'Remote/User/Ajax';
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
class ChangePasswordUserSettings
2016-06-30 08:02:45 +08:00
{
2016-07-16 05:29:42 +08:00
constructor() {
this.changeProcess = ko.observable(false);
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
this.errorDescription = ko.observable('');
this.passwordMismatch = ko.observable(false);
this.passwordUpdateError = ko.observable(false);
this.passwordUpdateSuccess = ko.observable(false);
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
this.currentPassword = ko.observable('');
this.currentPassword.error = ko.observable(false);
this.newPassword = ko.observable('');
this.newPassword2 = ko.observable('');
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
this.currentPassword.subscribe(() => {
2014-08-21 23:08:34 +08:00
this.passwordUpdateError(false);
this.passwordUpdateSuccess(false);
this.currentPassword.error(false);
2016-07-16 05:29:42 +08:00
});
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
this.newPassword.subscribe(() => {
this.passwordUpdateError(false);
this.passwordUpdateSuccess(false);
this.passwordMismatch(false);
});
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
this.newPassword2.subscribe(() => {
this.passwordUpdateError(false);
this.passwordUpdateSuccess(false);
this.passwordMismatch(false);
});
this.saveNewPasswordCommand = createCommand(this, () => {
if (this.newPassword() !== this.newPassword2())
{
this.passwordMismatch(true);
this.errorDescription(i18n('SETTINGS_CHANGE_PASSWORD/ERROR_PASSWORD_MISMATCH'));
}
else
{
this.changeProcess(true);
this.passwordUpdateError(false);
this.passwordUpdateSuccess(false);
this.currentPassword.error(false);
this.passwordMismatch(false);
this.errorDescription('');
Remote.changePassword(this.onChangePasswordResponse, this.currentPassword(), this.newPassword());
}
}, () => !this.changeProcess() && '' !== this.currentPassword() && '' !== this.newPassword() && '' !== this.newPassword2());
this.onChangePasswordResponse = _.bind(this.onChangePasswordResponse, this);
}
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
onHide() {
this.changeProcess(false);
2014-08-21 23:08:34 +08:00
this.currentPassword('');
this.newPassword('');
this.newPassword2('');
2016-07-16 05:29:42 +08:00
this.errorDescription('');
this.passwordMismatch(false);
this.currentPassword.error(false);
}
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
onChangePasswordResponse(result, data) {
this.changeProcess(false);
this.passwordMismatch(false);
this.errorDescription('');
2014-08-21 23:08:34 +08:00
this.currentPassword.error(false);
2016-07-16 05:29:42 +08:00
if (StorageResultType.Success === result && data && data.Result)
2014-08-21 23:08:34 +08:00
{
2016-07-16 05:29:42 +08:00
this.currentPassword('');
this.newPassword('');
this.newPassword2('');
this.passwordUpdateSuccess(true);
this.currentPassword.error(false);
require('App/User').default.setClientSideToken(data.Result);
2014-08-21 23:08:34 +08:00
}
2016-07-16 05:29:42 +08:00
else
{
if (data && Notification.CurrentPasswordIncorrect === data.ErrorCode)
{
this.currentPassword.error(true);
}
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
this.passwordUpdateError(true);
this.errorDescription(getNotificationFromResponse(data, Notification.CouldNotSaveNewPassword));
}
2016-06-30 08:02:45 +08:00
}
2016-07-16 05:29:42 +08:00
}
2014-08-21 23:08:34 +08:00
2016-07-16 03:54:37 +08:00
export {ChangePasswordUserSettings, ChangePasswordUserSettings as default};