diff --git a/packages/api/src/hooks/index.ts b/packages/api/src/hooks/index.ts index 6795a3ac3953..4e7c224e93b1 100644 --- a/packages/api/src/hooks/index.ts +++ b/packages/api/src/hooks/index.ts @@ -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'; diff --git a/packages/api/src/hooks/useBalance.tsx b/packages/api/src/hooks/useBalance.ts similarity index 100% rename from packages/api/src/hooks/useBalance.tsx rename to packages/api/src/hooks/useBalance.ts diff --git a/packages/api/src/hooks/useSettings.ts b/packages/api/src/hooks/useSettings.ts new file mode 100644 index 000000000000..d51820815b01 --- /dev/null +++ b/packages/api/src/hooks/useSettings.ts @@ -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>['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;