88 lines
2.1 KiB
TypeScript
88 lines
2.1 KiB
TypeScript
import { ref } from 'vue'
|
|
|
|
// Services
|
|
import { postSourceDevice, putSourceDevice, removeSourceDevice } from '~/services/device.service'
|
|
|
|
const recId = ref<number>(0)
|
|
const recAction = ref<string>('')
|
|
const recItem = ref<any>(null)
|
|
const isProcessing = ref(false)
|
|
const isFormEntryDialogOpen = ref(false)
|
|
const isRecordConfirmationOpen = ref(false)
|
|
|
|
function onResetState() {
|
|
recId.value = 0
|
|
recAction.value = ''
|
|
recItem.value = null
|
|
}
|
|
|
|
export async function handleActionSave(values: any, refresh: () => void, reset: () => void) {
|
|
let isSuccess = false
|
|
isProcessing.value = true
|
|
try {
|
|
const result = await postSourceDevice(values)
|
|
if (result.success) {
|
|
isFormEntryDialogOpen.value = false
|
|
isSuccess = true
|
|
if (refresh) refresh()
|
|
}
|
|
} catch (error) {
|
|
console.warn('Error saving form:', error)
|
|
isSuccess = false
|
|
} finally {
|
|
if (isSuccess) {
|
|
setTimeout(() => {
|
|
reset()
|
|
}, 500)
|
|
}
|
|
isProcessing.value = false
|
|
}
|
|
}
|
|
|
|
export async function handleActionEdit(id: number | string, values: any, refresh: () => void, reset: () => void) {
|
|
let isSuccess = false
|
|
isProcessing.value = true
|
|
try {
|
|
const result = await putSourceDevice(id, values)
|
|
if (result.success) {
|
|
isFormEntryDialogOpen.value = false
|
|
isSuccess = true
|
|
if (refresh) refresh()
|
|
}
|
|
} catch (error) {
|
|
console.warn('Error editing form:', error)
|
|
isSuccess = false
|
|
} finally {
|
|
if (isSuccess) {
|
|
setTimeout(() => {
|
|
reset()
|
|
}, 500)
|
|
}
|
|
isProcessing.value = false
|
|
}
|
|
}
|
|
|
|
export async function handleActionRemove(id: number | string, refresh: () => void) {
|
|
isProcessing.value = true
|
|
try {
|
|
const result = await removeSourceDevice(id)
|
|
if (result.success) {
|
|
if (refresh) refresh()
|
|
}
|
|
} catch (error) {
|
|
console.error('Error deleting record:', error)
|
|
} finally {
|
|
onResetState()
|
|
isProcessing.value = false
|
|
}
|
|
}
|
|
|
|
export function handleCancelForm(reset: () => void) {
|
|
isFormEntryDialogOpen.value = false
|
|
setTimeout(() => {
|
|
reset()
|
|
}, 500)
|
|
}
|
|
|
|
export { recId, recAction, recItem, isProcessing, isFormEntryDialogOpen, isRecordConfirmationOpen }
|