Skip to content

Commit 381d05c

Browse files
committed
models/version: Simplify PATCH calls by using ember-api-actions
1 parent 4f4f698 commit 381d05c

File tree

2 files changed

+17
-34
lines changed

2 files changed

+17
-34
lines changed

app/adapters/version.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import ApplicationAdapter from './application';
2+
3+
export default class VersionAdapter extends ApplicationAdapter {
4+
urlForUpdateRecord(id, modelName, snapshot) {
5+
let crateName = snapshot.record.crate.id;
6+
let num = snapshot.record.num;
7+
return `/${this.namespace}/crates/${crateName}/${num}`;
8+
}
9+
}

app/models/version.js

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
2+
import { waitForPromise } from '@ember/test-waiters';
23

4+
import { apiAction } from '@mainmatter/ember-api-actions';
35
import { keepLatestTask, task } from 'ember-concurrency';
46
import fetch from 'fetch';
57
import { alias } from 'macro-decorators';
@@ -166,42 +168,14 @@ export default class Version extends Model {
166168
}
167169

168170
yankTask = keepLatestTask(async () => {
169-
let response = await fetch(`/api/v1/crates/${this.crate.id}/${this.num}`, {
170-
method: 'PATCH',
171-
headers: {
172-
'Content-Type': 'application/json',
173-
},
174-
body: JSON.stringify({
175-
version: {
176-
yanked: true,
177-
},
178-
}),
179-
});
180-
if (!response.ok) {
181-
throw new Error(`Yank request for ${this.crateName} v${this.num} failed`);
182-
}
183-
this.set('yanked', true);
184-
185-
return await response.json();
171+
let data = { version: { yanked: true } };
172+
let payload = await waitForPromise(apiAction(this, { method: 'PATCH', data }));
173+
this.store.pushPayload(payload);
186174
});
187175

188176
unyankTask = keepLatestTask(async () => {
189-
let response = await fetch(`/api/v1/crates/${this.crate.id}/${this.num}`, {
190-
method: 'PATCH',
191-
headers: {
192-
'Content-Type': 'application/json',
193-
},
194-
body: JSON.stringify({
195-
version: {
196-
yanked: false,
197-
},
198-
}),
199-
});
200-
if (!response.ok) {
201-
throw new Error(`Unyank request for ${this.crateName} v${this.num} failed`);
202-
}
203-
this.set('yanked', false);
204-
205-
return await response.json();
177+
let data = { version: { yanked: false } };
178+
let payload = await waitForPromise(apiAction(this, { method: 'PATCH', data }));
179+
this.store.pushPayload(payload);
206180
});
207181
}

0 commit comments

Comments
 (0)