meilisearch-admin/app/components/toast/task.js

130 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2022-09-14 07:29:58 +00:00
import Component from '@glimmer/component';
import { registerDestructor } from '@ember/destroyable';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { Loading, Check, AlertOctagonOutline } from 'ember-mdi';
2022-09-21 10:31:59 +00:00
import Task from 'meiliadmin/models/task';
2022-09-14 07:29:58 +00:00
2022-09-16 08:19:44 +00:00
// TODO: refactor for using more inside Task logic
2022-09-21 10:31:59 +00:00
export default class ToastTaskComponent extends Component {
2022-09-14 07:29:58 +00:00
@service meilisearch;
@tracked task;
2022-09-14 09:55:48 +00:00
@tracked autoClose;
2022-09-14 07:29:58 +00:00
constructor(owner, args) {
super(owner, args);
this.interval = setInterval(this.reloadTask, 1000);
registerDestructor(this, this.clearInterval);
}
get icon() {
switch (this.taskStatus) {
case 'enqueued':
2022-09-21 11:26:09 +00:00
case 'processing':
2022-09-14 07:29:58 +00:00
return Loading;
case 'succeeded':
return Check;
case 'failed':
return AlertOctagonOutline;
default:
return null;
}
}
2022-09-14 09:55:48 +00:00
get status() {
2022-09-21 11:26:09 +00:00
if (this.isEnqueued || this.isProcessing) {
2022-09-14 09:55:48 +00:00
return 'info';
} else if (this.isFailed) {
return 'danger';
} else if (this.isSucceeded) {
return 'success';
} else {
return null;
}
}
2022-09-14 07:29:58 +00:00
get color() {
2022-09-14 09:55:48 +00:00
// Current classes need for taiwind builder
2022-09-21 11:26:09 +00:00
if (this.isEnqueued || this.isProcessing) {
2022-09-14 09:55:48 +00:00
return 'text-info';
} else if (this.isFailed) {
return 'text-danger';
} else if (this.isSucceeded) {
return 'text-success';
} else {
return null;
}
}
get bg() {
// Current classes need for taiwind builder
2022-09-21 11:26:09 +00:00
if (this.isEnqueued || this.isProcessing) {
2022-09-14 09:55:48 +00:00
return 'bg-info';
} else if (this.isFailed) {
return 'bg-danger';
} else if (this.isSucceeded) {
return 'bg-success';
} else {
return null;
2022-09-14 07:29:58 +00:00
}
}
get isEnqueued() {
return this.taskStatus === 'enqueued';
}
2022-09-21 11:26:09 +00:00
get isProcessing() {
return this.taskStatus === 'processing';
}
2022-09-14 07:29:58 +00:00
get isFailed() {
return this.taskStatus === 'failed';
}
get isSucceeded() {
return this.taskStatus === 'succeeded';
}
2022-09-21 11:26:09 +00:00
get isFulfilled() {
return this.isSucceeded || this.isFailed;
}
2022-09-14 07:29:58 +00:00
get taskUid() {
return this.task?.taskUid ?? this.args.task.taskUid;
}
get taskType() {
return this.task?.type ?? this.args.task.type;
}
get taskStatus() {
return this.task?.status ?? this.args.task.status;
}
@action
reloadTask() {
this.meilisearch.getTask(this.taskUid).then((task) => {
2022-09-16 08:19:44 +00:00
this.task = new Task(task);
2022-09-14 07:37:11 +00:00
2022-09-15 14:08:19 +00:00
if (this.isSucceeded || this.isFailed) {
2022-09-14 07:37:11 +00:00
this.clearInterval();
2022-09-14 11:43:09 +00:00
}
if (this.isSucceeded) {
2022-09-14 09:55:48 +00:00
this.autoClose = true;
2022-09-14 07:37:11 +00:00
}
2022-09-14 07:29:58 +00:00
});
}
@action
clearInterval() {
if (this.interval) {
clearInterval(this.interval);
}
}
}