Skip to content

crate.owners: Replace async actions with ember-concurrency tasks #2448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 43 additions & 41 deletions app/controllers/crate/owners.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,59 @@
import Controller from '@ember/controller';

import { task } from 'ember-concurrency';

export default Controller.extend({
crate: null,
error: false,
invited: false,
removed: false,
username: '',

actions: {
async addOwner() {
this.set('error', false);
this.set('invited', false);
addOwnerTask: task(function* (event) {
event.preventDefault();

this.set('error', false);
this.set('invited', false);

const username = this.username;

const username = this.username;
if (!username) {
this.set('error', 'Please enter a username');
return false;
}

if (!username) {
this.set('error', 'Please enter a username');
return false;
try {
yield this.crate.inviteOwner(username);
this.set('invited', `An invite has been sent to ${username}`);
} catch (error) {
if (error.errors) {
this.set('error', `Error sending invite: ${error.errors[0].detail}`);
} else {
this.set('error', 'Error sending invite');
}
}
}),

try {
await this.crate.inviteOwner(username);
this.set('invited', `An invite has been sent to ${username}`);
} catch (error) {
if (error.errors) {
this.set('error', `Error sending invite: ${error.errors[0].detail}`);
} else {
this.set('error', 'Error sending invite');
}
removeOwnerTask: task(function* (owner) {
this.set('removed', false);
try {
yield this.crate.removeOwner(owner.get('login'));
switch (owner.kind) {
case 'user':
this.set('removed', `User ${owner.get('login')} removed as crate owner`);
this.get('crate.owner_user').removeObject(owner);
break;
case 'team':
this.set('removed', `Team ${owner.get('display_name')} removed as crate owner`);
this.get('crate.owner_team').removeObject(owner);
break;
}
},

async removeOwner(owner) {
this.set('removed', false);
try {
await this.crate.removeOwner(owner.get('login'));
switch (owner.kind) {
case 'user':
this.set('removed', `User ${owner.get('login')} removed as crate owner`);
this.get('crate.owner_user').removeObject(owner);
break;
case 'team':
this.set('removed', `Team ${owner.get('display_name')} removed as crate owner`);
this.get('crate.owner_team').removeObject(owner);
break;
}
} catch (error) {
if (error.errors) {
this.set('removed', `Error removing owner: ${error.errors[0].detail}`);
} else {
this.set('removed', 'Error removing owner');
}
} catch (error) {
if (error.errors) {
this.set('removed', `Error removing owner: ${error.errors[0].detail}`);
} else {
this.set('removed', 'Error removing owner');
}
},
},
}
}),
});
6 changes: 3 additions & 3 deletions app/templates/crate/owners.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<p data-test-invited-message>{{this.invited}}</p>
{{/if}}

<form local-class="email-form" {{action 'addOwner' on='submit'}}>
<form local-class="email-form" {{on "submit" (perform this.addOwnerTask)}}>
<label local-class="email-input-label" for='new-owner-username'>
Username
</label>
Expand All @@ -44,7 +44,7 @@
<div local-class="email-column">
{{team.email}}
</div>
<button type="button" local-class="remove-button" data-test-remove-owner-button {{action 'removeOwner' team}}>Remove</button>
<button type="button" local-class="remove-button" data-test-remove-owner-button {{on "click" (perform this.removeOwnerTask team)}}>Remove</button>
</div>
{{/each}}
{{#each this.crate.owner_user as |user|}}
Expand All @@ -58,7 +58,7 @@
<div local-class="email-column">
{{user.email}}
</div>
<button type="button" local-class="remove-button" data-test-remove-owner-button {{action 'removeOwner' user}}>Remove</button>
<button type="button" local-class="remove-button" data-test-remove-owner-button {{on "click" (perform this.removeOwnerTask user)}}>Remove</button>
</div>
{{/each}}
</div>