Skip to content

Commit

Permalink
Merge pull request #88 from ddbj/poll
Browse files Browse the repository at this point in the history
Poll request changes
  • Loading branch information
ursm authored Jan 12, 2024
2 parents 928a245 + 15f0e01 commit ca0aaae
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 41 deletions.
4 changes: 1 addition & 3 deletions web/app/controllers/requests/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ export default class RequestsShowController extends Controller {
@action
async downloadFile(url: string) {
const res = await fetch(url, {
headers: {
Authorization: `Bearer ${this.currentUser.apiKey}`,
},
headers: this.currentUser.authorizationHeader,
});

location.href = res.url;
Expand Down
6 changes: 1 addition & 5 deletions web/app/controllers/submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ export default class SubmitController extends Controller {

const res = await fetch(url, {
method: 'POST',

headers: {
Authorization: `Bearer ${this.currentUser.apiKey}`,
},

headers: this.currentUser.authorizationHeader,
body: formData,
});

Expand Down
4 changes: 3 additions & 1 deletion web/app/helpers/format-datetime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default function formatDatetime(date: Date | string) {
export default function formatDatetime(date?: Date | string) {
if (!date) return '';

if (typeof date === 'string') {
date = new Date(date);
}
Expand Down
4 changes: 1 addition & 3 deletions web/app/routes/requests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ export default class RequestsRoute extends Route {

async model() {
const res = await fetch(`${ENV.apiURL}/requests`, {
headers: {
Authorization: `Bearer ${this.currentUser.apiKey}`,
},
headers: this.currentUser.authorizationHeader,
});

return await res.json();
Expand Down
35 changes: 19 additions & 16 deletions web/app/routes/requests/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,27 @@ import type CurrentUserService from 'ddbj-repository/services/current-user';
export default class RequestsShowRoute extends Route {
@service declare currentUser: CurrentUserService;

async model({ id }: { id: string }) {
return await waitForRequestFinished(`${ENV.apiURL}/requests/${id}`, this.currentUser.apiKey!);
}
}

async function waitForRequestFinished(url: string, apiKey: string) {
const res = await fetch(url, {
headers: {
Authorization: `Bearer ${apiKey}`,
},
});
timer?: number;

const payload = await res.json();
const { status } = payload;
async model({ id }: { id: string }) {
const res = await fetch(`${ENV.apiURL}/requests/${id}`, {
headers: this.currentUser.authorizationHeader,
});

if (status === 'finished' || status === 'canceled') return payload;
return await res.json();
}

await new Promise((resolve) => setTimeout(resolve, 1000));
afterModel({ status }: { status: string }) {
if (status !== 'finished' && status !== 'canceled') {
this.timer = setTimeout(() => {
this.refresh();
}, 1000);
}
}

return waitForRequestFinished(url, apiKey);
deactivate() {
if (this.timer) {
clearTimeout(this.timer);
}
}
}
37 changes: 24 additions & 13 deletions web/app/services/current-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export default class CurrentUserService extends Service {
return !!this.apiKey;
}

get authorizationHeader() {
return { Authorization: `Bearer ${this.apiKey}` };
}

ensureLogin() {
if (!this.isLoggedIn) {
this.router.transitionTo('login');
Expand All @@ -32,6 +36,9 @@ export default class CurrentUserService extends Service {
async login(apiKey: string) {
localStorage.setItem('apiKey', apiKey);

this.apiKey = apiKey;
this.uid = undefined;

await this.restore();

this.router.transitionTo('index');
Expand All @@ -40,32 +47,36 @@ export default class CurrentUserService extends Service {
async logout() {
localStorage.removeItem('apiKey');

this.apiKey = this.uid = undefined;

await this.restore();

this.router.transitionTo('index');
}

async restore() {
this.apiKey = localStorage.getItem('apiKey') || undefined;
if (!this.apiKey) {
this.apiKey = localStorage.getItem('apiKey') || undefined;
}

if (this.apiKey) {
const res = await fetch(`${ENV.apiURL}/me`, {
headers: {
Authorization: `Bearer ${this.apiKey}`,
},
});
if (!this.uid) {
const res = await fetch(`${ENV.apiURL}/me`, {
headers: this.authorizationHeader,
});

if (!res.ok) {
localStorage.removeItem('apiKey');
if (!res.ok) {
localStorage.removeItem('apiKey');

this.apiKey = this.uid = undefined;
this.apiKey = this.uid = undefined;

throw new LoginError();
}
throw new LoginError();
}

const { uid } = await res.json();
const { uid } = await res.json();

this.uid = uid;
this.uid = uid;
}
} else {
this.uid = undefined;
}
Expand Down

0 comments on commit ca0aaae

Please sign in to comment.