refactor: ♻️ refacto container requests

This commit is contained in:
Michel Roux 2024-11-05 00:12:25 +01:00
parent 4943f81dea
commit dedcfa2708

View File

@ -96,72 +96,34 @@ export default {
await this.refresh() await this.refresh()
}, },
methods: { methods: {
async pull() { async request(method: 'get' | 'post' | 'delete', path: string) {
try { try {
this.loading = true this.loading = true
const response = await axios.post<Container>( const response = await axios[method]<Container>(path)
`/api/container/${this.container_name}/pull`,
)
this.container = response.data this.container = response.data
} catch (error) { } catch (error) {
console.error(error) console.error(error)
this.error = this.error =
error instanceof AxiosError error instanceof AxiosError
? error.message ? error.message
: 'Error pulling container' : 'Error performing operation'
} finally { } finally {
this.loading = false this.loading = false
} }
}, },
async pull() {
await this.request('post', `/api/container/${this.container_name}/pull`)
},
async refresh() { async refresh() {
try { await this.request('get', `/api/container/${this.container_name}`)
this.loading = true
const response = await axios.get<Container>(
`/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
}
}, },
async remove() { async remove() {
if (!confirm('Are you sure you want to remove this container?')) return if (!confirm('Are you sure you want to remove this container?')) return
try { await this.request('delete', `/api/container/${this.container_name}`)
this.loading = true this.$router.push('/')
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
}
}, },
async restart() { async restart() {
try { this.request('post', `/api/container/${this.container_name}/restart`)
this.loading = true
const response = await axios.post<Container>(
`/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
}
}, },
}, },
} }