fix: dirty state of batch form

This commit is contained in:
Jannat Patel
2026-02-12 10:33:21 +05:30
parent f59eecd34e
commit 7ef8aad2c8

View File

@@ -23,19 +23,16 @@
v-model="batchDetail.doc.published" v-model="batchDetail.doc.published"
type="checkbox" type="checkbox"
:label="__('Published')" :label="__('Published')"
@change="makeFormDirty"
/> />
<FormControl <FormControl
v-model="batchDetail.doc.allow_self_enrollment" v-model="batchDetail.doc.allow_self_enrollment"
type="checkbox" type="checkbox"
:label="__('Allow Self Enrollment')" :label="__('Allow Self Enrollment')"
@change="makeFormDirty"
/> />
<FormControl <FormControl
v-model="batchDetail.doc.certification" v-model="batchDetail.doc.certification"
type="checkbox" type="checkbox"
:label="__('Certification')" :label="__('Certification')"
@change="makeFormDirty"
/> />
</div> </div>
</div> </div>
@@ -267,7 +264,9 @@ import {
onBeforeUnmount, onBeforeUnmount,
reactive, reactive,
ref, ref,
toRaw,
watch, watch,
nextTick,
} from 'vue' } from 'vue'
import { import {
FormControl, FormControl,
@@ -322,25 +321,19 @@ const props = defineProps({
onMounted(() => { onMounted(() => {
if (!user.data) window.location.href = '/login' if (!user.data) window.location.href = '/login'
if (props.batchName != 'new') { if (props.batchName != 'new') {
fetchBatchInfo()
} else { } else {
capture('batch_form_opened') capture('batch_form_opened')
} }
window.addEventListener('keydown', keyboardShortcut) window.addEventListener('keydown', keyboardShortcut)
}) })
const fetchBatchInfo = () => {
batchDetail.reload()
getMetaInfo('batches', props.batch?.data?.name, meta)
}
const keyboardShortcut = (e) => { const keyboardShortcut = (e) => {
if ( if (
e.key === 's' && e.key === 's' &&
(e.ctrlKey || e.metaKey) && (e.ctrlKey || e.metaKey) &&
!e.target.classList.contains('ProseMirror') !e.target.classList.contains('ProseMirror')
) { ) {
saveBatch() submitBatch()
e.preventDefault() e.preventDefault()
} }
} }
@@ -375,6 +368,8 @@ const batchDetail = createDocumentResource({
watch( watch(
() => batchDetail.doc, () => batchDetail.doc,
() => { () => {
if (!batchDetail.doc) return
console.log('watch batch detail')
getMetaInfo('batches', batchDetail.doc?.name, meta) getMetaInfo('batches', batchDetail.doc?.name, meta)
updateBatchData() updateBatchData()
} }
@@ -400,7 +395,7 @@ const updateBatchData = () => {
let key = checkboxes[idx] let key = checkboxes[idx]
batchDetail.doc[key] = batchDetail.doc[key] ? true : false batchDetail.doc[key] = batchDetail.doc[key] ? true : false
} }
originalDoc.value = batchDetail.doc originalDoc.value = structuredClone(toRaw(batchDetail.doc))
} }
const formatTime = (timeStr) => { const formatTime = (timeStr) => {
@@ -409,34 +404,16 @@ const formatTime = (timeStr) => {
return `${hours}:${minutes}` return `${hours}:${minutes}`
} }
const editBatch = createResource({
url: 'frappe.client.set_value',
makeParams(values) {
return {
doctype: 'LMS Batch',
name: props.batchName,
fieldname: {
meta_image: batch.meta_image,
video_link: batch.video_link,
instructors: instructors.value.map((instructor) => ({
instructor: instructor,
})),
...batch,
},
}
},
})
const validateFields = () => { const validateFields = () => {
batch.description = sanitizeHTML(batch.description) batchDetail.doc.description = sanitizeHTML(batchDetail.doc.description)
batch.batch_details = sanitizeHTML(batch.batch_details) batchDetail.doc.batch_details = sanitizeHTML(batchDetail.doc.batch_details)
Object.keys(batch).forEach((key) => { Object.keys(batchDetail.doc).forEach((key) => {
if ( if (
!['description', 'batch_details'].includes(key) && !['description', 'batch_details'].includes(key) &&
typeof batch[key] === 'string' typeof batchDetail.doc[key] === 'string'
) { ) {
batch[key] = escapeHTML(batch[key]) batchDetail.doc[key] = escapeHTML(batchDetail.doc[key])
} }
}) })
} }
@@ -447,7 +424,7 @@ const submitBatch = () => {
} }
const updateBatch = () => { const updateBatch = () => {
batchDetails.setValue.submit( batchDetail.setValue.submit(
{ {
...batchDetail.doc, ...batchDetail.doc,
instructors: instructors.value.map((instructor) => ({ instructors: instructors.value.map((instructor) => ({
@@ -458,7 +435,10 @@ const updateBatch = () => {
onSuccess(data) { onSuccess(data) {
updateMetaInfo('batches', data.name, meta) updateMetaInfo('batches', data.name, meta)
toast.success(__('Batch updated successfully')) toast.success(__('Batch updated successfully'))
isDirty.value = false nextTick(() => {
originalDoc.value = structuredClone(data)
isDirty.value = false
})
}, },
onError(err) { onError(err) {
toast.error(err.messages?.[0] || err) toast.error(err.messages?.[0] || err)
@@ -468,9 +448,16 @@ const updateBatch = () => {
) )
} }
const makeFormDirty = () => { watch(
isDirty.value = true () => batchDetail.doc,
} () => {
if (originalDoc.value) {
isDirty.value =
JSON.stringify(batchDetail.doc) !== JSON.stringify(originalDoc.value)
}
},
{ deep: true }
)
const deleteBatch = () => { const deleteBatch = () => {
$dialog({ $dialog({