Initial rewrite of the apps view

This commit is contained in:
Johannes Zellner
2024-12-29 00:36:48 +01:00
parent a42c7e4735
commit 55e0d734df
6 changed files with 164 additions and 14 deletions
+71
View File
@@ -0,0 +1,71 @@
import { ISTATES } from '../constants.js';
import { fetcher } from 'pankow';
import { sleep } from 'pankow/utils';
export function create(origin, accessToken, id) {
return {
name: 'AppsModel',
async list() {
let error, result;
try {
result = await fetcher.get(`${origin}/api/v1/apps`, { access_token: accessToken });
} catch (e) {
error = e;
}
if (error || result.status !== 200) {
console.error('Failed to list apps.', error || result.status);
return [];
}
return result.body.apps;
},
async get() {
let error, result;
try {
result = await fetcher.get(`${origin}/api/v1/apps/${id}`, { access_token: accessToken });
} catch (e) {
error = e;
}
if (error || result.status !== 200) {
console.error(`Invalid app ${id}`, error || result.status);
this.fatalError = `Invalid app ${id}`;
return;
}
return result.body;
},
async restart() {
let error, result;
try {
result = await fetcher.post(`${origin}/api/v1/apps/${id}/restart`, null, { access_token: accessToken });
} catch (e) {
error = e;
}
if (error || result.status !== 202) {
console.error(`Failed to restart app ${this.id}`, error || result.status);
return;
}
while(true) {
let result;
try {
result = await fetcher.get(`${origin}/api/v1/apps/${id}`, { access_token: accessToken });
} catch (e) {
console.error(e);
}
if (result && result.status === 200 && result.body.installationState === ISTATES.INSTALLED) break;
await sleep(2000);
}
}
};
}
export default {
create
};