Skip to content

Commit b1df7a7

Browse files
authored
Merge pull request #477 from microsoftgraph/nivogt/people-picker-group
People picker default search state for Groups and groupId
2 parents c523bfc + 8efb608 commit b1df7a7

File tree

4 files changed

+62
-48
lines changed

4 files changed

+62
-48
lines changed

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,13 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
108108
@property({
109109
attribute: 'type',
110110
converter: (value, type) => {
111+
value = value.toLowerCase();
111112
if (!value || value.length === 0) {
112-
return PersonType.Any;
113+
return PersonType.any;
113114
}
114115

115116
if (typeof PersonType[value] === 'undefined') {
116-
return PersonType.Any;
117+
return PersonType.any;
117118
} else {
118119
return PersonType[value];
119120
}
@@ -140,7 +141,7 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
140141
attribute: 'group-type',
141142
converter: (value, type) => {
142143
if (!value || value.length === 0) {
143-
return GroupType.Any;
144+
return GroupType.any;
144145
}
145146

146147
const values = value.split(',');
@@ -154,7 +155,7 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
154155
}
155156

156157
if (groupTypes.length === 0) {
157-
return GroupType.Any;
158+
return GroupType.any;
158159
}
159160

160161
// tslint:disable-next-line:no-bitwise
@@ -255,8 +256,8 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
255256
@property({ attribute: false }) private _showLoading: boolean;
256257

257258
private _groupId: string;
258-
private _type: PersonType = PersonType.Person;
259-
private _groupType: GroupType = GroupType.Any;
259+
private _type: PersonType = PersonType.person;
260+
private _groupType: GroupType = GroupType.any;
260261

261262
private defaultPeople: IDynamicPerson[];
262263

@@ -644,12 +645,25 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
644645
if (!people && provider && provider.state === ProviderState.SignedIn) {
645646
const graph = provider.graph.forComponent(this);
646647

647-
// default common people
648648
if (!input.length && this._isFocused) {
649649
if (this.defaultPeople) {
650650
people = this.defaultPeople;
651651
} else {
652-
people = await getPeople(graph);
652+
if (this.groupId) {
653+
if (this._groupPeople === null) {
654+
try {
655+
this._groupPeople = await getPeopleFromGroup(graph, this.groupId);
656+
} catch (_) {
657+
this._groupPeople = [];
658+
}
659+
}
660+
people = this._groupPeople || [];
661+
} else if (this.type === PersonType.person || this.type === PersonType.any) {
662+
people = await getPeople(graph);
663+
} else if (this.type === PersonType.group) {
664+
const groups = (await findGroups(graph, '', this.showMax, this.groupType)) || [];
665+
people = groups;
666+
}
653667
this.defaultPeople = people;
654668
}
655669
this._showLoading = false;
@@ -663,19 +677,9 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
663677
this.fireCustomEvent('selectionChanged', this.selectedPeople);
664678
}
665679

666-
if (this.groupId && input) {
667-
if (this._groupPeople === null) {
668-
try {
669-
this._groupPeople = await getPeopleFromGroup(graph, this.groupId);
670-
} catch (_) {
671-
this._groupPeople = [];
672-
}
673-
}
674-
675-
people = this._groupPeople || [];
676-
} else if (input) {
680+
if (input) {
677681
people = [];
678-
if (this.type === PersonType.Person || this.type === PersonType.Any) {
682+
if (this.type === PersonType.person || this.type === PersonType.any) {
679683
try {
680684
people = (await findPeople(graph, input, this.showMax)) || [];
681685
} catch (e) {
@@ -699,7 +703,8 @@ export class MgtPeoplePicker extends MgtTemplatedComponent {
699703
}
700704
}
701705

702-
if ((this.type === PersonType.Group || this.type === PersonType.Any) && people.length < this.showMax) {
706+
if ((this.type === PersonType.group || this.type === PersonType.any) && people.length < this.showMax) {
707+
people = [];
703708
try {
704709
const groups = (await findGroups(graph, input, this.showMax, this.groupType)) || [];
705710
people = people.concat(groups);

src/graph/graph.groups.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,31 @@ export enum GroupType {
1919
/**
2020
* Any group Type
2121
*/
22-
Any = 0,
22+
any = 0,
2323

2424
/**
2525
* Office 365 group
2626
*/
2727
// tslint:disable-next-line:no-bitwise
28-
Unified = 1 << 0,
28+
unified = 1 << 0,
2929

3030
/**
3131
* Security group
3232
*/
3333
// tslint:disable-next-line:no-bitwise
34-
Security = 1 << 1,
34+
security = 1 << 1,
3535

3636
/**
3737
* Mail Enabled Security group
3838
*/
3939
// tslint:disable-next-line:no-bitwise
40-
MailEnabledSecurity = 1 << 2,
40+
mailenabledsecurity = 1 << 2,
4141

4242
/**
4343
* Distribution Group
4444
*/
4545
// tslint:disable-next-line:no-bitwise
46-
Distribution = 1 << 3
46+
distribution = 1 << 3
4747
}
4848

4949
/**
@@ -53,43 +53,46 @@ export enum GroupType {
5353
* @param {IGraph} graph
5454
* @param {string} query - what to search for
5555
* @param {number} [top=10] - number of groups to return
56-
* @param {GroupType} [groupTypes=GroupType.Any] - the type of group to search for
56+
* @param {GroupType} [groupTypes=GroupType.any] - the type of group to search for
5757
* @returns {Promise<Group[]>} An array of Groups
5858
*/
5959
export async function findGroups(
6060
graph: IGraph,
6161
query: string,
6262
top: number = 10,
63-
groupTypes: GroupType = GroupType.Any
63+
groupTypes: GroupType = GroupType.any
6464
): Promise<Group[]> {
6565
const scopes = 'Group.Read.All';
6666

67-
let filterQuery = `(startswith(displayName,'${query}') or startswith(mailNickname,'${query}') or startswith(mail,'${query}'))`;
67+
let filterQuery = '';
68+
if (query !== '') {
69+
filterQuery = `(startswith(displayName,'${query}') or startswith(mailNickname,'${query}') or startswith(mail,'${query}'))`;
70+
}
6871

69-
if (groupTypes !== GroupType.Any) {
72+
if (groupTypes !== GroupType.any) {
7073
const filterGroups = [];
7174

7275
// tslint:disable-next-line:no-bitwise
73-
if (GroupType.Unified === (groupTypes & GroupType.Unified)) {
76+
if (GroupType.unified === (groupTypes & GroupType.unified)) {
7477
filterGroups.push("groupTypes/any(c:c+eq+'Unified')");
7578
}
7679

7780
// tslint:disable-next-line:no-bitwise
78-
if (GroupType.Security === (groupTypes & GroupType.Security)) {
81+
if (GroupType.security === (groupTypes & GroupType.security)) {
7982
filterGroups.push('(mailEnabled eq false and securityEnabled eq true)');
8083
}
8184

8285
// tslint:disable-next-line:no-bitwise
83-
if (GroupType.MailEnabledSecurity === (groupTypes & GroupType.MailEnabledSecurity)) {
86+
if (GroupType.mailenabledsecurity === (groupTypes & GroupType.mailenabledsecurity)) {
8487
filterGroups.push('(mailEnabled eq true and securityEnabled eq true)');
8588
}
8689

8790
// tslint:disable-next-line:no-bitwise
88-
if (GroupType.Distribution === (groupTypes & GroupType.Distribution)) {
91+
if (GroupType.distribution === (groupTypes & GroupType.distribution)) {
8992
filterGroups.push('(mailEnabled eq true and securityEnabled eq false)');
9093
}
9194

92-
filterQuery += ' and ' + filterGroups.join(' or ');
95+
filterQuery += (query !== '' ? ' and ' : '') + filterGroups.join(' or ');
9396
}
9497

9598
const result = await graph

src/graph/graph.people.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,45 @@ export enum PersonType {
2020
/**
2121
* Any type
2222
*/
23-
Any = 0,
23+
any = 0,
2424

2525
/**
2626
* A Person such as User or Contact
2727
*/
28-
Person = 'Person',
28+
person = 'person',
2929

3030
/**
3131
* A group
3232
*/
33-
Group = 'Group'
33+
group = 'group'
3434
}
3535

3636
/**
3737
* async promise, returns all Graph people who are most relevant contacts to the signed in user.
3838
*
3939
* @param {string} query
4040
* @param {number} [top=10] - number of people to return
41-
* @param {PersonType} [personType=PersonType.Person] - the type of person to search for
41+
* @param {PersonType} [personType=PersonType.person] - the type of person to search for
4242
* @returns {(Promise<Person[]>)}
4343
*/
4444
export async function findPeople(
4545
graph: IGraph,
4646
query: string,
4747
top: number = 10,
48-
personType: PersonType = PersonType.Person
48+
personType: PersonType = PersonType.person
4949
): Promise<Person[]> {
5050
const scopes = 'people.read';
5151

5252
let filterQuery = '';
5353

54-
if (personType !== PersonType.Any) {
55-
filterQuery = `personType/class eq '${personType}'`;
54+
if (personType !== PersonType.any) {
55+
// converts personType to capitalized case
56+
const personTypeString =
57+
personType
58+
.toString()
59+
.charAt(0)
60+
.toUpperCase() + personType.toString().slice(1);
61+
filterQuery = `personType/class eq '${personTypeString}'`;
5662
}
5763

5864
const result = await graph

stories/components/peoplePicker.stories.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ export const DarkMode = () => html`
5050
`;
5151

5252
export const pickPeopleAndGroups = () => html`
53-
<mgt-people-picker type="Any"></mgt-people-picker>
54-
<!-- type can be "Any", "Person", "Group" -->
53+
<mgt-people-picker type="any"></mgt-people-picker>
54+
<!-- type can be "any", "person", "group" -->
5555
`;
5656

5757
export const pickGroups = () => html`
58-
<mgt-people-picker type="Group"></mgt-people-picker>
59-
<!-- type can be "Any", "Person", "Group" -->
58+
<mgt-people-picker type="group"></mgt-people-picker>
59+
<!-- type can be "any", "person", "group" -->
6060
`;
6161

6262
export const pickDistributionGroups = () => html`
63-
<mgt-people-picker type="Group" group-type="Distribution"></mgt-people-picker>
64-
<!-- group-type can be "Any", "Unified", "Security", "MailEnabledSecurity", "Distribution" -->
63+
<mgt-people-picker type="group" group-type="distribution"></mgt-people-picker>
64+
<!-- group-type can be "any", "unified", "security", "mailenabledsecurity", "distribution" -->
6565
`;

0 commit comments

Comments
 (0)