Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 1 | // 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 Hanks | 25f49af | 2016-10-10 17:26:43 -0700 | [diff] [blame] | 20 | /** |
| 21 | * Fired when account details are changed. |
| 22 | * |
| 23 | * @event account-detail-update |
| 24 | */ |
| 25 | |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 26 | properties: { |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 27 | usernameMutable: { |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 28 | type: Boolean, |
| 29 | notify: true, |
Kasper Nilsson | f4d03fc | 2017-11-14 15:12:50 -0800 | [diff] [blame] | 30 | computed: '_computeUsernameMutable(_serverConfig, _account.username)', |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 31 | }, |
| 32 | nameMutable: { |
| 33 | type: Boolean, |
| 34 | notify: true, |
| 35 | computed: '_computeNameMutable(_serverConfig)', |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 36 | }, |
| 37 | hasUnsavedChanges: { |
| 38 | type: Boolean, |
| 39 | notify: true, |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 40 | computed: '_computeHasUnsavedChanges(_hasNameChange, ' + |
| 41 | '_hasUsernameChange, _hasStatusChange)', |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 42 | }, |
| 43 | |
Kasper Nilsson | 5cb34e7 | 2017-12-27 14:12:15 -0800 | [diff] [blame^] | 44 | _hasNameChange: Boolean, |
| 45 | _hasUsernameChange: Boolean, |
| 46 | _hasStatusChange: Boolean, |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 47 | _loading: { |
| 48 | type: Boolean, |
| 49 | value: false, |
| 50 | }, |
| 51 | _saving: { |
| 52 | type: Boolean, |
| 53 | value: false, |
| 54 | }, |
Becky Siegel | 8d92d53 | 2017-08-11 16:32:47 -0700 | [diff] [blame] | 55 | /** @type {?} */ |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 56 | _account: Object, |
| 57 | _serverConfig: Object, |
Kasper Nilsson | f4d03fc | 2017-11-14 15:12:50 -0800 | [diff] [blame] | 58 | _username: { |
| 59 | type: String, |
| 60 | observer: '_usernameChanged', |
| 61 | }, |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 62 | }, |
| 63 | |
| 64 | observers: [ |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 65 | '_nameChanged(_account.name)', |
| 66 | '_statusChanged(_account.status)', |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 67 | ], |
| 68 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 69 | loadData() { |
| 70 | const promises = []; |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 71 | |
| 72 | this._loading = true; |
| 73 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 74 | promises.push(this.$.restAPI.getConfig().then(config => { |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 75 | this._serverConfig = config; |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 76 | })); |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 77 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 78 | promises.push(this.$.restAPI.getAccount().then(account => { |
Kasper Nilsson | 5cb34e7 | 2017-12-27 14:12:15 -0800 | [diff] [blame^] | 79 | this._hasNameChange = false; |
| 80 | this._hasUsernameChange = false; |
| 81 | this._hasStatusChange = false; |
Kasper Nilsson | f4d03fc | 2017-11-14 15:12:50 -0800 | [diff] [blame] | 82 | // Provide predefined value for username to trigger computation of |
| 83 | // username mutability. |
| 84 | account.username = account.username || ''; |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 85 | this._account = account; |
Kasper Nilsson | f4d03fc | 2017-11-14 15:12:50 -0800 | [diff] [blame] | 86 | this._username = account.username; |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 87 | })); |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 88 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 89 | return Promise.all(promises).then(() => { |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 90 | this._loading = false; |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 91 | }); |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 92 | }, |
| 93 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 94 | save() { |
Paladox none | fbe932e | 2017-04-26 21:36:06 +0000 | [diff] [blame] | 95 | if (!this.hasUnsavedChanges) { |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 96 | return Promise.resolve(); |
| 97 | } |
| 98 | |
| 99 | this._saving = true; |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 100 | // 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 Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 103 | .then(this._maybeSetUsername.bind(this)) |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 104 | .then(this._maybeSetStatus.bind(this)) |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 105 | .then(() => { |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 106 | this._hasNameChange = false; |
| 107 | this._hasStatusChange = false; |
| 108 | this._saving = false; |
| 109 | this.fire('account-detail-update'); |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 110 | }); |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 111 | }, |
| 112 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 113 | _maybeSetName() { |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 114 | 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 Nilsson | f4d03fc | 2017-11-14 15:12:50 -0800 | [diff] [blame] | 121 | this.$.restAPI.setAccountUsername(this._username) : |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 122 | Promise.resolve(); |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 123 | }, |
| 124 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 125 | _maybeSetStatus() { |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 126 | return this._hasStatusChange ? |
| 127 | this.$.restAPI.setAccountStatus(this._account.status) : |
| 128 | Promise.resolve(); |
| 129 | }, |
| 130 | |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 131 | _computeHasUnsavedChanges(nameChanged, usernameChanged, statusChanged) { |
| 132 | return nameChanged || usernameChanged || statusChanged; |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 133 | }, |
| 134 | |
Kasper Nilsson | f4d03fc | 2017-11-14 15:12:50 -0800 | [diff] [blame] | 135 | _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 Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 139 | }, |
| 140 | |
| 141 | _computeNameMutable(config) { |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 142 | return config.auth.editable_account_fields.includes('FULL_NAME'); |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 143 | }, |
| 144 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 145 | _statusChanged() { |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 146 | if (this._loading) { return; } |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 147 | this._hasStatusChange = true; |
| 148 | }, |
| 149 | |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 150 | _usernameChanged() { |
Kasper Nilsson | 5cb34e7 | 2017-12-27 14:12:15 -0800 | [diff] [blame^] | 151 | if (this._loading || !this._account) { return; } |
| 152 | this._hasUsernameChange = |
| 153 | (this._account.username || '') !== (this._username || ''); |
Kasper Nilsson | 8a4c47e | 2017-11-09 16:08:06 -0800 | [diff] [blame] | 154 | }, |
| 155 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 156 | _nameChanged() { |
Kasper Nilsson | 5827312 | 2017-03-09 10:55:10 -0800 | [diff] [blame] | 157 | if (this._loading) { return; } |
| 158 | this._hasNameChange = true; |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 159 | }, |
| 160 | |
Kasper Nilsson | 9513134 | 2017-05-15 17:13:16 -0700 | [diff] [blame] | 161 | _handleKeydown(e) { |
Wyatt Allen | 767b760 | 2016-06-15 16:51:20 -0700 | [diff] [blame] | 162 | if (e.keyCode === 13) { // Enter |
| 163 | e.stopPropagation(); |
| 164 | this.save(); |
| 165 | } |
| 166 | }, |
| 167 | }); |
| 168 | })(); |