crm/frontend/src/stores/contacts.js
2023-11-04 00:40:24 +05:30

41 lines
946 B
JavaScript

import { defineStore } from 'pinia'
import { createResource } from 'frappe-ui'
import { reactive } from 'vue'
export const contactsStore = defineStore('crm-contacts', () => {
let contactsByPhone = reactive({})
let contactsByName = reactive({})
const contacts = createResource({
url: 'crm.api.session.get_contacts',
cache: 'contacts',
initialData: [],
transform(contacts) {
for (let contact of contacts) {
contactsByPhone[contact.mobile_no] = contact
contactsByName[contact.name] = contact
}
return contacts
},
onError(error) {
if (error && error.exc_type === 'AuthenticationError') {
router.push('/login')
}
},
})
contacts.fetch()
function getContact(mobile_no) {
return contactsByPhone[mobile_no]
}
function getContactByName(name) {
return contactsByName[name]
}
return {
contacts,
getContact,
getContactByName,
}
})