forked from deriv-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
farhan/feat: add useSettings hook to @deriv/api (deriv-com#9805)
* 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
1 parent
655f2c7
commit 11a5f9b
Showing
3 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |