crm/frontend/src/stores/organizations.js
2023-11-03 16:33:41 +05:30

35 lines
841 B
JavaScript

import { defineStore } from 'pinia'
import { createResource } from 'frappe-ui'
import { reactive } from 'vue'
export const organizationsStore = defineStore('crm-organizations', () => {
let organizationsByName = reactive({})
const organizations = createResource({
url: 'crm.api.session.get_organizations',
cache: 'organizations',
initialData: [],
transform(organizations) {
for (let organization of organizations) {
organizationsByName[organization.name] = organization
}
return organizations
},
onError(error) {
if (error && error.exc_type === 'AuthenticationError') {
router.push('/login')
}
},
})
organizations.fetch()
function getOrganization(name) {
return organizationsByName[name]
}
return {
organizations,
getOrganization,
}
})