Chamilo is a learning management system focused on ease of use and accessibility
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
chamilo-lms/assets/vue/composables/datatableUpdate.js

92 lines
1.7 KiB

10 months ago
import { computed, ref, watch } from "vue"
import { useStore } from "vuex"
import { useRoute } from "vue-router"
import { isEmpty } from "lodash"
import { useI18n } from "vue-i18n"
import { useToast } from "primevue/usetoast"
10 months ago
export function useDatatableUpdate(servicePrefix) {
const moduleName = servicePrefix.toLowerCase()
10 months ago
const store = useStore()
const route = useRoute()
const { t } = useI18n()
10 months ago
const toast = useToast()
10 months ago
const isLoading = computed(() => store.getters[`${moduleName}/isLoading`])
10 months ago
const item = ref({})
10 months ago
async function retrieve() {
let id = route.params.id
10 months ago
if (isEmpty(id)) {
id = route.query.id
}
10 months ago
if (isEmpty(id)) {
return
}
10 months ago
await store.dispatch(`${moduleName}/load`, decodeURIComponent(id))
}
10 months ago
const retrievedItem = computed(() => {
let id = route.params.id
10 months ago
if (isEmpty(id)) {
id = route.query.id
}
10 months ago
if (isEmpty(id)) {
return null
}
10 months ago
return store.getters[`${moduleName}/find`](id)
})
10 months ago
watch(retrievedItem, (newValue) => {
item.value = newValue
})
10 months ago
async function updateItem(item) {
await store.dispatch(`${moduleName}/update`, item)
}
10 months ago
const updated = computed(() => store.state[moduleName].updated)
10 months ago
watch(updated, (newValue) => {
if (!newValue) {
return
}
10 months ago
onUpdated(item)
})
async function updateItemWithFormData(item) {
await store.dispatch(`${moduleName}/updateWithFormData`, item)
}
function onUpdated(item) {
toast.add({
severity: "success",
detail: t("{resource} updated", {
resource: item["@id"],
}),
life: 3500,
})
}
return {
isLoading,
item,
retrieve,
retrievedItem,
updateItem,
updateItemWithFormData,
updated,
onUpdated,
}
}