Use source loader for email sprite icons
[ProtonMail-WebClient.git] / packages / wallet / store / hooks / useQuotesByProvider.ts
blobf05fb752e27c4209bc30f15ad147a6fcc9204f22
1 import { useCallback, useEffect } from 'react';
3 import { createSelector } from '@reduxjs/toolkit';
5 import { baseUseSelector } from '@proton/react-redux-store';
6 import { createHooks } from '@proton/redux-utilities';
8 import type { GetQuotesArgs, QuotesByProvider } from '../slices/quotesByProvider';
9 import { getQuotesByProviderKey, quotesByProviderThunk, selectQuotesByProvider } from '../slices/quotesByProvider';
11 const hooks = createHooks(quotesByProviderThunk, selectQuotesByProvider);
13 export const useGetQuotesByProvider = () => {
14     const get = hooks.useGet();
15     return useCallback(
16         async (args: GetQuotesArgs) => {
17             const key = getQuotesByProviderKey(args);
18             const results = await get({ thunkArg: args });
20             return results[key];
21         },
22         [get]
23     );
26 export const useQuotesByProvider = (args: GetQuotesArgs) => {
27     const getQuotesByProvider = useGetQuotesByProvider();
29     const key = getQuotesByProviderKey(args);
31     const paymentMethodsByProvider = createSelector(
32         selectQuotesByProvider,
33         (result): [QuotesByProvider | undefined, boolean] => {
34             const { error, value } = result;
36             const rate = value?.[key];
37             const loading = rate === undefined && error === undefined;
39             return [rate, loading];
40         }
41     );
43     useEffect(() => {
44         void getQuotesByProvider(args);
45     }, [args, getQuotesByProvider]);
47     return baseUseSelector(paymentMethodsByProvider);