/* eslint-disable import/prefer-default-export */ import { useApolloClient, useQuery } from '@apollo/client'; import { useStore } from 'stores/hooks'; import getAddProductTypes from './get-addproduct-types-data'; import getBrands from './get-brands'; import getDealers from './get-dealers'; import getInsuranceData from './get-insurance-data'; import getMainData from './get-main-data'; import { QUERY_GET_OWNER_DATA } from './get-owner-data'; export function useCRMData() { const store = useStore(); const apolloClient = useApolloClient(); const { $calculation, $tables } = store; function setManyOptions(options) { Object.keys(options).forEach((elementName) => { const elementOptions = options[elementName]; $calculation.element(elementName).setOptions(elementOptions); }); } getMainData(apolloClient).then(({ options }) => setManyOptions(options)); getBrands(apolloClient).then(({ options }) => setManyOptions(options)); getDealers(apolloClient).then(({ options }) => setManyOptions(options)); getAddProductTypes(apolloClient).then(({ options }) => setManyOptions(options)); function setManyRowOptions(options) { Object.keys(options).forEach((key) => { const rowOptions = options[key]; if (rowOptions !== undefined) { Object.keys(rowOptions).forEach((valueName) => { $tables.insurance.row(key).setOptions(valueName, rowOptions[valueName]); }); } }); } getInsuranceData(apolloClient).then(({ tables }) => setManyRowOptions(tables.insurance)); } export function useOwnerData(user) { const store = useStore(); const { $calculation } = store; function handleOnStartFetch() { $calculation.$status.setStatus('selectLead', 'Loading'); $calculation.$status.setStatus('selectOpportunity', 'Loading'); } function handleOnCompleted(data) { $calculation.element('selectLead').setOptions(data?.selectLead); $calculation.element('selectOpportunity').setOptions(data?.selectOpportunity); $calculation.$status.setStatus('selectLead', 'Default'); $calculation.$status.setStatus('selectOpportunity', 'Default'); } const { loading, error } = useQuery(QUERY_GET_OWNER_DATA, { variables: { domainname: user?.domainName, }, onCompleted: handleOnCompleted, }); if (loading) { handleOnStartFetch(); } return { error, }; }