blob: 91bc6281c5078e077c392813f3e2b2dd2f811468 [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: {
27 mutable: {
28 type: Boolean,
29 notify: true,
30 computed: '_computeMutable(_serverConfig)',
31 },
32 hasUnsavedChanges: {
33 type: Boolean,
34 notify: true,
Kasper Nilsson58273122017-03-09 10:55:10 -080035 computed: '_computeHasUnsavedChanges(_hasNameChange, _hasStatusChange)',
Wyatt Allen767b7602016-06-15 16:51:20 -070036 },
37
Kasper Nilsson58273122017-03-09 10:55:10 -080038 _hasNameChange: {
39 type: Boolean,
40 value: false,
41 },
42 _hasStatusChange: {
43 type: Boolean,
44 value: false,
45 },
Wyatt Allen767b7602016-06-15 16:51:20 -070046 _loading: {
47 type: Boolean,
48 value: false,
49 },
50 _saving: {
51 type: Boolean,
52 value: false,
53 },
54 _account: Object,
55 _serverConfig: Object,
56 },
57
58 observers: [
Kasper Nilsson58273122017-03-09 10:55:10 -080059 '_nameChanged(_account.name)',
60 '_statusChanged(_account.status)',
Wyatt Allen767b7602016-06-15 16:51:20 -070061 ],
62
63 loadData: function() {
64 var promises = [];
65
66 this._loading = true;
67
68 promises.push(this.$.restAPI.getConfig().then(function(config) {
69 this._serverConfig = config;
70 }.bind(this)));
71
72 promises.push(this.$.restAPI.getAccount().then(function(account) {
73 this._account = account;
74 }.bind(this)));
75
76 return Promise.all(promises).then(function() {
77 this._loading = false;
78 }.bind(this));
79 },
80
81 save: function() {
Paladox nonefbe932e2017-04-26 21:36:06 +000082 if (!this.hasUnsavedChanges) {
Wyatt Allen767b7602016-06-15 16:51:20 -070083 return Promise.resolve();
84 }
85
86 this._saving = true;
Kasper Nilsson58273122017-03-09 10:55:10 -080087 // Set only the fields that have changed.
88 // Must be done in sequence to avoid race conditions (@see Issue 5721)
89 return this._maybeSetName()
90 .then(this._maybeSetStatus.bind(this))
91 .then(function() {
92 this._hasNameChange = false;
93 this._hasStatusChange = false;
94 this._saving = false;
95 this.fire('account-detail-update');
96 }.bind(this));
97 },
98
99 _maybeSetName: function() {
Paladox nonefbe932e2017-04-26 21:36:06 +0000100 return this._hasNameChange && this.mutable ?
101 this.$.restAPI.setAccountName(this._account.name) :
102 Promise.resolve();
Kasper Nilsson58273122017-03-09 10:55:10 -0800103 },
104
105 _maybeSetStatus: function() {
106 return this._hasStatusChange ?
107 this.$.restAPI.setAccountStatus(this._account.status) :
108 Promise.resolve();
109 },
110
111 _computeHasUnsavedChanges: function(name, status) {
112 return name || status;
Wyatt Allen767b7602016-06-15 16:51:20 -0700113 },
114
115 _computeMutable: function(config) {
116 return config.auth.editable_account_fields.indexOf('FULL_NAME') !== -1;
117 },
118
Kasper Nilsson58273122017-03-09 10:55:10 -0800119 _statusChanged: function() {
Wyatt Allen767b7602016-06-15 16:51:20 -0700120 if (this._loading) { return; }
Kasper Nilsson58273122017-03-09 10:55:10 -0800121 this._hasStatusChange = true;
122 },
123
124 _nameChanged: function() {
125 if (this._loading) { return; }
126 this._hasNameChange = true;
Wyatt Allen767b7602016-06-15 16:51:20 -0700127 },
128
Kasper Nilsson85875742017-02-02 12:13:01 -0800129 _handleKeydown: function(e) {
Wyatt Allen767b7602016-06-15 16:51:20 -0700130 if (e.keyCode === 13) { // Enter
131 e.stopPropagation();
132 this.save();
133 }
134 },
135 });
136})();