refactor: use doc in activity & child components

This commit is contained in:
Shariq Ansari 2025-07-30 16:11:49 +05:30
parent 0605cf5fd0
commit af830b8782
7 changed files with 55 additions and 69 deletions

View File

@ -367,7 +367,7 @@
<div v-else-if="title == 'Data'" class="h-full flex flex-col px-3 sm:px-10"> <div v-else-if="title == 'Data'" class="h-full flex flex-col px-3 sm:px-10">
<DataFields <DataFields
:doctype="doctype" :doctype="doctype"
:docname="doc.data.name" :docname="docname"
@beforeSave="(data) => emit('beforeSave', data)" @beforeSave="(data) => emit('beforeSave', data)"
@afterSave="(data) => emit('afterSave', data)" @afterSave="(data) => emit('afterSave', data)"
/> />
@ -438,10 +438,9 @@
:doc="doc" :doc="doc"
/> />
<FilesUploader <FilesUploader
v-if="doc.data?.name"
v-model="showFilesUploader" v-model="showFilesUploader"
:doctype="doctype" :doctype="doctype"
:docname="doc.data.name" :docname="docname"
@after=" @after="
() => { () => {
all_activities.reload() all_activities.reload()
@ -514,6 +513,10 @@ const props = defineProps({
type: String, type: String,
default: 'CRM Lead', default: 'CRM Lead',
}, },
docname: {
type: String,
default: '',
},
tabs: { tabs: {
type: Array, type: Array,
default: () => [], default: () => [],
@ -524,11 +527,12 @@ const emit = defineEmits(['beforeSave', 'afterSave'])
const route = useRoute() const route = useRoute()
const doc = defineModel()
const reload = defineModel('reload') const reload = defineModel('reload')
const tabIndex = defineModel('tabIndex') const tabIndex = defineModel('tabIndex')
const { document: _document } = useDocument(props.doctype, doc.value.data.name) const { document: _document } = useDocument(props.doctype, props.docname)
const doc = computed(() => _document.doc || {})
const reload_email = ref(false) const reload_email = ref(false)
const modalRef = ref(null) const modalRef = ref(null)
@ -545,8 +549,8 @@ const changeTabTo = (tabName) => {
const all_activities = createResource({ const all_activities = createResource({
url: 'crm.api.activities.get_activities', url: 'crm.api.activities.get_activities',
params: { name: doc.value.data.name }, params: { name: props.docname },
cache: ['activity', doc.value.data.name], cache: ['activity', props.docname],
auto: true, auto: true,
transform: ([versions, calls, notes, tasks, attachments]) => { transform: ([versions, calls, notes, tasks, attachments]) => {
return { versions, calls, notes, tasks, attachments } return { versions, calls, notes, tasks, attachments }
@ -557,10 +561,10 @@ const showWhatsappTemplates = ref(false)
const whatsappMessages = createResource({ const whatsappMessages = createResource({
url: 'crm.api.whatsapp.get_whatsapp_messages', url: 'crm.api.whatsapp.get_whatsapp_messages',
cache: ['whatsapp_messages', doc.value.data.name], cache: ['whatsapp_messages', props.docname],
params: { params: {
reference_doctype: props.doctype, reference_doctype: props.doctype,
reference_name: doc.value.data.name, reference_name: props.docname,
}, },
auto: true, auto: true,
transform: (data) => sortByCreation(data), transform: (data) => sortByCreation(data),
@ -575,7 +579,7 @@ onMounted(() => {
$socket.on('whatsapp_message', (data) => { $socket.on('whatsapp_message', (data) => {
if ( if (
data.reference_doctype === props.doctype && data.reference_doctype === props.doctype &&
data.reference_name === doc.value.data.name data.reference_name === props.docname
) { ) {
whatsappMessages.reload() whatsappMessages.reload()
} }
@ -597,8 +601,8 @@ function sendTemplate(template) {
url: 'crm.api.whatsapp.send_whatsapp_template', url: 'crm.api.whatsapp.send_whatsapp_template',
params: { params: {
reference_doctype: props.doctype, reference_doctype: props.doctype,
reference_name: doc.value.data.name, reference_name: props.docname,
to: doc.value.data.mobile_no, to: doc.value.mobile_no,
template, template,
}, },
auto: true, auto: true,
@ -801,7 +805,7 @@ const callActions = computed(() => {
}, },
{ {
label: __('Make a Call'), label: __('Make a Call'),
onClick: () => makeCall(doc.data.mobile_no), onClick: () => makeCall(doc.value.mobile_no),
condition: () => callEnabled.value, condition: () => callEnabled.value,
}, },
] ]

View File

@ -140,7 +140,7 @@ const defaultActions = computed(() => {
{ {
icon: h(PhoneIcon, { class: 'h-4 w-4' }), icon: h(PhoneIcon, { class: 'h-4 w-4' }),
label: __('Make a Call'), label: __('Make a Call'),
onClick: () => makeCall(props.doc.data.mobile_no), onClick: () => makeCall(props.doc.mobile_no),
condition: () => callEnabled.value, condition: () => callEnabled.value,
}, },
{ {
@ -184,7 +184,7 @@ const callActions = computed(() => {
{ {
label: __('Make a Call'), label: __('Make a Call'),
icon: h(PhoneIcon, { class: 'h-4 w-4' }), icon: h(PhoneIcon, { class: 'h-4 w-4' }),
onClick: () => makeCall(props.doc.data.mobile_no), onClick: () => makeCall(props.doc.mobile_no),
condition: () => callEnabled.value, condition: () => callEnabled.value,
}, },
] ]

View File

@ -4,7 +4,7 @@
v-model:reloadTasks="activities" v-model:reloadTasks="activities"
:task="task" :task="task"
:doctype="doctype" :doctype="doctype"
:doc="doc.data?.name" :doc="doc?.name"
@after="redirect('tasks')" @after="redirect('tasks')"
/> />
<NoteModal <NoteModal
@ -12,7 +12,7 @@
v-model:reloadNotes="activities" v-model:reloadNotes="activities"
:note="note" :note="note"
:doctype="doctype" :doctype="doctype"
:doc="doc.data?.name" :doc="doc?.name"
@after="redirect('notes')" @after="redirect('notes')"
/> />
<CallLogModal <CallLogModal
@ -92,8 +92,8 @@ const referenceDoc = ref({})
function createCallLog() { function createCallLog() {
let doctype = props.doctype let doctype = props.doctype
let docname = props.doc.data?.name let docname = props.doc?.name
referenceDoc.value = { ...props.doc.data } referenceDoc.value = { ...props.doc }
callLog.value = { callLog.value = {
reference_doctype: doctype, reference_doctype: doctype,
reference_docname: docname, reference_docname: docname,

View File

@ -107,9 +107,9 @@ function sendTextMessage(event) {
async function sendWhatsAppMessage() { async function sendWhatsAppMessage() {
let args = { let args = {
reference_doctype: props.doctype, reference_doctype: props.doctype,
reference_name: doc.value.data.name, reference_name: doc.value.name,
message: content.value, message: content.value,
to: doc.value.data.mobile_no, to: doc.value.mobile_no,
attach: whatsapp.value.attach || '', attach: whatsapp.value.attach || '',
reply_to: reply.value?.name || '', reply_to: reply.value?.name || '',
content_type: whatsapp.value.content_type, content_type: whatsapp.value.content_type,

View File

@ -45,7 +45,7 @@
onClick: () => { onClick: () => {
showEmailBox = false showEmailBox = false
newEmailEditor.subject = subject newEmailEditor.subject = subject
newEmailEditor.toEmails = doc.data.email ? [doc.data.email] : [] newEmailEditor.toEmails = doc.email ? [doc.email] : []
newEmailEditor.ccEmails = [] newEmailEditor.ccEmails = []
newEmailEditor.bccEmails = [] newEmailEditor.bccEmails = []
newEmailEditor.cc = false newEmailEditor.cc = false
@ -54,7 +54,7 @@
}, },
}" }"
:editable="showEmailBox" :editable="showEmailBox"
v-model="doc.data" v-model="doc"
v-model:attachments="attachments" v-model:attachments="attachments"
:doctype="doctype" :doctype="doctype"
:subject="subject" :subject="subject"
@ -79,7 +79,7 @@
}, },
}" }"
:editable="showCommentBox" :editable="showCommentBox"
v-model="doc.data" v-model="doc"
v-model:attachments="attachments" v-model:attachments="attachments"
:doctype="doctype" :doctype="doctype"
:placeholder="__('@John, can you please check this?')" :placeholder="__('@John, can you please check this?')"
@ -125,12 +125,12 @@ const attachments = ref([])
const subject = computed(() => { const subject = computed(() => {
let prefix = '' let prefix = ''
if (doc.value.data?.lead_name) { if (doc.value?.lead_name) {
prefix = doc.value.data.lead_name prefix = doc.value.lead_name
} else if (doc.value.data?.organization) { } else if (doc.value?.organization) {
prefix = doc.value.data.organization prefix = doc.value.organization
} }
return `${prefix} (#${doc.value.data.name})` return `${prefix} (#${doc.value.name})`
}) })
const signature = createResource({ const signature = createResource({
@ -199,7 +199,7 @@ async function sendMail() {
subject: subject, subject: subject,
content: newEmail.value, content: newEmail.value,
doctype: props.doctype, doctype: props.doctype,
name: doc.value.data.name, name: doc.value.name,
send_email: 1, send_email: 1,
sender: getUser().email, sender: getUser().email,
sender_full_name: getUser()?.full_name || undefined, sender_full_name: getUser()?.full_name || undefined,
@ -209,7 +209,7 @@ async function sendMail() {
async function sendComment() { async function sendComment() {
let comment = await call('frappe.desk.form.utils.add_comment', { let comment = await call('frappe.desk.form.utils.add_comment', {
reference_doctype: props.doctype, reference_doctype: props.doctype,
reference_name: doc.value.data.name, reference_name: doc.value.name,
content: newComment.value, content: newComment.value,
comment_email: getUser().email, comment_email: getUser().email,
comment_by: getUser()?.full_name || undefined, comment_by: getUser()?.full_name || undefined,

View File

@ -43,16 +43,16 @@
</Dropdown> </Dropdown>
</template> </template>
</LayoutHeader> </LayoutHeader>
<div v-if="deal.data" class="flex h-full overflow-hidden"> <div v-if="doc.name" class="flex h-full overflow-hidden">
<Tabs as="div" v-model="tabIndex" :tabs="tabs"> <Tabs as="div" v-model="tabIndex" :tabs="tabs">
<template #tab-panel> <template #tab-panel>
<Activities <Activities
ref="activities" ref="activities"
doctype="CRM Deal" doctype="CRM Deal"
:docname="doc.name"
:tabs="tabs" :tabs="tabs"
v-model:reload="reload" v-model:reload="reload"
v-model:tabIndex="tabIndex" v-model:tabIndex="tabIndex"
v-model="deal"
@beforeSave="beforeStatusChange" @beforeSave="beforeStatusChange"
@afterSave="reloadAssignees" @afterSave="reloadAssignees"
/> />
@ -72,12 +72,12 @@
size="3xl" size="3xl"
class="size-12" class="size-12"
:label="title" :label="title"
:image="organization.data?.organization_logo" :image="organization?.organization_logo"
/> />
</div> </div>
</Tooltip> </Tooltip>
<div class="flex flex-col gap-2.5 truncate text-ink-gray-9"> <div class="flex flex-col gap-2.5 truncate text-ink-gray-9">
<Tooltip :text="organization.data?.name || __('Set an organization')"> <Tooltip :text="organization?.name || __('Set an organization')">
<div class="truncate text-2xl font-medium"> <div class="truncate text-2xl font-medium">
{{ title }} {{ title }}
</div> </div>
@ -462,35 +462,28 @@ watch(
{ once: true }, { once: true },
) )
const deal = createResource({ const organizationDocument = ref(null)
url: 'crm.fcrm.doctype.crm_deal.api.get_deal',
params: { name: props.dealId }, watch(
cache: ['deal', props.dealId], () => doc.value.organization,
onSuccess: (data) => { (org) => {
if (data.organization) { if (org && !organizationDocument.value?.doc) {
organization.update({ let { document: _organizationDocument } = useDocument(
params: { doctype: 'CRM Organization', name: data.organization }, 'CRM Organization',
}) org,
organization.fetch() )
organizationDocument.value = _organizationDocument
} }
}, },
}) { immediate: true },
)
const organization = createResource({ const organization = computed(() => organizationDocument.value?.doc || {})
url: 'frappe.client.get',
onSuccess: (data) => (deal.data._organizationObj = data),
})
onMounted(() => { onMounted(() => {
$socket.on('crm_customer_created', () => { $socket.on('crm_customer_created', () => {
toast.success(__('Customer created successfully')) toast.success(__('Customer created successfully'))
}) })
if (deal.data) {
organization.data = deal.data._organizationObj
return
}
deal.fetch()
}) })
onBeforeUnmount(() => { onBeforeUnmount(() => {

View File

@ -48,16 +48,16 @@
/> />
</template> </template>
</LayoutHeader> </LayoutHeader>
<div v-if="lead.data" class="flex h-full overflow-hidden"> <div v-if="doc.name" class="flex h-full overflow-hidden">
<Tabs as="div" v-model="tabIndex" :tabs="tabs"> <Tabs as="div" v-model="tabIndex" :tabs="tabs">
<template #tab-panel> <template #tab-panel>
<Activities <Activities
ref="activities" ref="activities"
doctype="CRM Lead" doctype="CRM Lead"
:docname="doc.name"
:tabs="tabs" :tabs="tabs"
v-model:reload="reload" v-model:reload="reload"
v-model:tabIndex="tabIndex" v-model:tabIndex="tabIndex"
v-model="lead"
@beforeSave="saveChanges" @beforeSave="saveChanges"
@afterSave="reloadAssignees" @afterSave="reloadAssignees"
/> />
@ -362,17 +362,6 @@ watch(
{ once: true }, { once: true },
) )
const lead = createResource({
url: 'crm.fcrm.doctype.crm_lead.api.get_lead',
params: { name: props.leadId },
cache: ['lead', props.leadId],
})
onMounted(() => {
if (lead.data) return
lead.fetch()
})
const reload = ref(false) const reload = ref(false)
const showFilesUploader = ref(false) const showFilesUploader = ref(false)