DEV: don't swallow a promise from group.findMembers method and switch to using async/await (#13888)

This commit is contained in:
Andrei Prigorshnev 2021-07-30 21:00:34 +04:00 committed by GitHub
parent f740c1a952
commit f0d048b42a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 67 deletions

View File

@ -51,7 +51,7 @@ export default Component.extend(CardContentsBase, CleansUp, {
}
return group.can_see_members &&
group.members.length < maxMembersToDisplay
? group.findMembers({ limit: maxMembersToDisplay }, true)
? group.reloadMembers({ limit: maxMembersToDisplay }, true)
: Promise.resolve();
})
.catch(() => this._close())

View File

@ -35,10 +35,10 @@ export default Controller.extend({
@observes("order", "asc", "filter")
_filtersChanged() {
this.findMembers(true);
this.reloadMembers(true);
},
findMembers(refresh) {
reloadMembers(refresh) {
if (this.loading || !this.model) {
return;
}
@ -49,7 +49,7 @@ export default Controller.extend({
}
this.set("loading", true);
this.model.findMembers(this.memberParams, refresh).finally(() => {
this.model.reloadMembers(this.memberParams, refresh).finally(() => {
this.setProperties({
"application.showFooter":
this.model.members.length >= this.model.user_count,
@ -96,7 +96,7 @@ export default Controller.extend({
@action
loadMore() {
this.findMembers();
this.reloadMembers();
},
@action
@ -139,7 +139,7 @@ export default Controller.extend({
type: "DELETE",
data: { user_ids: selection.map((u) => u.id).join(",") },
}).then(() => {
this.model.findMembers(this.memberParams, true);
this.model.reloadMembers(this.memberParams, true);
this.set("isBulk", false);
});

View File

@ -34,9 +34,9 @@ const Group = RestModel.extend({
return automatic ? "automatic" : "custom";
},
findMembers(params, refresh) {
async reloadMembers(params, refresh) {
if (isEmpty(this.name) || !this.can_see_members) {
return Promise.reject();
return;
}
if (refresh) {
@ -48,25 +48,24 @@ const Group = RestModel.extend({
params
);
return Group.loadMembers(this.name, params).then((result) => {
const ownerIds = new Set();
result.owners.forEach((owner) => ownerIds.add(owner.id));
const response = await Group.loadMembers(this.name, params);
const ownerIds = new Set();
response.owners.forEach((owner) => ownerIds.add(owner.id));
const members = refresh ? [] : this.members;
members.pushObjects(
result.members.map((member) => {
member.owner = ownerIds.has(member.id);
member.primary = member.primary_group_name === this.name;
return User.create(member);
})
);
const members = refresh ? [] : this.members;
members.pushObjects(
response.members.map((member) => {
member.owner = ownerIds.has(member.id);
member.primary = member.primary_group_name === this.name;
return User.create(member);
})
);
this.setProperties({
members,
user_count: result.meta.total,
limit: result.meta.limit,
offset: result.meta.offset,
});
this.setProperties({
members,
user_count: response.meta.total,
limit: response.meta.limit,
offset: response.meta.offset,
});
},
@ -100,62 +99,63 @@ const Group = RestModel.extend({
});
},
removeOwner(member) {
return ajax(`/admin/groups/${this.id}/owners.json`, {
async removeOwner(member) {
await ajax(`/admin/groups/${this.id}/owners.json`, {
type: "DELETE",
data: { user_id: member.id },
}).then(() => this.findMembers({}, true));
});
await this.reloadMembers({}, true);
},
removeMember(member, params) {
return ajax(`/groups/${this.id}/members.json`, {
async removeMember(member, params) {
await ajax(`/groups/${this.id}/members.json`, {
type: "DELETE",
data: { user_id: member.id },
}).then(() => this.findMembers(params, true));
});
await this.reloadMembers(params, true);
},
leave() {
return ajax(`/groups/${this.id}/leave.json`, {
async leave() {
await ajax(`/groups/${this.id}/leave.json`, {
type: "DELETE",
}).then(() => this.findMembers({}, true));
});
await this.reloadMembers({}, true);
},
addMembers(usernames, filter, notifyUsers, emails = []) {
return ajax(`/groups/${this.id}/members.json`, {
async addMembers(usernames, filter, notifyUsers, emails = []) {
const response = await ajax(`/groups/${this.id}/members.json`, {
type: "PUT",
data: { usernames, emails, notify_users: notifyUsers },
}).then((response) => {
if (filter) {
this._filterMembers(response);
} else {
this.findMembers();
}
});
if (filter) {
await this._filterMembers(response.usernames);
} else {
await this.reloadMembers();
}
},
join() {
return ajax(`/groups/${this.id}/join.json`, {
async join() {
await ajax(`/groups/${this.id}/join.json`, {
type: "PUT",
}).then(() => {
this.findMembers({}, true);
});
await this.reloadMembers({}, true);
},
addOwners(usernames, filter, notifyUsers) {
return ajax(`/admin/groups/${this.id}/owners.json`, {
async addOwners(usernames, filter, notifyUsers) {
const response = await ajax(`/admin/groups/${this.id}/owners.json`, {
type: "PUT",
data: { group: { usernames, notify_users: notifyUsers } },
}).then((response) => {
if (filter) {
this._filterMembers(response);
} else {
this.findMembers({}, true);
}
});
if (filter) {
await this._filterMembers(response.usernames);
} else {
await this.reloadMembers({}, true);
}
},
_filterMembers(response) {
return this.findMembers({ filter: response.usernames.join(",") });
_filterMembers(usernames) {
return this.reloadMembers({ filter: usernames.join(",") });
},
@discourseComputed("display_name", "name")
@ -291,19 +291,19 @@ const Group = RestModel.extend({
return attrs;
},
create() {
return ajax("/admin/groups", {
async create() {
const response = await ajax("/admin/groups", {
type: "POST",
data: { group: this.asJSON() },
}).then((resp) => {
this.setProperties({
id: resp.basic_group.id,
usernames: null,
ownerUsernames: null,
});
this.findMembers();
});
this.setProperties({
id: response.basic_group.id,
usernames: null,
ownerUsernames: null,
});
await this.reloadMembers();
},
save(opts = {}) {

View File

@ -20,7 +20,7 @@ export default DiscourseRoute.extend({
showing: "members",
});
controller.findMembers(true);
controller.reloadMembers(true);
},
@action