fix: added on before create hook in all modals

(cherry picked from commit 2e5c1bc3b564a9c7e1dd49f521cd4ec36b202102)
This commit is contained in:
Shariq Ansari 2025-06-30 19:16:24 +05:30 committed by Mergify
parent d8f990ac8e
commit f6af8d562e
6 changed files with 108 additions and 77 deletions

View File

@ -84,7 +84,10 @@ const error = ref(null)
const title = ref(null) const title = ref(null)
const editMode = ref(false) const editMode = ref(false)
const { document: _address } = useDocument('Address', props.address || '') const { document: _address, triggerOnBeforeCreate } = useDocument(
'Address',
props.address || '',
)
const dialogOptions = computed(() => { const dialogOptions = computed(() => {
let title = !editMode.value let title = !editMode.value
@ -95,8 +98,7 @@ const dialogOptions = computed(() => {
{ {
label: editMode.value ? __('Save') : __('Create'), label: editMode.value ? __('Save') : __('Create'),
variant: 'solid', variant: 'solid',
onClick: () => onClick: () => (editMode.value ? updateAddress() : createAddress()),
editMode.value ? updateAddress() : createAddress.submit(),
}, },
] ]
@ -133,16 +135,22 @@ async function updateAddress() {
await _address.save.submit(null, callBacks) await _address.save.submit(null, callBacks)
} }
const createAddress = createResource({ async function createAddress() {
url: 'frappe.client.insert', loading.value = true
makeParams() { error.value = null
return {
await triggerOnBeforeCreate?.()
await _createAddress.submit({
doc: { doc: {
doctype: 'Address', doctype: 'Address',
..._address.doc, ..._address.doc,
}, },
})
} }
},
const _createAddress = createResource({
url: 'frappe.client.insert',
onSuccess(doc) { onSuccess(doc) {
loading.value = false loading.value = false
if (doc.name) { if (doc.name) {

View File

@ -86,7 +86,7 @@ const show = defineModel()
const loading = ref(false) const loading = ref(false)
const { document: _contact } = useDocument('Contact') const { document: _contact, triggerOnBeforeCreate } = useDocument('Contact')
async function createContact() { async function createContact() {
if (_contact.doc.email_id) { if (_contact.doc.email_id) {
@ -99,6 +99,8 @@ async function createContact() {
delete _contact.doc.mobile_no delete _contact.doc.mobile_no
} }
await triggerOnBeforeCreate?.()
const doc = await call('frappe.client.insert', { const doc = await call('frappe.client.insert', {
doc: { doc: {
doctype: 'Contact', doctype: 'Contact',

View File

@ -27,7 +27,7 @@
</div> </div>
</div> </div>
<div v-if="tabs.data"> <div v-if="tabs.data">
<FieldLayout :tabs="tabs.data" :data="_data" :doctype="doctype" /> <FieldLayout :tabs="tabs.data" :data="_data.doc" :doctype="doctype" />
<ErrorMessage class="mt-2" :message="error" /> <ErrorMessage class="mt-2" :message="error" />
</div> </div>
</div> </div>
@ -51,6 +51,7 @@
import FieldLayout from '@/components/FieldLayout/FieldLayout.vue' import FieldLayout from '@/components/FieldLayout/FieldLayout.vue'
import EditIcon from '@/components/Icons/EditIcon.vue' import EditIcon from '@/components/Icons/EditIcon.vue'
import { usersStore } from '@/stores/users' import { usersStore } from '@/stores/users'
import { useDocument } from '@/data/document'
import { isMobileView } from '@/composables/settings' import { isMobileView } from '@/composables/settings'
import { showQuickEntryModal, quickEntryProps } from '@/composables/modals' import { showQuickEntryModal, quickEntryProps } from '@/composables/modals'
import { FeatherIcon, createResource, ErrorMessage, call } from 'frappe-ui' import { FeatherIcon, createResource, ErrorMessage, call } from 'frappe-ui'
@ -76,7 +77,7 @@ const show = defineModel()
const loading = ref(false) const loading = ref(false)
const error = ref(null) const error = ref(null)
let _data = ref({}) const { document: _data, triggerOnBeforeCreate } = useDocument(props.doctype)
const dialogOptions = computed(() => { const dialogOptions = computed(() => {
let doctype = props.doctype let doctype = props.doctype
@ -109,12 +110,14 @@ async function create() {
loading.value = true loading.value = true
error.value = null error.value = null
await triggerOnBeforeCreate?.()
let doc = await call( let doc = await call(
'frappe.client.insert', 'frappe.client.insert',
{ {
doc: { doc: {
doctype: props.doctype, doctype: props.doctype,
..._data.value, ..._data.doc,
}, },
}, },
{ {
@ -138,7 +141,7 @@ watch(
if (!value) return if (!value) return
nextTick(() => { nextTick(() => {
_data.value = { ...props.data } _data.doc = { ...props.data }
}) })
}, },
) )

View File

@ -98,7 +98,11 @@ const show = defineModel()
const router = useRouter() const router = useRouter()
const error = ref(null) const error = ref(null)
const { document: deal, triggerOnChange } = useDocument('CRM Deal') const {
document: deal,
triggerOnChange,
triggerOnBeforeCreate,
} = useDocument('CRM Deal')
const hasOrganizationSections = ref(true) const hasOrganizationSections = ref(true)
const hasContactSections = ref(true) const hasContactSections = ref(true)
@ -175,7 +179,7 @@ const dealStatuses = computed(() => {
return statuses return statuses
}) })
function createDeal() { async function createDeal() {
if (deal.doc.website && !deal.doc.website.startsWith('http')) { if (deal.doc.website && !deal.doc.website.startsWith('http')) {
deal.doc.website = 'https://' + deal.doc.website deal.doc.website = 'https://' + deal.doc.website
} }
@ -186,6 +190,8 @@ function createDeal() {
deal.doc['mobile_no'] = null deal.doc['mobile_no'] = null
} else deal.doc['contact'] = null } else deal.doc['contact'] = null
await triggerOnBeforeCreate?.()
createResource({ createResource({
url: 'crm.fcrm.doctype.crm_deal.crm_deal.create_deal', url: 'crm.fcrm.doctype.crm_deal.crm_deal.create_deal',
params: { args: deal.doc }, params: { args: deal.doc },

View File

@ -74,7 +74,11 @@ const router = useRouter()
const error = ref(null) const error = ref(null)
const isLeadCreating = ref(false) const isLeadCreating = ref(false)
const { document: lead, triggerOnChange } = useDocument('CRM Lead') const {
document: lead,
triggerOnChange,
triggerOnBeforeCreate,
} = useDocument('CRM Lead')
const leadStatuses = computed(() => { const leadStatuses = computed(() => {
let statuses = statusOptions('lead', null, [], triggerOnChange) let statuses = statusOptions('lead', null, [], triggerOnChange)
@ -112,22 +116,23 @@ const tabs = createResource({
const createLead = createResource({ const createLead = createResource({
url: 'frappe.client.insert', url: 'frappe.client.insert',
makeParams(values) {
return {
doc: {
doctype: 'CRM Lead',
...values,
},
}
},
}) })
function createNewLead() { async function createNewLead() {
if (lead.doc.website && !lead.doc.website.startsWith('http')) { if (lead.doc.website && !lead.doc.website.startsWith('http')) {
lead.doc.website = 'https://' + lead.doc.website lead.doc.website = 'https://' + lead.doc.website
} }
createLead.submit(lead.doc, { await triggerOnBeforeCreate?.()
createLead.submit(
{
doc: {
doctype: 'CRM Lead',
...lead.doc,
},
},
{
validate() { validate() {
error.value = null error.value = null
if (!lead.doc.first_name) { if (!lead.doc.first_name) {
@ -176,7 +181,8 @@ function createNewLead() {
} }
error.value = err.messages.join('\n') error.value = err.messages.join('\n')
}, },
}) },
)
} }
function openQuickEntryModal() { function openQuickEntryModal() {

View File

@ -88,9 +88,15 @@ const show = defineModel()
const loading = ref(false) const loading = ref(false)
const error = ref(null) const error = ref(null)
const { document: organization } = useDocument('CRM Organization') const { document: organization, triggerOnBeforeCreate } =
useDocument('CRM Organization')
async function createOrganization() { async function createOrganization() {
loading.value = true
error.value = null
await triggerOnBeforeCreate?.()
const doc = await call( const doc = await call(
'frappe.client.insert', 'frappe.client.insert',
{ {