Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright (C) 2017 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 | 5c3a3cd | 2017-08-30 13:43:37 -0700 | [diff] [blame] | 18 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 19 | import '../../../styles/shared-styles.js'; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 20 | import '../../plugins/gr-endpoint-decorator/gr-endpoint-decorator.js'; |
| 21 | import '../../plugins/gr-endpoint-param/gr-endpoint-param.js'; |
| 22 | import '../../shared/gr-avatar/gr-avatar.js'; |
| 23 | import '../../shared/gr-date-formatter/gr-date-formatter.js'; |
| 24 | import '../../shared/gr-rest-api-interface/gr-rest-api-interface.js'; |
| 25 | import '../../../styles/dashboard-header-styles.js'; |
| 26 | import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js'; |
| 27 | import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js'; |
| 28 | import {PolymerElement} from '@polymer/polymer/polymer-element.js'; |
| 29 | import {htmlTemplate} from './gr-user-header_html.js'; |
Dmitrii Filippov | eb8b269 | 2020-04-06 18:02:35 +0200 | [diff] [blame] | 30 | import {GerritNav} from '../../core/gr-navigation/gr-navigation.js'; |
Paladox none | 44c55c5 | 2019-10-14 12:43:51 +0000 | [diff] [blame] | 31 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 32 | /** |
| 33 | * @extends Polymer.Element |
| 34 | */ |
| 35 | class GrUserHeader extends GestureEventListeners( |
| 36 | LegacyElementMixin( |
| 37 | PolymerElement)) { |
| 38 | static get template() { return htmlTemplate; } |
| 39 | |
| 40 | static get is() { return 'gr-user-header'; } |
| 41 | |
| 42 | static get properties() { |
| 43 | return { |
| 44 | /** @type {?string} */ |
| 45 | userId: { |
| 46 | type: String, |
| 47 | observer: '_accountChanged', |
| 48 | }, |
| 49 | |
| 50 | showDashboardLink: { |
| 51 | type: Boolean, |
| 52 | value: false, |
| 53 | }, |
| 54 | |
| 55 | loggedIn: { |
| 56 | type: Boolean, |
| 57 | value: false, |
| 58 | }, |
| 59 | |
| 60 | /** |
| 61 | * @type {?{name: ?, email: ?, registered_on: ?}} |
| 62 | */ |
| 63 | _accountDetails: { |
| 64 | type: Object, |
| 65 | value: null, |
| 66 | }, |
| 67 | |
Tao Zhou | 1ad8f24 | 2020-01-09 19:26:23 +0100 | [diff] [blame] | 68 | /** @type {?string} */ |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 69 | _status: { |
| 70 | type: String, |
| 71 | value: null, |
| 72 | }, |
| 73 | }; |
Dmitrii Filippov | 3fd2b10 | 2019-11-15 16:16:46 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 76 | _accountChanged(userId) { |
| 77 | if (!userId) { |
| 78 | this._accountDetails = null; |
| 79 | this._status = null; |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | this.$.restAPI.getAccountDetails(userId).then(details => { |
| 84 | this._accountDetails = details; |
| 85 | }); |
| 86 | this.$.restAPI.getAccountStatus(userId).then(status => { |
| 87 | this._status = status; |
| 88 | }); |
| 89 | } |
| 90 | |
| 91 | _computeDisplayClass(status) { |
| 92 | return status ? ' ' : 'hide'; |
| 93 | } |
| 94 | |
| 95 | _computeDetail(accountDetails, name) { |
| 96 | return accountDetails ? accountDetails[name] : ''; |
| 97 | } |
| 98 | |
| 99 | _computeStatusClass(accountDetails) { |
| 100 | return this._computeDetail(accountDetails, 'status') ? '' : 'hide'; |
| 101 | } |
| 102 | |
| 103 | _computeDashboardUrl(accountDetails) { |
| 104 | if (!accountDetails) { return null; } |
| 105 | const id = accountDetails._account_id; |
| 106 | const email = accountDetails.email; |
| 107 | if (!id && !email ) { return null; } |
Dmitrii Filippov | eb8b269 | 2020-04-06 18:02:35 +0200 | [diff] [blame] | 108 | return GerritNav.getUrlForUserDashboard(id ? id : email); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | _computeDashboardLinkClass(showDashboardLink, loggedIn) { |
| 112 | return showDashboardLink && loggedIn ? |
| 113 | 'dashboardLink' : 'dashboardLink hide'; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | customElements.define(GrUserHeader.is, GrUserHeader); |