Skip to content

Commit

Permalink
farhan/feat: add useSettings hook to @deriv/api (deriv-com#9805)
Browse files Browse the repository at this point in the history
* chore: added useSettings to @deriv/api

* chore: combine get_settings with set_settings

* refactor: change function name

* chore: return all mutation data

* chore: export hook

* refactor: types and mutation function name
  • Loading branch information
farhan-nurzi-deriv authored and vinu-deriv committed Sep 1, 2023
1 parent 655f2c7 commit 11a5f9b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/api/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export { default as useAuthorize } from './useAuthorize';
export { default as useBalance } from './useBalance';
export { default as useCurrencyConfig } from './useCurrencyConfig';
export { default as useMT5LoginList } from './useMT5LoginList';
export { default as useSettings } from './useSettings';
export { default as useTradingPlatformAccounts } from './useTradingPlatformAccounts';
export { default as useWalletAccountsList } from './useWalletAccountsList';
File renamed without changes.
32 changes: 32 additions & 0 deletions packages/api/src/hooks/useSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useCallback, useMemo } from 'react';
import useFetch from '../useFetch';
import useInvalidateQuery from '../useInvalidateQuery';
import useRequest from '../useRequest';

type TSetSettingsPayload = NonNullable<
NonNullable<NonNullable<Parameters<ReturnType<typeof useRequest<'set_settings'>>['mutate']>>[0]>['payload']
>;

/** A custom hook to get user settings (email, date of birth, address etc) */
const useSettings = () => {
const { data, ...rest } = useFetch('get_settings');
const invalidate = useInvalidateQuery();
const { mutate, ...mutate_rest } = useRequest('set_settings', {
onSuccess: () => invalidate('get_settings'),
});

const update = useCallback((values: TSetSettingsPayload) => mutate({ payload: { ...values } }), [mutate]);

const modified_data = useMemo(() => ({ ...data?.get_settings }), [data?.get_settings]);

return {
/** User information and settings */
data: modified_data,
/** Function to update user settings */
update,
mutation: mutate_rest,
...rest,
};
};

export default useSettings;

0 comments on commit 11a5f9b

Please sign in to comment.