Add job namespace and remove old json_routes logic

This commit is contained in:
Patrick Jentsch
2024-12-06 11:41:36 +01:00
parent 1372c86609
commit 93344c9573
10 changed files with 82 additions and 110 deletions

View File

@ -3,6 +3,7 @@ nopaque.App = class App {
this.socket = io({transports: ['websocket'], upgrade: false});
// Endpoints
this.jobs = new nopaque.app.endpoints.Jobs(this);
this.users = new nopaque.app.endpoints.Users(this);
// Extensions

View File

@ -0,0 +1,37 @@
nopaque.app.endpoints.Jobs = class Jobs {
constructor(app) {
this.app = app;
this.socket = io('/jobs', {transports: ['websocket'], upgrade: false});
}
async delete(id) {
const response = await this.socket.emitWithAck('delete', id);
if (response.status != 202) {
throw new Error(`[${response.status}] ${response.statusText}`);
}
return response.body;
}
async log(id) {
const response = await this.socket.emitWithAck('log', id);
if (response.status != 200) {
throw new Error(`[${response.status}] ${response.statusText}`);
}
return response.body;
}
async restart(id) {
const response = await this.socket.emitWithAck('restart', id);
if (response.status != 202) {
throw new Error(`[${response.status}] ${response.statusText}`);
}
return response.body;
}
}

View File

@ -5,8 +5,8 @@ nopaque.app.endpoints.Users = class Users {
this.socket = io('/users', {transports: ['websocket'], upgrade: false});
}
async get(userId) {
const response = await this.socket.emitWithAck('get', userId);
async get(id) {
const response = await this.socket.emitWithAck('get', id);
if (response.status !== 200) {
throw new Error(`[${response.status}] ${response.statusText}`);
@ -15,27 +15,29 @@ nopaque.app.endpoints.Users = class Users {
return response.body;
}
async subscribe(userId) {
const response = await this.socket.emitWithAck('subscribe', userId);
async subscribe(id) {
const response = await this.socket.emitWithAck('subscribe', id);
if (response.status != 200) {
throw new Error(`[${response.status}] ${response.statusText}`);
}
}
async unsubscribe(userId) {
const response = await this.socket.emitWithAck('unsubscribe', userId);
async unsubscribe(id) {
const response = await this.socket.emitWithAck('unsubscribe', id);
if (response.status != 200) {
throw new Error(`[${response.status}] ${response.statusText}`);
}
}
async delete(userId) {
const response = await this.socket.emitWithAck('delete', userId);
async delete(id) {
const response = await this.socket.emitWithAck('delete', id);
if (response.status != 202) {
throw new Error(`[${response.status}] ${response.statusText}`);
}
return response.body;
}
}

View File

@ -136,8 +136,9 @@ nopaque.resource_lists.JobList = class JobList extends nopaque.resource_lists.Re
}
);
let confirmElement = modalElement.querySelector('.action-button[data-action="confirm"]');
confirmElement.addEventListener('click', (event) => {
nopaque.requests.jobs.entity.delete(itemId);
confirmElement.addEventListener('click', async (event) => {
const message = await app.jobs.delete(itemId);
app.ui.flash(message, 'job');
});
modal.open();
break;
@ -221,8 +222,9 @@ nopaque.resource_lists.JobList = class JobList extends nopaque.resource_lists.Re
);
let confirmElement = modalElement.querySelector('.action-button[data-action="confirm"]');
confirmElement.addEventListener('click', (event) => {
this.selectedItemIds.forEach(selectedItemId => {
nopaque.requests.jobs.entity.delete(selectedItemId);
this.selectedItemIds.forEach(async (selectedItemId) => {
const message = await app.jobs.delete(selectedItemId);
app.ui.flash(message, 'job');
});
this.selectedItemIds.clear();
this.renderingItemSelection();