blob: 6bb1bf8add80c2d230002d55cd80da4d5343d99b [file] [log] [blame]
Dave Borowitz8cdc76b2018-03-26 10:04:27 -04001/**
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 Filippovdaf0ec92020-03-17 11:27:28 +010017import '../../../scripts/bundled-polymer.js';
Wyatt Allen5c3a3cd2017-08-30 13:43:37 -070018
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010019import '../../../styles/shared-styles.js';
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010020import '../../plugins/gr-endpoint-decorator/gr-endpoint-decorator.js';
21import '../../plugins/gr-endpoint-param/gr-endpoint-param.js';
22import '../../shared/gr-avatar/gr-avatar.js';
23import '../../shared/gr-date-formatter/gr-date-formatter.js';
24import '../../shared/gr-rest-api-interface/gr-rest-api-interface.js';
25import '../../../styles/dashboard-header-styles.js';
26import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
27import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js';
28import {PolymerElement} from '@polymer/polymer/polymer-element.js';
29import {htmlTemplate} from './gr-user-header_html.js';
Dmitrii Filippoveb8b2692020-04-06 18:02:35 +020030import {GerritNav} from '../../core/gr-navigation/gr-navigation.js';
Paladox none44c55c52019-10-14 12:43:51 +000031
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010032/**
33 * @extends Polymer.Element
34 */
35class 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 Zhou1ad8f242020-01-09 19:26:23 +010068 /** @type {?string} */
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010069 _status: {
70 type: String,
71 value: null,
72 },
73 };
Dmitrii Filippov3fd2b102019-11-15 16:16:46 +010074 }
75
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010076 _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 Filippoveb8b2692020-04-06 18:02:35 +0200108 return GerritNav.getUrlForUserDashboard(id ? id : email);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100109 }
110
111 _computeDashboardLinkClass(showDashboardLink, loggedIn) {
112 return showDashboardLink && loggedIn ?
113 'dashboardLink' : 'dashboardLink hide';
114 }
115}
116
117customElements.define(GrUserHeader.is, GrUserHeader);