Skip to content

Commit

Permalink
chore: combine get_settings with set_settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhan-slurrp committed Aug 24, 2023
1 parent c67c572 commit 22a6883
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/api/src/hooks/useSettings.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import { useMemo } from 'react';
import { useCallback, useMemo } from 'react';
import useFetch from '../useFetch';
import useInvalidateQuery from '../useInvalidateQuery';
import useRequest from '../useRequest';

type TSetSettingsPayload = 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 } = useRequest('set_settings', {
onSuccess: () => invalidate('get_settings'),
});

const set_settings = useCallback(
(values: NonNullable<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 */
set_settings,
...rest,
};
};
Expand Down

0 comments on commit 22a6883

Please sign in to comment.