1
0
forked from test/crm
jcrm/frontend/src/pages/Deal.vue
2023-11-11 13:35:39 +05:30

628 lines
21 KiB
Vue

<template>
<LayoutHeader v-if="deal.data">
<template #left-header>
<Breadcrumbs :items="breadcrumbs" />
</template>
<template #right-header>
<FormControl
type="autocomplete"
:options="activeAgents"
:value="getUser(deal.data.deal_owner).full_name"
@change="(option) => updateField('deal_owner', option.email)"
placeholder="Deal owner"
>
<template #prefix>
<UserAvatar class="mr-2" :user="deal.data.deal_owner" size="sm" />
</template>
<template #item-prefix="{ option }">
<UserAvatar class="mr-2" :user="option.email" size="sm" />
</template>
</FormControl>
<Dropdown
:options="statusDropdownOptions(deal.data, 'deal', updateField)"
>
<template #default="{ open }">
<Button :label="deal.data.status">
<template #prefix>
<IndicatorIcon :class="dealStatuses[deal.data.status].color" />
</template>
<template #suffix>
<FeatherIcon
:name="open ? 'chevron-up' : 'chevron-down'"
class="h-4 text-gray-600"
/>
</template>
</Button>
</template>
</Dropdown>
</template>
</LayoutHeader>
<div v-if="deal.data" class="flex h-full overflow-hidden">
<Tabs v-model="tabIndex" v-slot="{ tab }" :tabs="tabs">
<Activities
doctype="CRM Deal"
:title="tab.label"
v-model:reload="reload"
v-model="deal"
/>
</Tabs>
<div class="flex w-[352px] flex-col justify-between border-l">
<div
class="flex h-[41px] items-center border-b px-5 py-2.5 text-lg font-semibold"
>
About this deal
</div>
<div class="flex items-center justify-start gap-5 border-b p-5">
<Tooltip
text="Organization logo"
class="group relative h-[88px] w-[88px]"
>
<Avatar
size="3xl"
class="h-[88px] w-[88px]"
:label="organization?.name"
:image="organization?.organization_logo"
/>
</Tooltip>
<div class="flex flex-col gap-2.5 truncate">
<Tooltip :text="organization?.name">
<div class="truncate text-2xl font-medium">
{{ organization?.name }}
</div>
</Tooltip>
<div class="flex gap-1.5">
<Tooltip text="Make a call...">
<Button
class="h-7 w-7"
@click="() => makeCall(deal.data.mobile_no)"
>
<PhoneIcon class="h-4 w-4" />
</Button>
</Tooltip>
<Button class="h-7 w-7">
<EmailIcon class="h-4 w-4" />
</Button>
<Tooltip text="Go to website...">
<Button class="h-7 w-7">
<LinkIcon
class="h-4 w-4"
@click="openWebsite(deal.data.website)"
/>
</Button>
</Tooltip>
</div>
</div>
</div>
<div class="flex flex-1 flex-col justify-between overflow-hidden">
<div class="flex flex-col overflow-y-auto">
<div
v-for="(section, i) in detailSections"
:key="section.label"
class="flex flex-col p-3"
:class="{ 'border-b': i !== detailSections.length - 1 }"
>
<Toggler :is-opened="section.opened" v-slot="{ opened, toggle }">
<div class="flex items-center justify-between">
<div
class="flex h-7 max-w-fit cursor-pointer items-center gap-2 pl-2 pr-3 text-base font-semibold leading-5"
@click="toggle()"
>
<FeatherIcon
name="chevron-right"
class="h-4 text-gray-900 transition-all duration-300 ease-in-out"
:class="{ 'rotate-90': opened }"
/>
{{ section.label }}
</div>
<div v-if="section.contacts" class="pr-2">
<Autocomplete
value=""
:options="
contacts.data.map((contact) => {
return {
label: contact.full_name,
value: contact.name,
}
})
"
@change="(e) => addContact(e.value)"
>
<template #target="{ togglePopover }">
<Button
class="h-7 px-3"
label="Add contact"
@click="togglePopover()"
>
<template #prefix>
<FeatherIcon name="plus" class="h-4" />
</template>
</Button>
</template>
</Autocomplete>
</div>
</div>
<transition
enter-active-class="duration-300 ease-in"
leave-active-class="duration-300 ease-[cubic-bezier(0, 1, 0.5, 1)]"
enter-to-class="max-h-[200px] overflow-hidden"
leave-from-class="max-h-[200px] overflow-hidden"
enter-from-class="max-h-0 overflow-hidden"
leave-to-class="max-h-0 overflow-hidden"
>
<div v-if="opened" class="flex flex-col gap-1.5">
<div
v-if="section.fields"
v-for="field in section.fields"
:key="field.label"
class="flex items-center gap-2 px-3 text-base leading-5 first:mt-3"
>
<div class="w-[106px] shrink-0 text-gray-600">
{{ field.label }}
</div>
<div class="flex-1 overflow-hidden">
<Autocomplete
v-if="field.type === 'link'"
:value="deal.data[field.name]"
:options="field.options"
@change="(e) => field.change(e)"
:placeholder="field.placeholder"
class="form-control"
>
<template
v-if="field.create"
#footer="{ value, close }"
>
<div>
<Button
variant="ghost"
class="w-full !justify-start"
label="Create one"
@click="field.create(value, close)"
>
<template #prefix>
<FeatherIcon name="plus" class="h-4" />
</template>
</Button>
</div>
</template>
</Autocomplete>
<Dropdown
v-else-if="field.type === 'dropdown'"
:options="
statusDropdownOptions(deal.data, 'deal', updateField)
"
class="w-full flex-1"
>
<template #default="{ open }">
<Button
:label="deal.data[field.name]"
class="w-full justify-between"
>
<template #prefix>
<IndicatorIcon
:class="
dealStatuses[deal.data[field.name]].color
"
/>
</template>
<template #default>{{
deal.data[field.name]
}}</template>
<template #suffix>
<FeatherIcon
:name="open ? 'chevron-up' : 'chevron-down'"
class="h-4 text-gray-600"
/>
</template>
</Button>
</template>
</Dropdown>
<FormControl
v-else-if="field.type === 'date'"
type="date"
:value="deal.data[field.name]"
@change.stop="
updateDeal(field.name, $event.target.value)
"
:debounce="500"
class="form-control"
/>
<Tooltip
:text="field.tooltip"
class="flex h-7 cursor-pointer items-center px-2 py-1"
v-else-if="field.type === 'read_only'"
>
{{ field.value }}
</Tooltip>
<FormControl
v-else
type="text"
:value="deal.data[field.name]"
@change.stop="
updateDeal(field.name, $event.target.value)
"
:debounce="500"
class="form-control"
/>
</div>
<ExternalLinkIcon
v-if="
field.type === 'link' &&
field.link &&
deal.data[field.name]
"
class="h-4 w-4 shrink-0 cursor-pointer text-gray-600"
@click="field.link(deal.data[field.name])"
/>
</div>
<div v-else>
<div
v-if="section.contacts.length"
v-for="(contact, i) in section.contacts"
:key="contact.name"
>
<div
class="px-2 pb-2.5"
:class="[i == 0 ? 'pt-5' : 'pt-2.5']"
>
<Toggler
:is-opened="contact.opened"
v-slot="{ opened: cOpened, toggle: cToggle }"
>
<div
class="flex cursor-pointer items-center justify-between gap-2 pr-1 text-base leading-5 text-gray-700"
>
<div
class="flex h-7 items-center gap-2"
@click="cToggle()"
>
<Avatar
:label="
getContactByName(contact.name).full_name
"
:image="getContactByName(contact.name).image"
size="md"
/>
{{ getContactByName(contact.name).full_name }}
</div>
<div class="flex gap-3">
<FeatherIcon
name="trash-2"
class="h-4 w-4"
@click="removeContact(contact.name)"
/>
<ExternalLinkIcon
class="h-4 w-4"
@click="
router.push({
name: 'Contact',
params: { contactId: contact.name },
})
"
/>
<FeatherIcon
name="chevron-right"
class="h-4 w-4 text-gray-900 transition-all duration-300 ease-in-out"
:class="{ 'rotate-90': cOpened }"
@click="cToggle()"
/>
</div>
</div>
<transition
enter-active-class="duration-300 ease-in"
leave-active-class="duration-300 ease-[cubic-bezier(0, 1, 0.5, 1)]"
enter-to-class="max-h-[200px] overflow-hidden"
leave-from-class="max-h-[200px] overflow-hidden"
enter-from-class="max-h-0 overflow-hidden"
leave-to-class="max-h-0 overflow-hidden"
>
<div
v-if="cOpened"
class="flex flex-col gap-1.5 text-base text-gray-800"
>
<div
class="flex items-center gap-3 pb-1.5 pl-1 pt-4"
>
<EmailIcon class="h-4 w-4" />
{{ getContactByName(contact.name).email_id }}
</div>
<div class="flex items-center gap-3 p-1 py-1.5">
<PhoneIcon class="h-4 w-4" />
{{ getContactByName(contact.name).mobile_no }}
</div>
</div>
</transition>
</Toggler>
</div>
<div
v-if="i != section.contacts.length - 1"
class="mx-2 h-px border-t border-gray-200"
/>
</div>
<div v-else class="flex justify-center items-center text-base text-gray-600 h-20">
No contacts added
</div>
</div>
</div>
</transition>
</Toggler>
</div>
</div>
</div>
</div>
</div>
<OrganizationModal
v-model="showOrganizationModal"
:organization="_organization"
:options="{
redirect: false,
afterInsert: (doc) => updateField('organization', doc.name),
}"
/>
</template>
<script setup>
import ActivityIcon from '@/components/Icons/ActivityIcon.vue'
import EmailIcon from '@/components/Icons/EmailIcon.vue'
import PhoneIcon from '@/components/Icons/PhoneIcon.vue'
import TaskIcon from '@/components/Icons/TaskIcon.vue'
import NoteIcon from '@/components/Icons/NoteIcon.vue'
import IndicatorIcon from '@/components/Icons/IndicatorIcon.vue'
import LinkIcon from '@/components/Icons/LinkIcon.vue'
import ExternalLinkIcon from '@/components/Icons/ExternalLinkIcon.vue'
import LayoutHeader from '@/components/LayoutHeader.vue'
import Toggler from '@/components/Toggler.vue'
import Activities from '@/components/Activities.vue'
import UserAvatar from '@/components/UserAvatar.vue'
import OrganizationModal from '@/components/Modals/OrganizationModal.vue'
import Autocomplete from '@/components/frappe-ui/Autocomplete.vue'
import {
dealStatuses,
statusDropdownOptions,
openWebsite,
createToast,
activeAgents,
} from '@/utils'
import { usersStore } from '@/stores/users'
import { contactsStore } from '@/stores/contacts'
import { organizationsStore } from '@/stores/organizations'
import {
createResource,
FeatherIcon,
FormControl,
Dropdown,
Tooltip,
Avatar,
Tabs,
Breadcrumbs,
call,
} from 'frappe-ui'
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
const { getUser } = usersStore()
const { getContactByName, contacts } = contactsStore()
const { getOrganization, getOrganizationOptions } = organizationsStore()
const router = useRouter()
const props = defineProps({
dealId: {
type: String,
required: true,
},
})
const deal = createResource({
url: 'crm.fcrm.doctype.crm_deal.api.get_deal',
params: { name: props.dealId },
cache: ['deal', props.dealId],
auto: true,
})
const reload = ref(false)
const showOrganizationModal = ref(false)
const _organization = ref({})
function updateDeal(fieldname, value, callback) {
value = Array.isArray(fieldname) ? '' : value
createResource({
url: 'frappe.client.set_value',
params: {
doctype: 'CRM Deal',
name: props.dealId,
fieldname,
value,
},
auto: true,
onSuccess: () => {
deal.reload()
contacts.reload()
reload.value = true
createToast({
title: 'Deal updated',
icon: 'check',
iconClasses: 'text-green-600',
})
callback?.()
},
onError: (err) => {
createToast({
title: 'Error updating deal',
text: err.messages?.[0],
icon: 'x',
iconClasses: 'text-red-600',
})
},
})
}
const breadcrumbs = computed(() => {
let items = [{ label: 'Deals', route: { name: 'Deals' } }]
items.push({
label: organization.value?.name,
route: { name: 'Deal', params: { dealId: deal.data.name } },
})
return items
})
const tabIndex = ref(0)
const tabs = [
{
label: 'Activity',
icon: ActivityIcon,
},
{
label: 'Emails',
icon: EmailIcon,
},
{
label: 'Calls',
icon: PhoneIcon,
},
{
label: 'Tasks',
icon: TaskIcon,
},
{
label: 'Notes',
icon: NoteIcon,
},
]
const detailSections = computed(() => {
return [
{
label: 'Organization',
opened: true,
fields: [
{
label: 'Organization',
type: 'link',
name: 'organization',
placeholder: 'Select organization',
options: getOrganizationOptions(),
change: (data) => updateField('organization', data.value),
create: (value, close) => {
_organization.value.organization_name = value
showOrganizationModal.value = true
close()
},
link: () => {
router.push({
name: 'Organization',
params: { organizationId: organization.value.name },
})
},
},
{
label: 'Website',
type: 'read_only',
name: 'website',
value: organization.value?.website,
tooltip:
'It is a read only field, value is fetched from organization',
},
{
label: 'Amount',
type: 'read_only',
name: 'annual_revenue',
value: organization.value?.annual_revenue,
tooltip:
'It is a read only field, value is fetched from organization',
},
{
label: 'Close date',
type: 'date',
name: 'close_date',
},
{
label: 'Probability',
type: 'data',
name: 'probability',
},
{
label: 'Next step',
type: 'data',
name: 'next_step',
},
],
},
{
label: 'Contacts',
opened: true,
contacts: deal.data.contacts.map((contact) => {
return {
name: contact.contact,
opened: false,
}
}),
},
]
})
async function addContact(value) {
let d = await call('crm.fcrm.doctype.crm_deal.crm_deal.add_contact', {
deal: props.dealId,
contact: value,
})
if (d) {
deal.reload()
contacts.reload()
createToast({
title: 'Contact added',
icon: 'check',
iconClasses: 'text-green-600',
})
}
}
async function removeContact(value) {
let d = await call('crm.fcrm.doctype.crm_deal.crm_deal.remove_contact', {
deal: props.dealId,
contact: value,
})
if (d) {
deal.reload()
contacts.reload()
createToast({
title: 'Contact removed',
icon: 'check',
iconClasses: 'text-green-600',
})
}
}
const organization = computed(() => {
return getOrganization(deal.data.organization)
})
function updateField(name, value) {
updateDeal(name, value, () => {
deal.data[name] = value
})
}
</script>
<style scoped>
:deep(.form-control input),
:deep(.form-control button) {
border-color: transparent;
background: white;
}
:deep(.form-control button) {
gap: 0;
}
:deep(.form-control button > div) {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
:deep(.form-control button svg) {
color: white;
width: 0;
}
</style>