From c4feed116dcccdf23cc346557c36b3e719111e29 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Wed, 4 Jun 2025 19:00:57 +0530 Subject: [PATCH] fix: handle new document for lead/deal/contact/organization --- .../src/components/Modals/ContactModal.vue | 42 +++++---- frontend/src/components/Modals/DealModal.vue | 79 ++++++++--------- frontend/src/components/Modals/LeadModal.vue | 83 ++++++++--------- .../components/Modals/OrganizationModal.vue | 88 +++++++++++-------- frontend/src/pages/Deal.vue | 1 + frontend/src/pages/MobileDeal.vue | 1 + 6 files changed, 145 insertions(+), 149 deletions(-) diff --git a/frontend/src/components/Modals/ContactModal.vue b/frontend/src/components/Modals/ContactModal.vue index 840fa14f..9238b422 100644 --- a/frontend/src/components/Modals/ContactModal.vue +++ b/frontend/src/components/Modals/ContactModal.vue @@ -25,7 +25,7 @@ @@ -49,9 +49,10 @@ import FieldLayout from '@/components/FieldLayout/FieldLayout.vue' import EditIcon from '@/components/Icons/EditIcon.vue' import { usersStore } from '@/stores/users' import { isMobileView } from '@/composables/settings' +import { useDocument } from '@/data/document' import { capture } from '@/telemetry' import { call, createResource } from 'frappe-ui' -import { ref, nextTick, watch } from 'vue' +import { ref, nextTick, onMounted } from 'vue' import { useRouter } from 'vue-router' const props = defineProps({ @@ -77,23 +78,27 @@ const show = defineModel() const loading = ref(false) -let _contact = ref({}) +const { document: _contact } = useDocument('Contact') + +if (Object.keys(_contact.doc).length != 0) { + _contact.doc = {} +} async function createContact() { - if (_contact.value.email_id) { - _contact.value.email_ids = [{ email_id: _contact.value.email_id }] - delete _contact.value.email_id + if (_contact.doc.email_id) { + _contact.doc.email_ids = [{ email_id: _contact.doc.email_id }] + delete _contact.doc.email_id } - if (_contact.value.mobile_no) { - _contact.value.phone_nos = [{ phone: _contact.value.mobile_no }] - delete _contact.value.mobile_no + if (_contact.doc.mobile_no) { + _contact.doc.phone_nos = [{ phone: _contact.doc.mobile_no }] + delete _contact.doc.mobile_no } const doc = await call('frappe.client.insert', { doc: { doctype: 'Contact', - ..._contact.value, + ..._contact.doc, }, }) if (doc.name) { @@ -130,7 +135,7 @@ const tabs = createResource({ field.read_only = false } else if (field.fieldname == 'address') { field.create = (value, close) => { - _contact.value.address = value + _contact.doc.address = value emit('openAddressModal') show.value = false close() @@ -140,7 +145,7 @@ const tabs = createResource({ show.value = false } } else if (field.fieldtype === 'Table') { - _contact.value[field.fieldname] = [] + _contact.doc[field.fieldname] = [] } }) }) @@ -149,16 +154,9 @@ const tabs = createResource({ }, }) -watch( - () => show.value, - (value) => { - if (!value) return - nextTick(() => { - _contact.value = { ...props.contact.data } - }) - }, -) - +onMounted(() => { + Object.assign(_contact.doc, props.contact.data || props.contact || {}) +}) const showQuickEntryModal = defineModel('showQuickEntryModal') function openQuickEntryModal() { diff --git a/frontend/src/components/Modals/DealModal.vue b/frontend/src/components/Modals/DealModal.vue index 7bd0b592..bf08bdd0 100644 --- a/frontend/src/components/Modals/DealModal.vue +++ b/frontend/src/components/Modals/DealModal.vue @@ -50,7 +50,7 @@ ref="fieldLayoutRef" v-if="tabs.data?.length" :tabs="tabs.data" - :data="deal" + :data="deal.doc" doctype="CRM Deal" /> @@ -76,9 +76,10 @@ import FieldLayout from '@/components/FieldLayout/FieldLayout.vue' import { usersStore } from '@/stores/users' import { statusesStore } from '@/stores/statuses' import { isMobileView } from '@/composables/settings' +import { useDocument } from '@/data/document' import { capture } from '@/telemetry' import { Switch, createResource } from 'frappe-ui' -import { computed, ref, reactive, onMounted, nextTick, watch } from 'vue' +import { computed, ref, onMounted, nextTick, watch } from 'vue' import { useRouter } from 'vue-router' const props = defineProps({ @@ -92,24 +93,11 @@ const show = defineModel() const router = useRouter() const error = ref(null) -const deal = reactive({ - organization: '', - organization_name: '', - website: '', - no_of_employees: '', - territory: '', - annual_revenue: '', - industry: '', - contact: '', - salutation: '', - first_name: '', - last_name: '', - email: '', - mobile_no: '', - gender: '', - status: '', - deal_owner: '', -}) +const { document: deal } = useDocument('CRM Deal') + +if (Object.keys(deal.doc).length != 0) { + deal.doc = {} +} const hasOrganizationSections = ref(true) const hasContactSections = ref(true) @@ -165,11 +153,11 @@ const tabs = createResource({ if (field.fieldname == 'status') { field.fieldtype = 'Select' field.options = dealStatuses.value - field.prefix = getDealStatus(deal.status).color + field.prefix = getDealStatus(deal.doc.status).color } if (field.fieldtype === 'Table') { - deal[field.fieldname] = [] + deal.doc[field.fieldname] = [] } }) }) @@ -180,46 +168,49 @@ const tabs = createResource({ const dealStatuses = computed(() => { let statuses = statusOptions('deal') - if (!deal.status) { - deal.status = statuses[0].value + if (!deal.doc.status) { + deal.doc.status = statuses[0].value } return statuses }) function createDeal() { - if (deal.website && !deal.website.startsWith('http')) { - deal.website = 'https://' + deal.website + if (deal.doc.website && !deal.doc.website.startsWith('http')) { + deal.doc.website = 'https://' + deal.doc.website } if (chooseExistingContact.value) { - deal['first_name'] = null - deal['last_name'] = null - deal['email'] = null - deal['mobile_no'] = null - } else deal['contact'] = null + deal.doc['first_name'] = null + deal.doc['last_name'] = null + deal.doc['email'] = null + deal.doc['mobile_no'] = null + } else deal.doc['contact'] = null createResource({ url: 'crm.fcrm.doctype.crm_deal.crm_deal.create_deal', - params: { args: deal }, + params: { args: deal.doc }, auto: true, validate() { error.value = null - if (deal.annual_revenue) { - if (typeof deal.annual_revenue === 'string') { - deal.annual_revenue = deal.annual_revenue.replace(/,/g, '') - } else if (isNaN(deal.annual_revenue)) { + if (deal.doc.annual_revenue) { + if (typeof deal.doc.annual_revenue === 'string') { + deal.doc.annual_revenue = deal.doc.annual_revenue.replace(/,/g, '') + } else if (isNaN(deal.doc.annual_revenue)) { error.value = __('Annual Revenue should be a number') return error.value } } - if (deal.mobile_no && isNaN(deal.mobile_no.replace(/[-+() ]/g, ''))) { + if ( + deal.doc.mobile_no && + isNaN(deal.doc.mobile_no.replace(/[-+() ]/g, '')) + ) { error.value = __('Mobile No should be a number') return error.value } - if (deal.email && !deal.email.includes('@')) { + if (deal.doc.email && !deal.doc.email.includes('@')) { error.value = __('Invalid Email') return error.value } - if (!deal.status) { + if (!deal.doc.status) { error.value = __('Status is required') return error.value } @@ -252,12 +243,12 @@ function openQuickEntryModal() { } onMounted(() => { - Object.assign(deal, props.defaults) - if (!deal.deal_owner) { - deal.deal_owner = getUser().name + Object.assign(deal.doc, props.defaults) + if (!deal.doc.deal_owner) { + deal.doc.deal_owner = getUser().name } - if (!deal.status && dealStatuses.value[0].value) { - deal.status = dealStatuses.value[0].value + if (!deal.doc.status && dealStatuses.value[0].value) { + deal.doc.status = dealStatuses.value[0].value } }) diff --git a/frontend/src/components/Modals/LeadModal.vue b/frontend/src/components/Modals/LeadModal.vue index 0950b401..f0aab83b 100644 --- a/frontend/src/components/Modals/LeadModal.vue +++ b/frontend/src/components/Modals/LeadModal.vue @@ -23,7 +23,7 @@
- +
@@ -51,7 +51,8 @@ import { isMobileView } from '@/composables/settings' import { capture } from '@/telemetry' import { createResource } from 'frappe-ui' import { useOnboarding } from 'frappe-ui/frappe' -import { computed, onMounted, ref, reactive, nextTick } from 'vue' +import { useDocument } from '@/data/document' +import { computed, onMounted, ref, nextTick } from 'vue' import { useRouter } from 'vue-router' const props = defineProps({ @@ -68,6 +69,20 @@ const router = useRouter() const error = ref(null) const isLeadCreating = ref(false) +const { document: lead } = useDocument('CRM Lead') + +if (Object.keys(lead.doc).length != 0) { + lead.doc = {} +} + +const leadStatuses = computed(() => { + let statuses = statusOptions('lead') + if (!lead.doc.status) { + lead.doc.status = statuses?.[0]?.value + } + return statuses +}) + const tabs = createResource({ url: 'crm.fcrm.doctype.crm_fields_layout.crm_fields_layout.get_fields_layout', cache: ['QuickEntry', 'CRM Lead'], @@ -81,11 +96,11 @@ const tabs = createResource({ if (field.fieldname == 'status') { field.fieldtype = 'Select' field.options = leadStatuses.value - field.prefix = getLeadStatus(lead.status).color + field.prefix = getLeadStatus(lead.doc.status).color } if (field.fieldtype === 'Table') { - lead[field.fieldname] = [] + lead.doc[field.fieldname] = [] } }) }) @@ -94,23 +109,6 @@ const tabs = createResource({ }, }) -const lead = reactive({ - salutation: '', - first_name: '', - last_name: '', - email: '', - mobile_no: '', - gender: '', - organization: '', - website: '', - no_of_employees: '', - territory: '', - annual_revenue: '', - industry: '', - status: '', - lead_owner: '', -}) - const createLead = createResource({ url: 'frappe.client.insert', makeParams(values) { @@ -123,43 +121,38 @@ const createLead = createResource({ }, }) -const leadStatuses = computed(() => { - let statuses = statusOptions('lead') - if (!lead.status) { - lead.status = statuses?.[0]?.value - } - return statuses -}) - function createNewLead() { - if (lead.website && !lead.website.startsWith('http')) { - lead.website = 'https://' + lead.website + if (lead.doc.website && !lead.doc.website.startsWith('http')) { + lead.doc.website = 'https://' + lead.doc.website } - createLead.submit(lead, { + createLead.submit(lead.doc, { validate() { error.value = null - if (!lead.first_name) { + if (!lead.doc.first_name) { error.value = __('First Name is mandatory') return error.value } - if (lead.annual_revenue) { - if (typeof lead.annual_revenue === 'string') { - lead.annual_revenue = lead.annual_revenue.replace(/,/g, '') - } else if (isNaN(lead.annual_revenue)) { + if (lead.doc.annual_revenue) { + if (typeof lead.doc.annual_revenue === 'string') { + lead.doc.annual_revenue = lead.doc.annual_revenue.replace(/,/g, '') + } else if (isNaN(lead.doc.annual_revenue)) { error.value = __('Annual Revenue should be a number') return error.value } } - if (lead.mobile_no && isNaN(lead.mobile_no.replace(/[-+() ]/g, ''))) { + if ( + lead.doc.mobile_no && + isNaN(lead.doc.mobile_no.replace(/[-+() ]/g, '')) + ) { error.value = __('Mobile No should be a number') return error.value } - if (lead.email && !lead.email.includes('@')) { + if (lead.doc.email && !lead.doc.email.includes('@')) { error.value = __('Invalid Email') return error.value } - if (!lead.status) { + if (!lead.doc.status) { error.value = __('Status is required') return error.value } @@ -195,12 +188,12 @@ function openQuickEntryModal() { } onMounted(() => { - Object.assign(lead, props.defaults) - if (!lead.lead_owner) { - lead.lead_owner = getUser().name + Object.assign(lead.doc, props.defaults) + if (!lead.doc?.lead_owner) { + lead.doc.lead_owner = getUser().name } - if (!lead.status && leadStatuses.value[0]?.value) { - lead.status = leadStatuses.value[0].value + if (!lead.doc?.status && leadStatuses.value[0]?.value) { + lead.doc.status = leadStatuses.value[0].value } }) diff --git a/frontend/src/components/Modals/OrganizationModal.vue b/frontend/src/components/Modals/OrganizationModal.vue index 40105697..e652f6a6 100644 --- a/frontend/src/components/Modals/OrganizationModal.vue +++ b/frontend/src/components/Modals/OrganizationModal.vue @@ -9,7 +9,12 @@
-
- +
-
@@ -34,9 +50,10 @@ import FieldLayout from '@/components/FieldLayout/FieldLayout.vue' import EditIcon from '@/components/Icons/EditIcon.vue' import { usersStore } from '@/stores/users' import { isMobileView } from '@/composables/settings' +import { useDocument } from '@/data/document' import { capture } from '@/telemetry' import { call, FeatherIcon, createResource } from 'frappe-ui' -import { ref, nextTick, watch } from 'vue' +import { ref, nextTick, onMounted } from 'vue' import { useRouter } from 'vue-router' const props = defineProps({ @@ -44,7 +61,7 @@ const props = defineProps({ type: Object, default: { redirect: true, - afterInsert: () => { }, + afterInsert: () => {}, }, }, }) @@ -60,30 +77,32 @@ const organization = defineModel('organization') const loading = ref(false) const title = ref(null) -let _organization = ref({ - organization_name: '', - website: '', - annual_revenue: '', - no_of_employees: '1-10', - industry: '', -}) +const { document: _organization } = useDocument('CRM Organization') + +if (Object.keys(_organization.doc).length != 0) { + _organization.doc = { no_of_employees: '1-10' } +} let doc = ref({}) const error = ref(null) async function createOrganization() { - const doc = await call('frappe.client.insert', { - doc: { - doctype: 'CRM Organization', - ..._organization.value, + const doc = await call( + 'frappe.client.insert', + { + doc: { + doctype: 'CRM Organization', + ..._organization.doc, + }, }, - }, { - onError: (err) => { - if (err.error.exc_type == 'ValidationError') { - error.value = err.error?.messages?.[0] - } - } - }) + { + onError: (err) => { + if (err.error.exc_type == 'ValidationError') { + error.value = err.error?.messages?.[0] + } + }, + }, + ) loading.value = false if (doc.name) { capture('organization_created') @@ -116,7 +135,7 @@ const tabs = createResource({ column.fields.forEach((field) => { if (field.fieldname == 'address') { field.create = (value, close) => { - _organization.value.address = value + _organization.doc.address = value emit('openAddressModal') show.value = false close() @@ -126,7 +145,7 @@ const tabs = createResource({ show.value = false } } else if (field.fieldtype === 'Table') { - _organization.value[field.fieldname] = [] + _organization.doc[field.fieldname] = [] } }) }) @@ -135,19 +154,12 @@ const tabs = createResource({ }, }) -watch( - () => show.value, - (value) => { - if (!value) return - nextTick(() => { - // TODO: Issue with FormControl - // title.value.el.focus() - doc.value = organization.value?.doc || organization.value || {} - _organization.value = { ...doc.value } - }) - }, -) - +onMounted(() => { + Object.assign( + _organization.doc, + organization.value?.doc || organization.value || {}, + ) +}) const showQuickEntryModal = defineModel('showQuickEntryModal') function openQuickEntryModal() { diff --git a/frontend/src/pages/Deal.vue b/frontend/src/pages/Deal.vue index e61c0205..454565c0 100644 --- a/frontend/src/pages/Deal.vue +++ b/frontend/src/pages/Deal.vue @@ -280,6 +280,7 @@ }" />