Skip to content

Commit 2396bb5

Browse files
authored
Added fetchImage and fixed image fetching in mgt-person (#418)
* Fetch image if only id available * added fetchImage * clean up
1 parent 68a3e45 commit 2396bb5

File tree

5 files changed

+55
-13
lines changed

5 files changed

+55
-13
lines changed

src/components/mgt-people-picker/mgt-people-picker.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { Providers } from '../../Providers';
1717
import { ProviderState } from '../../providers/IProvider';
1818
import '../../styles/fabric-icon-font';
1919
import { debounce } from '../../utils/Utils';
20+
import { PersonCardInteraction } from '../PersonCardInteraction';
2021
import { MgtFlyout } from '../sub-components/mgt-flyout/mgt-flyout';
2122
import { MgtTemplatedComponent } from '../templatedComponent';
2223
import { styles } from './mgt-people-picker-css';
@@ -517,7 +518,7 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
517518
return (
518519
this.renderTemplate('person', { person }, person.id) ||
519520
html`
520-
<mgt-person .personDetails=${person} .personImage=${'@'}></mgt-person>
521+
<mgt-person .personDetails=${person} .fetchImage=${true}></mgt-person>
521522
<div class="people-person-text-area" id="${person.displayName}">
522523
${this.renderHighlightText(person)}
523524
<span class="${classMap(classes)}">${subTitle}</span>
@@ -539,9 +540,9 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
539540
<mgt-person
540541
class="selected-person"
541542
.personDetails=${person}
542-
.personImage=${'@'}
543-
show-name
544-
person-card="click"
543+
.fetchImage=${true}
544+
.showName=${true}
545+
.personCardInteraction=${PersonCardInteraction.click}
545546
></mgt-person>
546547
`;
547548
}

src/components/mgt-people/mgt-people.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export class MgtPeople extends MgtTemplatedComponent {
281281
html`
282282
<mgt-person
283283
.personDetails=${person}
284-
.personImage=${'@'}
284+
.fetchImage=${true}
285285
.avatarSize=${avatarSize}
286286
.personCardInteraction=${this.personCardInteraction}
287287
.showPresence=${this.showPresence}

src/components/mgt-person-card/mgt-person-card.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ export class MgtPersonCard extends MgtTemplatedComponent {
8282

8383
/**
8484
* Set the image of the person
85-
* Set to '@' to look up image from the graph
8685
*
8786
* @type {string}
8887
* @memberof MgtPersonCard
@@ -93,6 +92,20 @@ export class MgtPersonCard extends MgtTemplatedComponent {
9392
})
9493
public personImage: string;
9594

95+
/**
96+
* Sets whether the person image should be fetched
97+
* from the Microsoft Graph based on the personDetails
98+
* provided by the user
99+
*
100+
* @type {boolean}
101+
* @memberof MgtPerson
102+
*/
103+
@property({
104+
attribute: 'fetch-image',
105+
type: Boolean
106+
})
107+
public fetchImage: boolean;
108+
96109
/**
97110
* Gets or sets whether expanded details section is rendered
98111
*
@@ -575,9 +588,12 @@ export class MgtPersonCard extends MgtTemplatedComponent {
575588
const person = await getUserWithPhoto(graph, id);
576589
this.personDetails = person;
577590
this.personImage = this.getImage();
578-
} else if (this.personImage === '@' && !this.personDetails.personImage) {
591+
this.personDetails.personImage = this.personImage;
592+
} else if (
593+
!this.personDetails.personImage &&
594+
((this.fetchImage && !this.personImage) || this.personImage === '@')
595+
) {
579596
// in some cases we might only have name or email, but need to find the image
580-
// use @ for the image value to search for an image
581597
const image = await getPersonImage(graph, this.personDetails);
582598
if (image) {
583599
this.personDetails.personImage = image;

src/components/mgt-person/mgt-person.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,41 @@ export class MgtPerson extends MgtTemplatedComponent {
138138
} else {
139139
this._personAvatarBg = this.getColorFromName(value.displayName);
140140
}
141+
142+
if (this.fetchImage) {
143+
this.personImage = null;
144+
}
145+
146+
this.requestStateUpdate();
141147
this.requestUpdate('personDetails');
142148
}
143149

144150
/**
145151
* Set the image of the person
146-
* Set to '@' to look up image from the graph
147152
*
148153
* @type {string}
149154
* @memberof MgtPersonCard
150155
*/
151156
@property({
152157
attribute: 'person-image',
153-
reflect: true,
154158
type: String
155159
})
156160
public personImage: string;
157161

162+
/**
163+
* Sets whether the person image should be fetched
164+
* from the Microsoft Graph based on the personDetails
165+
* provided by the user
166+
*
167+
* @type {boolean}
168+
* @memberof MgtPerson
169+
*/
170+
@property({
171+
attribute: 'fetch-image',
172+
type: Boolean
173+
})
174+
public fetchImage: boolean;
175+
158176
/**
159177
* Gets or sets presence of person
160178
*
@@ -666,9 +684,7 @@ export class MgtPerson extends MgtTemplatedComponent {
666684
const graph = provider.graph.forComponent(this);
667685

668686
if (this.personDetails) {
669-
// in some cases we might only have name or email, but need to find the image
670-
// use @ for the image value to search for an image
671-
if (this.personImage === '@' && !this.personDetails.personImage) {
687+
if (!this.personDetails.personImage && ((this.fetchImage && !this.personImage) || this.personImage === '@')) {
672688
const image = await getPersonImage(graph, this.personDetails);
673689
if (image) {
674690
this.personDetails.personImage = image;
@@ -681,6 +697,7 @@ export class MgtPerson extends MgtTemplatedComponent {
681697

682698
this.personDetails = person;
683699
this.personImage = this.getImage();
700+
this.personDetails.personImage = this.personImage;
684701
} else if (this.personQuery) {
685702
// Use the personQuery to find our person.
686703
const people = await findPeople(graph, this.personQuery, 1);

src/graph/graph.photos.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,16 @@ export async function getPersonImage(graph: IGraph, person: IDynamicPerson) {
7575
const userPrincipalName = (person as MicrosoftGraph.Person).userPrincipalName;
7676
image = await getUserPhoto(graph, userPrincipalName);
7777
} else {
78+
if (person.id) {
79+
image = await getUserPhoto(graph, person.id);
80+
if (image) {
81+
return image;
82+
}
83+
}
84+
7885
// try to find a user by e-mail
7986
const email = getEmailFromGraphEntity(person);
87+
8088
if (email) {
8189
const users = await findUserByEmail(graph, email);
8290
if (users && users.length) {

0 commit comments

Comments
 (0)