Skip to content

Commit 0ee358d

Browse files
fixed an issue with user-ids (#348)
Co-authored-by: Shane Weaver <[email protected]>
1 parent 75e6a54 commit 0ee358d

File tree

3 files changed

+101
-59
lines changed

3 files changed

+101
-59
lines changed

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

Lines changed: 55 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import { getUsersForUserIds } from '../../graph/graph.user';
1313
import { Providers } from '../../Providers';
1414
import { ProviderState } from '../../providers/IProvider';
1515
import '../../styles/fabric-icon-font';
16-
import '../mgt-person/mgt-person';
16+
import { arraysAreEqual } from '../../utils/Utils';
17+
import { IDynamicPerson } from '../mgt-person/mgt-person';
1718
import { MgtTemplatedComponent } from '../templatedComponent';
1819
import { PersonCardInteraction } from './../PersonCardInteraction';
1920
import { styles } from './mgt-people-css';
@@ -38,26 +39,6 @@ export class MgtPeople extends MgtTemplatedComponent {
3839
return styles;
3940
}
4041

41-
/**
42-
* containing array of people used in the component.
43-
* @type {Array<MgtPersonDetails>}
44-
*/
45-
@property({
46-
attribute: 'people',
47-
type: Object
48-
})
49-
public people: Array<MicrosoftGraph.User | MicrosoftGraph.Person | MicrosoftGraph.Contact>;
50-
51-
/**
52-
* developer determined max people shown in component
53-
* @type {number}
54-
*/
55-
@property({
56-
attribute: 'show-max',
57-
type: Number
58-
})
59-
public showMax: number;
60-
6142
/**
6243
* determines if agenda events come from specific group
6344
* @type {string}
@@ -66,7 +47,16 @@ export class MgtPeople extends MgtTemplatedComponent {
6647
attribute: 'group-id',
6748
type: String
6849
})
69-
public groupId: string;
50+
public get groupId(): string {
51+
return this._groupId;
52+
}
53+
public set groupId(value) {
54+
if (this._groupId === value) {
55+
return;
56+
}
57+
this._groupId = value;
58+
this.requestStateUpdate(true);
59+
}
7060

7161
/**
7262
* user id array
@@ -76,19 +66,40 @@ export class MgtPeople extends MgtTemplatedComponent {
7666
@property({
7767
attribute: 'user-ids',
7868
converter: (value, type) => {
79-
return value.split(',');
69+
return value.split(',').map(v => v.trim());
8070
}
8171
})
8272
public get userIds(): string[] {
83-
return this.privateUserIds;
73+
return this._userIds;
8474
}
8575
public set userIds(value: string[]) {
86-
const oldValue = this.userIds;
87-
this.privateUserIds = value;
88-
this.updateUserIds(value);
89-
this.requestUpdate('userIds', oldValue);
76+
if (arraysAreEqual(this._userIds, value)) {
77+
return;
78+
}
79+
this._userIds = value;
80+
this.requestStateUpdate(true);
9081
}
9182

83+
/**
84+
* containing array of people used in the component.
85+
* @type {Array<MgtPersonDetails>}
86+
*/
87+
@property({
88+
attribute: 'people',
89+
type: Object
90+
})
91+
public people: IDynamicPerson[];
92+
93+
/**
94+
* developer determined max people shown in component
95+
* @type {number}
96+
*/
97+
@property({
98+
attribute: 'show-max',
99+
type: Number
100+
})
101+
public showMax: number;
102+
92103
/**
93104
* Sets how the person-card is invoked
94105
* Set to PersonCardInteraction.none to not show the card
@@ -109,14 +120,29 @@ export class MgtPeople extends MgtTemplatedComponent {
109120
})
110121
public personCardInteraction: PersonCardInteraction = PersonCardInteraction.hover;
111122

112-
private privateUserIds: string[];
123+
private _groupId: string;
124+
private _userIds: string[];
113125

114126
constructor() {
115127
super();
116128

117129
this.showMax = 3;
118130
}
119131

132+
/**
133+
* Request to reload the state.
134+
* Use reload instead of load to ensure loading events are fired.
135+
*
136+
* @protected
137+
* @memberof MgtBaseComponent
138+
*/
139+
protected requestStateUpdate(force?: boolean) {
140+
if (force) {
141+
this.people = null;
142+
}
143+
return super.requestStateUpdate(force);
144+
}
145+
120146
/**
121147
* Invoked on each update to perform rendering tasks. This method must return
122148
* a lit-html TemplateResult. Setting properties inside this method will *not*
@@ -250,34 +276,4 @@ export class MgtPeople extends MgtTemplatedComponent {
250276
}
251277
}
252278
}
253-
254-
private async updateUserIds(newIds: string[]) {
255-
if (this.isLoadingState) {
256-
return;
257-
}
258-
259-
const newIdsSet = new Set(newIds);
260-
this.people = this.people ? this.people.filter(p => newIdsSet.has(p.id)) : [];
261-
const oldIdsSet = new Set(this.people ? this.people.map(p => p.id) : []);
262-
263-
const newToLoad = [];
264-
265-
for (const id of newIds) {
266-
if (!oldIdsSet.has(id)) {
267-
newToLoad.push(id);
268-
}
269-
}
270-
271-
if (newToLoad && newToLoad.length > 0) {
272-
const provider = Providers.globalProvider;
273-
if (!provider || provider.state !== ProviderState.SignedIn) {
274-
return;
275-
}
276-
277-
const graph = provider.graph.forComponent(this);
278-
279-
const newPeople = await getUsersForUserIds(graph, newToLoad);
280-
this.people = (this.people || []).concat(newPeople);
281-
}
282-
}
283279
}

src/utils/Utils.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,44 @@ function equalsInternal(o1: any, o2: any, refs: Set<object>) {
215215
return o1 === o2;
216216
}
217217

218+
/**
219+
* Compares two arrays if the elements are equals
220+
* Should be used for arrays of primitive types
221+
*
222+
* @export
223+
* @template T the type of the elements in the array (should be primitive)
224+
* @param {T[]} arr1
225+
* @param {T[]} arr2
226+
* @returns true if both arrays contain the same items or if both arrays are null or empty
227+
*/
228+
export function arraysAreEqual<T>(arr1: T[], arr2: T[]) {
229+
if (arr1 === arr2) {
230+
return true;
231+
}
232+
233+
if (!arr1 || !arr2) {
234+
return false;
235+
}
236+
237+
if (arr1.length !== arr2.length) {
238+
return false;
239+
}
240+
241+
if (arr1.length === 0) {
242+
return true;
243+
}
244+
245+
const setArr1 = new Set(arr1);
246+
247+
for (const i of arr2) {
248+
if (!setArr1.has(i)) {
249+
return false;
250+
}
251+
}
252+
253+
return true;
254+
}
255+
218256
/**
219257
* converts a blob to base64 encoding
220258
*

stories/components/people.stories.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@ export default {
2323
export const People = () => html`
2424
<mgt-people show-max="5"></mgt-people>
2525
`;
26+
27+
export const GroupId = () => html`
28+
<mgt-people group-id="02bd9fd6-8f93-4758-87c3-1fb73740a315"></mgt-people>
29+
`;
30+
31+
export const UserIds = () => html`
32+
<mgt-people user-ids="2804bc07-1e1f-4938-9085-ce6d756a32d2 ,e8a02cc7-df4d-4778-956d-784cc9506e5a,c8913c86-ceea-4d39-b1ea-f63a5b675166"></mgt-people>
33+
`;

0 commit comments

Comments
 (0)