open-vault/ui/app/components/raft-storage-restore.js
Jordan Reimer 52fe56ec87
Eslint prefer-const (#17864)
* adds prefer-const to eslint config and runs fixer

* reverts unintended change
2022-11-09 15:15:31 -08:00

46 lines
1.3 KiB
JavaScript

import Component from '@ember/component';
import { task } from 'ember-concurrency';
import { getOwner } from '@ember/application';
import { inject as service } from '@ember/service';
import { alias } from '@ember/object/computed';
import { AbortController } from 'fetch';
export default Component.extend({
file: null,
errors: null,
forceRestore: false,
flashMessages: service(),
isUploading: alias('restore.isRunning'),
abortController: null,
restore: task(function* () {
this.set('errors', null);
const adapter = getOwner(this).lookup('adapter:application');
try {
let url = '/v1/sys/storage/raft/snapshot';
if (this.forceRestore) {
url = `${url}-force`;
}
const file = new Blob([this.file], { type: 'application/gzip' });
const controller = new AbortController();
this.set('abortController', controller);
yield adapter.rawRequest(url, 'POST', { body: file, signal: controller.signal });
this.flashMessages.success('The snapshot was successfully uploaded!');
} catch (e) {
if (e.name === 'AbortError') {
return;
}
let resp;
if (e.json) {
resp = yield e.json();
}
const err = resp ? resp.errors : [e];
this.set('errors', err);
}
}),
actions: {
cancelUpload() {
this.abortController.abort();
},
},
});