blob: fcc99aa7772cff9cb7f6a5f0c974f8156447d4dc [file] [log] [blame]
Dave Borowitz8cdc76b2018-03-26 10:04:27 -04001/**
2 * @license
3 * Copyright (C) 2016 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
Wyatt Allen767b7602016-06-15 16:51:20 -070017(function() {
18 'use strict';
19
20 Polymer({
21 is: 'gr-account-info',
22
Logan Hanks25f49af2016-10-10 17:26:43 -070023 /**
24 * Fired when account details are changed.
25 *
26 * @event account-detail-update
27 */
28
Wyatt Allen767b7602016-06-15 16:51:20 -070029 properties: {
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -080030 usernameMutable: {
Wyatt Allen767b7602016-06-15 16:51:20 -070031 type: Boolean,
32 notify: true,
Kasper Nilssonf4d03fc2017-11-14 15:12:50 -080033 computed: '_computeUsernameMutable(_serverConfig, _account.username)',
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -080034 },
35 nameMutable: {
36 type: Boolean,
37 notify: true,
38 computed: '_computeNameMutable(_serverConfig)',
Wyatt Allen767b7602016-06-15 16:51:20 -070039 },
40 hasUnsavedChanges: {
41 type: Boolean,
42 notify: true,
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -080043 computed: '_computeHasUnsavedChanges(_hasNameChange, ' +
44 '_hasUsernameChange, _hasStatusChange)',
Wyatt Allen767b7602016-06-15 16:51:20 -070045 },
46
Kasper Nilsson5cb34e72017-12-27 14:12:15 -080047 _hasNameChange: Boolean,
48 _hasUsernameChange: Boolean,
49 _hasStatusChange: Boolean,
Wyatt Allen767b7602016-06-15 16:51:20 -070050 _loading: {
51 type: Boolean,
52 value: false,
53 },
54 _saving: {
55 type: Boolean,
56 value: false,
57 },
Becky Siegel8d92d532017-08-11 16:32:47 -070058 /** @type {?} */
Wyatt Allen767b7602016-06-15 16:51:20 -070059 _account: Object,
60 _serverConfig: Object,
Kasper Nilssonf4d03fc2017-11-14 15:12:50 -080061 _username: {
62 type: String,
63 observer: '_usernameChanged',
64 },
Paladox none7e4726c2018-10-07 14:29:45 +000065 _avatarChangeUrl: {
66 type: String,
67 value: '',
68 },
Wyatt Allen767b7602016-06-15 16:51:20 -070069 },
70
71 observers: [
Kasper Nilsson58273122017-03-09 10:55:10 -080072 '_nameChanged(_account.name)',
73 '_statusChanged(_account.status)',
Wyatt Allen767b7602016-06-15 16:51:20 -070074 ],
75
Kasper Nilsson95131342017-05-15 17:13:16 -070076 loadData() {
77 const promises = [];
Wyatt Allen767b7602016-06-15 16:51:20 -070078
79 this._loading = true;
80
Kasper Nilsson95131342017-05-15 17:13:16 -070081 promises.push(this.$.restAPI.getConfig().then(config => {
Wyatt Allen767b7602016-06-15 16:51:20 -070082 this._serverConfig = config;
Kasper Nilsson95131342017-05-15 17:13:16 -070083 }));
Wyatt Allen767b7602016-06-15 16:51:20 -070084
Kasper Nilsson95131342017-05-15 17:13:16 -070085 promises.push(this.$.restAPI.getAccount().then(account => {
Kasper Nilsson5cb34e72017-12-27 14:12:15 -080086 this._hasNameChange = false;
87 this._hasUsernameChange = false;
88 this._hasStatusChange = false;
Kasper Nilssonf4d03fc2017-11-14 15:12:50 -080089 // Provide predefined value for username to trigger computation of
90 // username mutability.
91 account.username = account.username || '';
Wyatt Allen767b7602016-06-15 16:51:20 -070092 this._account = account;
Kasper Nilssonf4d03fc2017-11-14 15:12:50 -080093 this._username = account.username;
Kasper Nilsson95131342017-05-15 17:13:16 -070094 }));
Wyatt Allen767b7602016-06-15 16:51:20 -070095
Paladox none7e4726c2018-10-07 14:29:45 +000096 promises.push(this.$.restAPI.getAvatarChangeUrl().then(url => {
97 this._avatarChangeUrl = url;
98 }));
99
Kasper Nilsson95131342017-05-15 17:13:16 -0700100 return Promise.all(promises).then(() => {
Wyatt Allen767b7602016-06-15 16:51:20 -0700101 this._loading = false;
Kasper Nilsson95131342017-05-15 17:13:16 -0700102 });
Wyatt Allen767b7602016-06-15 16:51:20 -0700103 },
104
Kasper Nilsson95131342017-05-15 17:13:16 -0700105 save() {
Paladox nonefbe932e2017-04-26 21:36:06 +0000106 if (!this.hasUnsavedChanges) {
Wyatt Allen767b7602016-06-15 16:51:20 -0700107 return Promise.resolve();
108 }
109
110 this._saving = true;
Kasper Nilsson58273122017-03-09 10:55:10 -0800111 // Set only the fields that have changed.
112 // Must be done in sequence to avoid race conditions (@see Issue 5721)
113 return this._maybeSetName()
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -0800114 .then(this._maybeSetUsername.bind(this))
Kasper Nilsson58273122017-03-09 10:55:10 -0800115 .then(this._maybeSetStatus.bind(this))
Kasper Nilsson95131342017-05-15 17:13:16 -0700116 .then(() => {
Kasper Nilsson58273122017-03-09 10:55:10 -0800117 this._hasNameChange = false;
118 this._hasStatusChange = false;
119 this._saving = false;
120 this.fire('account-detail-update');
Kasper Nilsson95131342017-05-15 17:13:16 -0700121 });
Kasper Nilsson58273122017-03-09 10:55:10 -0800122 },
123
Kasper Nilsson95131342017-05-15 17:13:16 -0700124 _maybeSetName() {
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -0800125 return this._hasNameChange && this.nameMutable ?
126 this.$.restAPI.setAccountName(this._account.name) :
127 Promise.resolve();
128 },
129
130 _maybeSetUsername() {
131 return this._hasUsernameChange && this.usernameMutable ?
Kasper Nilssonf4d03fc2017-11-14 15:12:50 -0800132 this.$.restAPI.setAccountUsername(this._username) :
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -0800133 Promise.resolve();
Kasper Nilsson58273122017-03-09 10:55:10 -0800134 },
135
Kasper Nilsson95131342017-05-15 17:13:16 -0700136 _maybeSetStatus() {
Kasper Nilsson58273122017-03-09 10:55:10 -0800137 return this._hasStatusChange ?
138 this.$.restAPI.setAccountStatus(this._account.status) :
139 Promise.resolve();
140 },
141
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -0800142 _computeHasUnsavedChanges(nameChanged, usernameChanged, statusChanged) {
143 return nameChanged || usernameChanged || statusChanged;
Wyatt Allen767b7602016-06-15 16:51:20 -0700144 },
145
Kasper Nilssonf4d03fc2017-11-14 15:12:50 -0800146 _computeUsernameMutable(config, username) {
147 // Username may not be changed once it is set.
148 return config.auth.editable_account_fields.includes('USER_NAME') &&
149 !username;
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -0800150 },
151
152 _computeNameMutable(config) {
Kasper Nilsson95131342017-05-15 17:13:16 -0700153 return config.auth.editable_account_fields.includes('FULL_NAME');
Wyatt Allen767b7602016-06-15 16:51:20 -0700154 },
155
Kasper Nilsson95131342017-05-15 17:13:16 -0700156 _statusChanged() {
Wyatt Allen767b7602016-06-15 16:51:20 -0700157 if (this._loading) { return; }
Kasper Nilsson58273122017-03-09 10:55:10 -0800158 this._hasStatusChange = true;
159 },
160
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -0800161 _usernameChanged() {
Kasper Nilsson5cb34e72017-12-27 14:12:15 -0800162 if (this._loading || !this._account) { return; }
163 this._hasUsernameChange =
164 (this._account.username || '') !== (this._username || '');
Kasper Nilsson8a4c47e2017-11-09 16:08:06 -0800165 },
166
Kasper Nilsson95131342017-05-15 17:13:16 -0700167 _nameChanged() {
Kasper Nilsson58273122017-03-09 10:55:10 -0800168 if (this._loading) { return; }
169 this._hasNameChange = true;
Wyatt Allen767b7602016-06-15 16:51:20 -0700170 },
171
Kasper Nilsson95131342017-05-15 17:13:16 -0700172 _handleKeydown(e) {
Wyatt Allen767b7602016-06-15 16:51:20 -0700173 if (e.keyCode === 13) { // Enter
174 e.stopPropagation();
175 this.save();
176 }
177 },
Paladox none7e4726c2018-10-07 14:29:45 +0000178
179 _hideAvatarChangeUrl(avatarChangeUrl) {
180 if (!avatarChangeUrl) {
181 return 'hide';
182 }
183
184 return '';
185 },
Wyatt Allen767b7602016-06-15 16:51:20 -0700186 });
187})();