EvoCalculator.Client2/process/init/get-data/get-insurance-data.js

82 lines
1.8 KiB
JavaScript

/* eslint-disable import/prefer-default-export */
import { gql, useApolloClient } from '@apollo/client';
import { GetInsuranceDataDocument } from 'graphql/crm.types';
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
}
}
`;
/**
*
* @param {import('@apollo/client').ApolloClient['query']} query
* @param {*} onCompleted
*/
function getInsuranceData(query, onCompleted) {
query({
query: GetInsuranceDataDocument,
}).then(({ data }) => {
const insurance = {
osago: {
insuranceCompany: data.osago,
},
kasko: {
insuranceCompany: data.kasko,
},
fingap: {
insuranceCompany: data.fingap,
},
};
onCompleted(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);
}, []);
}