File size: 1,174 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import { useQuery } from '@tanstack/react-query';
import wpcom from 'calypso/lib/wp';
import type { EmailAccount } from './types';
import type { UseQueryOptions } from '@tanstack/react-query';
type UseGetEmailAccountsQueryData = EmailAccount[];
export const getCacheKey = ( siteId: number | null, domain: string ) => [
'sites',
siteId,
'emails',
'accounts',
domain,
'mailboxes',
];
/**
* Get the associated emails given a Site Identificator and a domain string (example.com)
* @param siteId Site identificator
* @param domain Domain name
* @param queryOptions Query options
* @returns Returns the result of the `useQuery` call
*/
export const useGetEmailAccountsQuery = (
siteId: number | null,
domain: string,
queryOptions?: Omit< UseQueryOptions< any, unknown, UseGetEmailAccountsQueryData >, 'queryKey' >
) => {
return useQuery< any, unknown, UseGetEmailAccountsQueryData >( {
queryKey: getCacheKey( siteId, domain ),
queryFn: () =>
wpcom.req.get( {
path: `/sites/${ siteId }/emails/accounts/${ encodeURIComponent( domain ) }/mailboxes`,
apiNamespace: 'wpcom/v2',
} ),
select: ( data ) => data.accounts,
...queryOptions,
} );
};
|