birbcontrol/src/views/Models.vue

157 lines
4.3 KiB
Vue
Raw Normal View History

2021-08-09 16:15:55 +02:00
<template>
2021-08-09 17:42:08 +02:00
<b-row>
<b-col>
2021-08-10 18:03:43 +02:00
<b-modal id="delete-confirm-modal" @ok="performDelete(confirmModal.type, confirmModal.id)" title="Confirmation">
<p>
Are you sure you want to <b>delete</b> the following model?
</p>
<p>
<b>type:</b> {{ confirmModal.type }}
</p>
<p>
<b>id:</b> {{ confirmModal.id }}
</p>
</b-modal>
<model-modal-info v-model="infoModal.show" :id="infoModal.id" :type="infoModal.type" @refresh="updateModelList"
@delete="childActionDelete"/>
2021-08-09 17:42:08 +02:00
<b-table striped hover :items="models" :fields="fields" :tbody-tr-class="rowClass" :busy="actionPending">
2021-08-09 17:45:45 +02:00
<!-- A virtual column -->
2021-08-09 17:42:08 +02:00
<template #cell(actions)="data">
2021-08-10 18:03:43 +02:00
<b-button class="mx-1" size="sm" variant="primary" @click="actionInfo(data)">
<b-icon icon="info"/>
</b-button>
<b-button class="mx-1" size="sm" variant="danger" @click="actionDelete(data)">
<b-icon icon="trash"/>
</b-button>
2021-08-09 17:42:08 +02:00
</template>
2021-08-09 17:45:45 +02:00
<template #table-busy>
<div class="text-center my-2">
<b-spinner class="align-middle mx-2"/>
<strong>Loading...</strong>
</div>
</template>
2021-08-09 17:42:08 +02:00
</b-table>
</b-col>
</b-row>
2021-08-09 16:15:55 +02:00
</template>
<script>
2021-08-10 18:03:43 +02:00
import ModelModalInfo from "@/components/ModelModalInfo";
2021-08-09 16:15:55 +02:00
export default {
2021-08-09 17:42:08 +02:00
name: "Models",
2021-08-10 18:03:43 +02:00
components: {
ModelModalInfo
},
2021-08-09 17:42:08 +02:00
data() {
return {
actionPending: false,
models: [],
fields: [
{
key: 'type',
sortable: true
},
{
key: 'id',
sortable: true
},
2021-08-10 18:03:43 +02:00
"actions"
],
infoModal: {
id: "",
type: "",
show: false
},
confirmModal: {
id: "",
type: ""
}
2021-08-09 17:42:08 +02:00
}
},
methods: {
updateModelList() {
return new Promise((resolve, reject) => {
this.actionPending = true
this.$api.get("/model").then(({data}) => {
this.models = data
this.actionPending = false
resolve()
}).catch((error) => {
if (!error.response) {
// network error happened
this.$showToast("Some network error happened.\nCheck logs!")
} else {
// server returned bruh moment
this.$showToast(`The server returned error: ${error.response.status} ${error.response.statusText}\n${error.response.data}`)
}
this.actionPending = false
reject()
})
});
},
rowClass(item, type) {
if (!item || type !== 'row') return
if (item.default) return 'table-info'
2021-08-09 17:50:56 +02:00
},
actionDelete({item}) {
2021-08-10 18:03:43 +02:00
this.askDelete(item.type, item.id)
},
childActionDelete({type, id}) {
this.askDelete(type, id)
},
askDelete(type, id) {
this.confirmModal = {id, type}
this.$bvModal.show("delete-confirm-modal")
},
performDelete(type, id) {
return new Promise((resolve, reject) => {
this.actionPending = true
this.$api.delete(`/model/${type}/${id}`).then(() => { // this resets actionPending
this.updateModelList().then(() => {
this.$showToast("Model deleted.", "success")
resolve()
}, (error) => {
if (!error.response) {
this.$showToast("Some network error happened.\nCheck logs!")
} else {
this.$showToast(`The server returned error: ${error.response.status} ${error.response.statusText}\n${error.response.data}`)
}
reject()
})
}).catch((error) => {
if (!error.response) {
// network error happened
this.$showToast("Some network error happened.\nCheck logs!")
} else {
// server returned bruh moment
this.$showToast(`The server returned error: ${error.response.status} ${error.response.statusText}\n${error.response.data}`)
}
this.actionPending = false
reject()
})
});
2021-08-09 17:50:56 +02:00
},
actionInfo({item}) {
2021-08-10 18:03:43 +02:00
this.infoModal.id = item.id
this.infoModal.type = item.type
this.infoModal.show = true
2021-08-09 17:42:08 +02:00
}
},
mounted() {
this.updateModelList()
}
2021-08-09 16:15:55 +02:00
}
</script>
<style scoped>
</style>