/* eslint-disable import/prefer-default-export */ import { gql, useApolloClient } from '@apollo/client'; import { useEffect } from 'react'; import { useStore } from 'stores/hooks'; const QUERY_GET_INSURANCE_DATA = gql` query GetInsuranceData($evo_account_type: [Int!]) { osago: accounts( evo_account_type: $evo_account_type evo_type_ins_policy: [100000001] statecode: 0 ) { value: accountid label: name } kasko: accounts( evo_account_type: $evo_account_type evo_type_ins_policy: [100000000] statecode: 0 ) { value: accountid label: name } fingap: accounts( evo_account_type: $evo_account_type evo_type_ins_policy: [100000002] statecode: 0 ) { value: accountid label: name } } `; function getInsuranceData(query, handleOnCompleted) { query({ query: QUERY_GET_INSURANCE_DATA, }).then(({ data }) => { const insurance = { osago: { insuranceCompany: data.osago, }, kasko: { insuranceCompany: data.kasko, }, fingap: { insuranceCompany: data.fingap, }, }; handleOnCompleted(insurance); }); } export function useInsuranceData() { const { $tables } = useStore(); const { query } = useApolloClient(); function handleOnCompleted(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]); }); } }); } useEffect(() => { getInsuranceData(query, handleOnCompleted); }, []); }