Skip to content

Commit

Permalink
farhan/feat: add useSettings hook to @deriv/api (#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 committed Aug 28, 2023
1 parent c495888 commit 4e00227
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;

1 comment on commit 4e00227

@vercel
Copy link

@vercel vercel bot commented on 4e00227 Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

deriv-app – ./

deriv-app.vercel.app
binary.sx
deriv-app.binary.sx
deriv-app-git-master.binary.sx

Please sign in to comment.