From dedcfa2708ba0207f6b5d0b4cc44cc7896e598ac Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Tue, 5 Nov 2024 00:12:25 +0100 Subject: [PATCH] refactor: :recycle: refacto container requests --- frontend/views/Container.vue | 58 +++++++----------------------------- 1 file changed, 10 insertions(+), 48 deletions(-) diff --git a/frontend/views/Container.vue b/frontend/views/Container.vue index 143fb5f..94c622b 100644 --- a/frontend/views/Container.vue +++ b/frontend/views/Container.vue @@ -96,72 +96,34 @@ export default { await this.refresh() }, methods: { - async pull() { + async request(method: 'get' | 'post' | 'delete', path: string) { try { this.loading = true - const response = await axios.post( - `/api/container/${this.container_name}/pull`, - ) + const response = await axios[method](path) this.container = response.data } catch (error) { console.error(error) this.error = error instanceof AxiosError ? error.message - : 'Error pulling container' + : 'Error performing operation' } finally { this.loading = false } }, + async pull() { + await this.request('post', `/api/container/${this.container_name}/pull`) + }, async refresh() { - try { - this.loading = true - const response = await axios.get( - `/api/container/${this.container_name}`, - ) - this.container = response.data - } catch (error) { - console.error(error) - this.error = - error instanceof AxiosError - ? error.message - : 'Error loading container' - } finally { - this.loading = false - } + await this.request('get', `/api/container/${this.container_name}`) }, async remove() { if (!confirm('Are you sure you want to remove this container?')) return - try { - this.loading = true - await axios.delete(`/api/container/${this.container_name}`) - this.$router.push('/') - } catch (error) { - console.error(error) - this.error = - error instanceof AxiosError - ? error.message - : 'Error deleting container' - } finally { - this.loading = false - } + await this.request('delete', `/api/container/${this.container_name}`) + this.$router.push('/') }, async restart() { - try { - this.loading = true - const response = await axios.post( - `/api/container/${this.container_name}/restart`, - ) - this.container = response.data - } catch (error) { - console.error(error) - this.error = - error instanceof AxiosError - ? error.message - : 'Error restarting container' - } finally { - this.loading = false - } + this.request('post', `/api/container/${this.container_name}/restart`) }, }, }