88import * as MicrosoftGraph from '@microsoft/microsoft-graph-types' ;
99import { customElement , html , property , query , TemplateResult } from 'lit-element' ;
1010import { classMap } from 'lit-html/directives/class-map' ;
11- import { findPerson , findUserByEmail , getEmailFromGraphEntity } from '../../graph/graph.people' ;
12- import { getContactPhoto , getUserPhoto , myPhoto } from '../../graph/graph.photos' ;
11+ import { findPerson , getEmailFromGraphEntity } from '../../graph/graph.people' ;
12+ import { getPersonImage } from '../../graph/graph.photos' ;
1313import { getUserWithPhoto } from '../../graph/graph.user' ;
14+ import { IDynamicPerson } from '../../graph/types' ;
1415import { Providers } from '../../Providers' ;
1516import { ProviderState } from '../../providers/IProvider' ;
1617import '../../styles/fabric-icon-font' ;
@@ -21,23 +22,6 @@ import { MgtTemplatedComponent } from '../templatedComponent';
2122import { PersonCardInteraction } from './../PersonCardInteraction' ;
2223import { styles } from './mgt-person-css' ;
2324
24- /**
25- * IDynamicPerson describes the person object we use throughout mgt-person,
26- * which can be one of three similar Graph types.
27- *
28- * In addition, this custom type also defines the optional `personImage` property,
29- * which is used to pass the image around to other components as part of the person object.
30- */
31- export type IDynamicPerson = ( MicrosoftGraph . User | MicrosoftGraph . Person | MicrosoftGraph . Contact ) & {
32- /**
33- * personDetails.personImage is a toolkit injected property to pass image between components
34- * an optimization to avoid fetching the image when unnecessary.
35- *
36- * @type {string }
37- */
38- personImage ?: string ;
39- } ;
40-
4125/**
4226 * The person component is used to display a person or contact by using their photo, name, and/or email address.
4327 *
@@ -220,7 +204,7 @@ export class MgtPerson extends MgtTemplatedComponent {
220204 */
221205 public render ( ) {
222206 // Loading
223- if ( this . isLoadingState ) {
207+ if ( this . isLoadingState && ! this . personDetails ) {
224208 return this . renderLoading ( ) ;
225209 }
226210
@@ -463,7 +447,6 @@ export class MgtPerson extends MgtTemplatedComponent {
463447 */
464448 protected async loadState ( ) {
465449 const provider = Providers . globalProvider ;
466-
467450 if ( ! provider || provider . state === ProviderState . Loading ) {
468451 return ;
469452 }
@@ -473,74 +456,37 @@ export class MgtPerson extends MgtTemplatedComponent {
473456 return ;
474457 }
475458
459+ const graph = provider . graph . forComponent ( this ) ;
476460 if ( this . personDetails ) {
477461 // in some cases we might only have name or email, but need to find the image
478462 // use @ for the image value to search for an image
479463 if ( this . personImage === '@' && ! this . personDetails . personImage ) {
480- this . loadImage ( ) ;
464+ const image = await getPersonImage ( graph , this . personDetails ) ;
465+ if ( image ) {
466+ this . personDetails . personImage = image ;
467+ this . personImage = image ;
468+ }
481469 }
482- return ;
483- }
484-
485- // Use userId or 'me' query to get the person and image
486- if ( this . userId || this . personQuery === 'me' ) {
487- const graph = provider . graph . forComponent ( this ) ;
470+ } else if ( this . userId || this . personQuery === 'me' ) {
471+ // Use userId or 'me' query to get the person and image
488472 const person = await getUserWithPhoto ( graph , this . userId ) ;
489473
490474 this . personDetails = person ;
491475 this . personImage = this . getImage ( ) ;
492- return ;
493- }
494-
495- // Use the personQuery to find our person.
496- if ( this . personQuery ) {
497- const graph = provider . graph . forComponent ( this ) ;
476+ } else if ( this . personQuery ) {
477+ // Use the personQuery to find our person.
498478 const people = await findPerson ( graph , this . personQuery ) ;
499479
500480 if ( people && people . length ) {
501481 this . personDetails = people [ 0 ] ;
502- this . loadImage ( ) ;
503- }
504- }
505- }
482+ const image = await getPersonImage ( graph , people [ 0 ] ) ;
506483
507- private async loadImage ( ) {
508- const provider = Providers . globalProvider ;
509- const graph = provider . graph . forComponent ( this ) ;
510- const person = this . personDetails ;
511- let image : string ;
512-
513- if ( ( person as MicrosoftGraph . Person ) . userPrincipalName ) {
514- // try to find a user by userPrincipalName
515- const userPrincipalName = ( person as MicrosoftGraph . Person ) . userPrincipalName ;
516- image = await getUserPhoto ( graph , userPrincipalName ) ;
517- } else {
518- // try to find a user by e-mail
519- const email = getEmailFromGraphEntity ( person ) ;
520- if ( email ) {
521- const users = await findUserByEmail ( graph , email ) ;
522- if ( users && users . length ) {
523- // Check for an OrganizationUser
524- const orgUser = users . find ( p => {
525- return ( p as any ) . personType && ( p as any ) . personType . subclass === 'OrganizationUser' ;
526- } ) ;
527- if ( orgUser ) {
528- // Lookup by userId
529- const userId = ( users [ 0 ] as MicrosoftGraph . Person ) . scoredEmailAddresses [ 0 ] . address ;
530- image = await getUserPhoto ( graph , userId ) ;
531- } else {
532- // Lookup by contactId
533- const contactId = users [ 0 ] . id ;
534- image = await getContactPhoto ( graph , contactId ) ;
535- }
484+ if ( image ) {
485+ this . personDetails . personImage = image ;
486+ this . personImage = image ;
536487 }
537488 }
538489 }
539-
540- if ( image ) {
541- this . personDetails . personImage = image ;
542- this . personImage = image ;
543- }
544490 }
545491
546492 private getImage ( ) : string {
0 commit comments