Skip to content

Commit

Permalink
app: Simplify isOwner with crate.hasOwnerUser(userId) fn
Browse files Browse the repository at this point in the history
This fixes the following deprecations:
- The findBy method on ember-data's PromiseManyArray is deprecated.
- The `findBy` method on the class ManyArray is deprecated.
  • Loading branch information
eth3lbert committed Dec 13, 2024
1 parent 770c2b0 commit c7a5fbf
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
13 changes: 2 additions & 11 deletions app/components/crate-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,21 @@ export default class CrateHeader extends Component {
@service session;

@alias('loadKeywordsTask.last.value') keywords;
@alias('loadOwnerUserTask.last.value') ownerUser;

constructor() {
super(...arguments);

this.loadKeywordsTask.perform().catch(() => {
// ignore all errors and just don't display keywords if the request fails
});
this.loadOwnerUserTask.perform().catch(() => {
// ignore all errors and just don't display settings if the request fails
});
}

get isOwner() {
let ownerUser = this.ownerUser ?? [];
let currentUserId = this.session.currentUser?.id;
return ownerUser.some(({ id }) => id === currentUserId);
let userId = this.session.currentUser?.id;
return this.args.crate?.hasOwnerUser(userId) ?? false;
}

loadKeywordsTask = task(async () => {
return (await this.args.crate?.keywords) ?? [];
});

loadOwnerUserTask = task(async () => {
return (await this.args.crate?.owner_user) ?? [];
});
}
3 changes: 2 additions & 1 deletion app/components/version-list/row.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export default class VersionRow extends Component {
}

get isOwner() {
return this.args.version.crate?.owner_user?.findBy('id', this.session.currentUser?.id);
let userId = this.session.currentUser?.id;
return this.args.version.crate.hasOwnerUser(userId);
}

@action setFocused(value) {
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/crate/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export default class CrateVersionController extends Controller {
@alias('model.version') currentVersion;

get isOwner() {
return this.crate.owner_user.findBy('id', this.session.currentUser?.id);
let userId = this.session.currentUser?.id;
return this.crate.hasOwnerUser(userId);
}

@alias('loadReadmeTask.last.value') readme;
Expand Down
4 changes: 4 additions & 0 deletions app/routes/crate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NotFoundError } from '@ember-data/adapter/error';
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { waitForPromise } from '@ember/test-waiters';

export default class CrateRoute extends Route {
@service headData;
Expand All @@ -26,6 +27,9 @@ export default class CrateRoute extends Route {
setupController(controller, model) {
super.setupController(...arguments);
this.headData.crate = model;
waitForPromise(model.loadOwnerUserTask.perform()).catch(() => {
// ignore all errors if the request fails
});
}

resetController() {
Expand Down
3 changes: 3 additions & 0 deletions tests/components/version-list-row-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module('Component | VersionList::Row', function (hooks) {
let store = this.owner.lookup('service:store');
let crateRecord = await store.findRecord('crate', crate.name);
let versions = (await crateRecord.versions).slice();
await crateRecord.loadOwnerUserTask.perform();
this.firstVersion = versions[0];
this.secondVersion = versions[1];

Expand All @@ -39,6 +40,7 @@ module('Component | VersionList::Row', function (hooks) {
let store = this.owner.lookup('service:store');
let crateRecord = await store.findRecord('crate', crate.name);
this.version = (await crateRecord.versions).slice()[0];
await crateRecord.loadOwnerUserTask.perform();

await render(hbs`<VersionList::Row @version={{this.version}} />`);
assert.dom('[data-test-release-track]').hasText('?');
Expand Down Expand Up @@ -72,6 +74,7 @@ module('Component | VersionList::Row', function (hooks) {
let store = this.owner.lookup('service:store');
let crateRecord = await store.findRecord('crate', crate.name);
let versions = (await crateRecord.versions).slice();
await crateRecord.loadOwnerUserTask.perform();
this.firstVersion = versions[0];
this.secondVersion = versions[1];
this.thirdVersion = versions[2];
Expand Down

0 comments on commit c7a5fbf

Please sign in to comment.