blob: 3cec65a59479df043414420ec5cfc83fbbf205b2 [file] [log] [blame]
Wyatt Allen767b7602016-06-15 16:51:20 -07001// Copyright (C) 2016 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14(function() {
15 'use strict';
16
17 Polymer({
18 is: 'gr-account-info',
19
Logan Hanks25f49af2016-10-10 17:26:43 -070020 /**
21 * Fired when account details are changed.
22 *
23 * @event account-detail-update
24 */
25
Wyatt Allen767b7602016-06-15 16:51:20 -070026 properties: {
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -080027 usernameMutable: {
Wyatt Allen767b7602016-06-15 16:51:20 -070028 type: Boolean,
29 notify: true,
Kasper Nilssonf4d03fc2017-11-14 15:12:50 -080030 computed: '_computeUsernameMutable(_serverConfig, _account.username)',
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -080031 },
32 nameMutable: {
33 type: Boolean,
34 notify: true,
35 computed: '_computeNameMutable(_serverConfig)',
Wyatt Allen767b7602016-06-15 16:51:20 -070036 },
37 hasUnsavedChanges: {
38 type: Boolean,
39 notify: true,
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -080040 computed: '_computeHasUnsavedChanges(_hasNameChange, ' +
41 '_hasUsernameChange, _hasStatusChange)',
Wyatt Allen767b7602016-06-15 16:51:20 -070042 },
43
Kasper Nilsson5cb34e72017-12-27 14:12:15 -080044 _hasNameChange: Boolean,
45 _hasUsernameChange: Boolean,
46 _hasStatusChange: Boolean,
Wyatt Allen767b7602016-06-15 16:51:20 -070047 _loading: {
48 type: Boolean,
49 value: false,
50 },
51 _saving: {
52 type: Boolean,
53 value: false,
54 },
Becky Siegel8d92d532017-08-11 16:32:47 -070055 /** @type {?} */
Wyatt Allen767b7602016-06-15 16:51:20 -070056 _account: Object,
57 _serverConfig: Object,
Kasper Nilssonf4d03fc2017-11-14 15:12:50 -080058 _username: {
59 type: String,
60 observer: '_usernameChanged',
61 },
Wyatt Allen767b7602016-06-15 16:51:20 -070062 },
63
64 observers: [
Kasper Nilsson58273122017-03-09 10:55:10 -080065 '_nameChanged(_account.name)',
66 '_statusChanged(_account.status)',
Wyatt Allen767b7602016-06-15 16:51:20 -070067 ],
68
Kasper Nilsson95131342017-05-15 17:13:16 -070069 loadData() {
70 const promises = [];
Wyatt Allen767b7602016-06-15 16:51:20 -070071
72 this._loading = true;
73
Kasper Nilsson95131342017-05-15 17:13:16 -070074 promises.push(this.$.restAPI.getConfig().then(config => {
Wyatt Allen767b7602016-06-15 16:51:20 -070075 this._serverConfig = config;
Kasper Nilsson95131342017-05-15 17:13:16 -070076 }));
Wyatt Allen767b7602016-06-15 16:51:20 -070077
Kasper Nilsson95131342017-05-15 17:13:16 -070078 promises.push(this.$.restAPI.getAccount().then(account => {
Kasper Nilsson5cb34e72017-12-27 14:12:15 -080079 this._hasNameChange = false;
80 this._hasUsernameChange = false;
81 this._hasStatusChange = false;
Kasper Nilssonf4d03fc2017-11-14 15:12:50 -080082 // Provide predefined value for username to trigger computation of
83 // username mutability.
84 account.username = account.username || '';
Wyatt Allen767b7602016-06-15 16:51:20 -070085 this._account = account;
Kasper Nilssonf4d03fc2017-11-14 15:12:50 -080086 this._username = account.username;
Kasper Nilsson95131342017-05-15 17:13:16 -070087 }));
Wyatt Allen767b7602016-06-15 16:51:20 -070088
Kasper Nilsson95131342017-05-15 17:13:16 -070089 return Promise.all(promises).then(() => {
Wyatt Allen767b7602016-06-15 16:51:20 -070090 this._loading = false;
Kasper Nilsson95131342017-05-15 17:13:16 -070091 });
Wyatt Allen767b7602016-06-15 16:51:20 -070092 },
93
Kasper Nilsson95131342017-05-15 17:13:16 -070094 save() {
Paladox nonefbe932e2017-04-26 21:36:06 +000095 if (!this.hasUnsavedChanges) {
Wyatt Allen767b7602016-06-15 16:51:20 -070096 return Promise.resolve();
97 }
98
99 this._saving = true;
Kasper Nilsson58273122017-03-09 10:55:10 -0800100 // Set only the fields that have changed.
101 // Must be done in sequence to avoid race conditions (@see Issue 5721)
102 return this._maybeSetName()
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -0800103 .then(this._maybeSetUsername.bind(this))
Kasper Nilsson58273122017-03-09 10:55:10 -0800104 .then(this._maybeSetStatus.bind(this))
Kasper Nilsson95131342017-05-15 17:13:16 -0700105 .then(() => {
Kasper Nilsson58273122017-03-09 10:55:10 -0800106 this._hasNameChange = false;
107 this._hasStatusChange = false;
108 this._saving = false;
109 this.fire('account-detail-update');
Kasper Nilsson95131342017-05-15 17:13:16 -0700110 });
Kasper Nilsson58273122017-03-09 10:55:10 -0800111 },
112
Kasper Nilsson95131342017-05-15 17:13:16 -0700113 _maybeSetName() {
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -0800114 return this._hasNameChange && this.nameMutable ?
115 this.$.restAPI.setAccountName(this._account.name) :
116 Promise.resolve();
117 },
118
119 _maybeSetUsername() {
120 return this._hasUsernameChange && this.usernameMutable ?
Kasper Nilssonf4d03fc2017-11-14 15:12:50 -0800121 this.$.restAPI.setAccountUsername(this._username) :
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -0800122 Promise.resolve();
Kasper Nilsson58273122017-03-09 10:55:10 -0800123 },
124
Kasper Nilsson95131342017-05-15 17:13:16 -0700125 _maybeSetStatus() {
Kasper Nilsson58273122017-03-09 10:55:10 -0800126 return this._hasStatusChange ?
127 this.$.restAPI.setAccountStatus(this._account.status) :
128 Promise.resolve();
129 },
130
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -0800131 _computeHasUnsavedChanges(nameChanged, usernameChanged, statusChanged) {
132 return nameChanged || usernameChanged || statusChanged;
Wyatt Allen767b7602016-06-15 16:51:20 -0700133 },
134
Kasper Nilssonf4d03fc2017-11-14 15:12:50 -0800135 _computeUsernameMutable(config, username) {
136 // Username may not be changed once it is set.
137 return config.auth.editable_account_fields.includes('USER_NAME') &&
138 !username;
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -0800139 },
140
141 _computeNameMutable(config) {
Kasper Nilsson95131342017-05-15 17:13:16 -0700142 return config.auth.editable_account_fields.includes('FULL_NAME');
Wyatt Allen767b7602016-06-15 16:51:20 -0700143 },
144
Kasper Nilsson95131342017-05-15 17:13:16 -0700145 _statusChanged() {
Wyatt Allen767b7602016-06-15 16:51:20 -0700146 if (this._loading) { return; }
Kasper Nilsson58273122017-03-09 10:55:10 -0800147 this._hasStatusChange = true;
148 },
149
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -0800150 _usernameChanged() {
Kasper Nilsson5cb34e72017-12-27 14:12:15 -0800151 if (this._loading || !this._account) { return; }
152 this._hasUsernameChange =
153 (this._account.username || '') !== (this._username || '');
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -0800154 },
155
Kasper Nilsson95131342017-05-15 17:13:16 -0700156 _nameChanged() {
Kasper Nilsson58273122017-03-09 10:55:10 -0800157 if (this._loading) { return; }
158 this._hasNameChange = true;
Wyatt Allen767b7602016-06-15 16:51:20 -0700159 },
160
Kasper Nilsson95131342017-05-15 17:13:16 -0700161 _handleKeydown(e) {
Wyatt Allen767b7602016-06-15 16:51:20 -0700162 if (e.keyCode === 13) { // Enter
163 e.stopPropagation();
164 this.save();
165 }
166 },
167 });
168})();