57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
/* eslint-disable import/prefer-default-export */
|
|
import { gql, useQuery } from '@apollo/client';
|
|
|
|
import type { User } from 'api/user/types';
|
|
import type { GetOwnerDataQuery, GetOwnerDataQueryVariables } from 'graphql/crm.types';
|
|
import { useStore } from 'stores/hooks';
|
|
import { normalizeOptions } from 'tools/entity';
|
|
|
|
const QUERY_GET_OWNER_DATA = gql`
|
|
query GetOwnerData($domainname: String) {
|
|
selectLead: leads(owner_domainname: $domainname) {
|
|
label: fullname
|
|
value: leadid
|
|
}
|
|
selectOpportunity: opportunities(owner_domainname: $domainname) {
|
|
label: name
|
|
value: opportunityid
|
|
}
|
|
}
|
|
`;
|
|
|
|
export function useOwnerData(user: User) {
|
|
const { $calculation } = useStore();
|
|
|
|
function handleOnStartFetch() {
|
|
$calculation.$status.setStatus('selectLead', 'Loading');
|
|
$calculation.$status.setStatus('selectOpportunity', 'Loading');
|
|
}
|
|
|
|
function handleOnCompleted(data: GetOwnerDataQuery) {
|
|
$calculation.element('selectLead').setOptions(normalizeOptions(data?.selectLead));
|
|
$calculation.element('selectOpportunity').setOptions(normalizeOptions(data?.selectOpportunity));
|
|
|
|
$calculation.$status.setStatus('selectLead', 'Default');
|
|
$calculation.$status.setStatus('selectOpportunity', 'Default');
|
|
}
|
|
|
|
const { loading, error } = useQuery<GetOwnerDataQuery, GetOwnerDataQueryVariables>(
|
|
QUERY_GET_OWNER_DATA,
|
|
{
|
|
variables: {
|
|
domainname: user?.domainName,
|
|
},
|
|
|
|
onCompleted: handleOnCompleted,
|
|
}
|
|
);
|
|
|
|
if (loading) {
|
|
handleOnStartFetch();
|
|
}
|
|
|
|
return {
|
|
error,
|
|
};
|
|
}
|