EvoCalculator.Client2/apps/web/process/init/get-insurance-data.js
2023-02-28 09:46:31 +03:00

80 lines
1.8 KiB
JavaScript

/* eslint-disable canonical/sort-keys */
import { useStore } from '@/stores/hooks';
import { gql, useApolloClient } from '@apollo/client';
import { useEffect } from 'react';
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} apolloClient
* @param {*} onCompleted
*/
function getInsuranceData({ query }, onCompleted) {
query({
query: QUERY_GET_INSURANCE_DATA,
}).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 apolloClient = 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(apolloClient, handleOnCompleted);
}, []);
}