MOBILE-3947 user: Fix user types on user-avatar

main
Pau Ferrer Ocaña 2023-11-13 12:36:04 +01:00
parent e40a4f13bb
commit 4ebec029ba
7 changed files with 30 additions and 9 deletions

View File

@ -40,7 +40,7 @@
<ion-item [attr.button]="isSiteClickable(isCurrentSite) ? true : null" (click)="siteClicked($event, site, isCurrentSite)"
[attr.detail]="isSiteClickable(isCurrentSite) ? 'true' : 'false'" [class.item-current]="isCurrentSite">
<core-user-avatar [user]="site" slot="start" [linkProfile]="false" [siteId]="site.id"></core-user-avatar>
<core-user-avatar [site]="site" slot="start" [linkProfile]="false" [siteId]="site.id"></core-user-avatar>
<ion-label>
<p class="item-heading">{{site.fullname}}</p>

View File

@ -14,7 +14,7 @@
import { Component, Input, OnInit, OnChanges, OnDestroy, SimpleChange } from '@angular/core';
import { CoreSites } from '@services/sites';
import { CoreSiteBasicInfo, CoreSites } from '@services/sites';
import { CoreUtils } from '@services/utils/utils';
import { CoreEventObserver, CoreEvents } from '@singletons/events';
import { USER_PROFILE_PICTURE_UPDATED, CoreUserBasicData } from '@features/user/services/user';
@ -22,6 +22,7 @@ import { CoreNavigator } from '@services/navigator';
import { CoreNetwork } from '@services/network';
import { CoreUserHelper } from '@features/user/services/user-helper';
import { CoreUrlUtils } from '@services/utils/url';
import { CoreSiteInfo } from '@classes/site';
/**
* Component to display a "user avatar".
@ -35,7 +36,8 @@ import { CoreUrlUtils } from '@services/utils/url';
})
export class CoreUserAvatarComponent implements OnInit, OnChanges, OnDestroy {
@Input() user?: CoreUserWithAvatar;
@Input() user?: CoreUserWithAvatar; // @todo Fix the accepted type and restrict it a bit.
@Input() site?: CoreSiteBasicInfo | CoreSiteInfo; // Site info contains user info.
// The following params will override the ones in user object.
@Input() profileUrl?: string;
@Input() linkProfile = true; // Avoid linking to the profile if wanted.
@ -71,8 +73,23 @@ export class CoreUserAvatarComponent implements OnInit, OnChanges, OnDestroy {
/**
* @inheritdoc
*/
ngOnInit(): void {
this.siteId = this.siteId || CoreSites.getCurrentSiteId();
async ngOnInit(): Promise<void> {
this.siteId = this.siteId ?? (this.site && 'id' in this.site
? this.site.id
: CoreSites.getCurrentSiteId());
if (this.site && !this.user) {
this.user = {
id: ('userid' in this.site
? this.site.userid
: this.site.userId)
?? (await CoreSites.getSite(this.siteId)).getUserId(),
fullname: this.site.fullname ?? '',
firstname: this.site.firstname ?? '',
lastname: this.site.lastname ?? '',
userpictureurl: this.site.userpictureurl,
};
}
this.setFields();
}
@ -189,4 +206,6 @@ export type CoreUserWithAvatar = CoreUserBasicData & {
isonline?: boolean;
courseid?: number;
lastaccess?: number;
firstname?: string; // The first name(s) of the user.
lastname?: string; // The family name of the user.
};

View File

@ -20,7 +20,7 @@
</ion-label>
</ion-item>
<ion-item *ngFor="let site of sites" (click)="siteClicked(site.id)" [detail]="false" button>
<core-user-avatar [user]="site" slot="start" [linkProfile]="false"></core-user-avatar>
<core-user-avatar [site]="site" slot="start" [linkProfile]="false"></core-user-avatar>
<ion-label>
<p class="item-heading">{{site.fullname}}</p>

View File

@ -45,7 +45,7 @@
<div class="core-login-user">
<!-- Show user avatar. -->
<core-user-avatar class="large-avatar" *ngIf="showUserAvatar" [user]="siteInfo" [linkProfile]="false"
<core-user-avatar class="large-avatar" *ngIf="showUserAvatar" [site]="siteInfo" [linkProfile]="false"
[siteId]="siteId"></core-user-avatar>
<p *ngIf="siteInfo?.fullname" class="core-login-fullname">
<core-format-text [text]="siteInfo?.fullname" [filter]="false"></core-format-text>

View File

@ -1,4 +1,4 @@
<core-user-avatar *ngIf="(alwaysShow || isMainScreen) && siteInfo" [user]="siteInfo" class="core-bar-button-image clickable"
<core-user-avatar *ngIf="(alwaysShow || isMainScreen) && siteInfo" [site]="siteInfo" class="core-bar-button-image clickable"
[linkProfile]="false" (ariaButtonClick)="openUserMenu($event)" [userTour]="userTour"
[attr.aria-label]="'core.user.useraccount' | translate">
</core-user-avatar>

View File

@ -44,7 +44,7 @@
<ion-item button class="core-usermenu-handler ion-text-wrap" *ngIf="siteInfo" lines="full" (click)="openUserProfile($event)"
[detail]="true" [attr.aria-label]="'core.user.profile' | translate">
<core-user-avatar [user]="siteInfo" [userId]="siteInfo.userid" [linkProfile]="false" slot="start"></core-user-avatar>
<core-user-avatar [site]="siteInfo" [userId]="siteInfo.userid" [linkProfile]="false" slot="start"></core-user-avatar>
<ion-label>
<p class="item-heading">{{ siteInfo.fullname }}</p>
</ion-label>

View File

@ -1253,6 +1253,7 @@ export class CoreSitesProvider {
const basicInfo: CoreSiteBasicInfo = {
id: site.id,
userId: siteInfo?.userid,
siteUrl: site.siteUrl,
siteUrlWithoutProtocol: site.siteUrl.replace(/^https?:\/\//, '').toLowerCase(),
fullname: siteInfo?.fullname,
@ -2148,6 +2149,7 @@ export type CoreSiteUserTokenResponse = {
*/
export type CoreSiteBasicInfo = {
id: string; // Site ID.
userId?: number; // User ID.
siteUrl: string; // Site URL.
siteUrlWithoutProtocol: string; // Site URL without protocol.
fullname?: string; // User's full name.