Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 1 | /** |
| 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 | */ |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 17 | import '../../../scripts/bundled-polymer.js'; |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 18 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 19 | import '@polymer/iron-input/iron-input.js'; |
| 20 | import '../../../behaviors/fire-behavior/fire-behavior.js'; |
| 21 | import '../../shared/gr-avatar/gr-avatar.js'; |
| 22 | import '../../shared/gr-date-formatter/gr-date-formatter.js'; |
| 23 | import '../../shared/gr-rest-api-interface/gr-rest-api-interface.js'; |
| 24 | import '../../../styles/gr-form-styles.js'; |
| 25 | import '../../../styles/shared-styles.js'; |
| 26 | import {mixinBehaviors} from '@polymer/polymer/lib/legacy/class.js'; |
| 27 | import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js'; |
| 28 | import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js'; |
| 29 | import {PolymerElement} from '@polymer/polymer/polymer-element.js'; |
| 30 | import {htmlTemplate} from './gr-account-info_html.js'; |
| 31 | |
| 32 | /** |
| 33 | * @appliesMixin Gerrit.FireMixin |
| 34 | * @extends Polymer.Element |
| 35 | */ |
| 36 | class GrAccountInfo extends mixinBehaviors( [ |
| 37 | Gerrit.FireBehavior, |
| 38 | ], GestureEventListeners( |
| 39 | LegacyElementMixin( |
| 40 | PolymerElement))) { |
| 41 | static get template() { return htmlTemplate; } |
| 42 | |
| 43 | static get is() { return 'gr-account-info'; } |
Dmitrii Filippov | 3fd2b10 | 2019-11-15 16:16:46 +0100 | [diff] [blame] | 44 | /** |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 45 | * Fired when account details are changed. |
| 46 | * |
| 47 | * @event account-detail-update |
Tao Zhou | 9a07681 | 2019-12-17 09:59:28 +0100 | [diff] [blame] | 48 | */ |
Logan Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 49 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 50 | static get properties() { |
| 51 | return { |
| 52 | usernameMutable: { |
| 53 | type: Boolean, |
| 54 | notify: true, |
| 55 | computed: '_computeUsernameMutable(_serverConfig, _account.username)', |
| 56 | }, |
| 57 | nameMutable: { |
| 58 | type: Boolean, |
| 59 | notify: true, |
| 60 | computed: '_computeNameMutable(_serverConfig)', |
| 61 | }, |
| 62 | hasUnsavedChanges: { |
| 63 | type: Boolean, |
| 64 | notify: true, |
| 65 | computed: '_computeHasUnsavedChanges(_hasNameChange, ' + |
| 66 | '_hasUsernameChange, _hasStatusChange)', |
| 67 | }, |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 68 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 69 | _hasNameChange: Boolean, |
| 70 | _hasUsernameChange: Boolean, |
| 71 | _hasStatusChange: Boolean, |
| 72 | _loading: { |
| 73 | type: Boolean, |
| 74 | value: false, |
| 75 | }, |
| 76 | _saving: { |
| 77 | type: Boolean, |
| 78 | value: false, |
| 79 | }, |
| 80 | /** @type {?} */ |
| 81 | _account: Object, |
| 82 | _serverConfig: Object, |
| 83 | _username: { |
| 84 | type: String, |
| 85 | observer: '_usernameChanged', |
| 86 | }, |
| 87 | _avatarChangeUrl: { |
| 88 | type: String, |
| 89 | value: '', |
| 90 | }, |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | static get observers() { |
| 95 | return [ |
| 96 | '_nameChanged(_account.name)', |
| 97 | '_statusChanged(_account.status)', |
| 98 | ]; |
| 99 | } |
| 100 | |
| 101 | loadData() { |
| 102 | const promises = []; |
| 103 | |
| 104 | this._loading = true; |
| 105 | |
| 106 | promises.push(this.$.restAPI.getConfig().then(config => { |
| 107 | this._serverConfig = config; |
| 108 | })); |
| 109 | |
| 110 | promises.push(this.$.restAPI.getAccount().then(account => { |
| 111 | this._hasNameChange = false; |
| 112 | this._hasUsernameChange = false; |
| 113 | this._hasStatusChange = false; |
| 114 | // Provide predefined value for username to trigger computation of |
| 115 | // username mutability. |
| 116 | account.username = account.username || ''; |
| 117 | this._account = account; |
| 118 | this._username = account.username; |
| 119 | })); |
| 120 | |
| 121 | promises.push(this.$.restAPI.getAvatarChangeUrl().then(url => { |
| 122 | this._avatarChangeUrl = url; |
| 123 | })); |
| 124 | |
| 125 | return Promise.all(promises).then(() => { |
| 126 | this._loading = false; |
| 127 | }); |
| 128 | } |
| 129 | |
| 130 | save() { |
| 131 | if (!this.hasUnsavedChanges) { |
| 132 | return Promise.resolve(); |
Dmitrii Filippov | 3fd2b10 | 2019-11-15 16:16:46 +0100 | [diff] [blame] | 133 | } |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 134 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 135 | this._saving = true; |
| 136 | // Set only the fields that have changed. |
| 137 | // Must be done in sequence to avoid race conditions (@see Issue 5721) |
| 138 | return this._maybeSetName() |
| 139 | .then(this._maybeSetUsername.bind(this)) |
| 140 | .then(this._maybeSetStatus.bind(this)) |
| 141 | .then(() => { |
| 142 | this._hasNameChange = false; |
| 143 | this._hasStatusChange = false; |
| 144 | this._saving = false; |
| 145 | this.fire('account-detail-update'); |
| 146 | }); |
| 147 | } |
| 148 | |
| 149 | _maybeSetName() { |
| 150 | return this._hasNameChange && this.nameMutable ? |
| 151 | this.$.restAPI.setAccountName(this._account.name) : |
| 152 | Promise.resolve(); |
| 153 | } |
| 154 | |
| 155 | _maybeSetUsername() { |
| 156 | return this._hasUsernameChange && this.usernameMutable ? |
| 157 | this.$.restAPI.setAccountUsername(this._username) : |
| 158 | Promise.resolve(); |
| 159 | } |
| 160 | |
| 161 | _maybeSetStatus() { |
| 162 | return this._hasStatusChange ? |
| 163 | this.$.restAPI.setAccountStatus(this._account.status) : |
| 164 | Promise.resolve(); |
| 165 | } |
| 166 | |
| 167 | _computeHasUnsavedChanges(nameChanged, usernameChanged, statusChanged) { |
| 168 | return nameChanged || usernameChanged || statusChanged; |
| 169 | } |
| 170 | |
| 171 | _computeUsernameMutable(config, username) { |
| 172 | // Polymer 2: check for undefined |
| 173 | if ([ |
| 174 | config, |
| 175 | username, |
| 176 | ].some(arg => arg === undefined)) { |
| 177 | return undefined; |
Dmitrii Filippov | 3fd2b10 | 2019-11-15 16:16:46 +0100 | [diff] [blame] | 178 | } |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 179 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 180 | // Username may not be changed once it is set. |
| 181 | return config.auth.editable_account_fields.includes('USER_NAME') && |
| 182 | !username; |
| 183 | } |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 184 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 185 | _computeNameMutable(config) { |
| 186 | return config.auth.editable_account_fields.includes('FULL_NAME'); |
| 187 | } |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 188 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 189 | _statusChanged() { |
| 190 | if (this._loading) { return; } |
| 191 | this._hasStatusChange = true; |
| 192 | } |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 193 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 194 | _usernameChanged() { |
| 195 | if (this._loading || !this._account) { return; } |
| 196 | this._hasUsernameChange = |
| 197 | (this._account.username || '') !== (this._username || ''); |
| 198 | } |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 199 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 200 | _nameChanged() { |
| 201 | if (this._loading) { return; } |
| 202 | this._hasNameChange = true; |
| 203 | } |
Paladox none | 7e4726c | 2018-10-07 14:29:45 +0000 | [diff] [blame] | 204 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 205 | _handleKeydown(e) { |
| 206 | if (e.keyCode === 13) { // Enter |
| 207 | e.stopPropagation(); |
| 208 | this.save(); |
Dmitrii Filippov | 3fd2b10 | 2019-11-15 16:16:46 +0100 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 212 | _hideAvatarChangeUrl(avatarChangeUrl) { |
| 213 | if (!avatarChangeUrl) { |
| 214 | return 'hide'; |
| 215 | } |
| 216 | |
| 217 | return ''; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | customElements.define(GrAccountInfo.is, GrAccountInfo); |