mirror of
https://github.com/logto-io/logto.git
synced 2025-02-17 22:04:19 -05:00
refactor(console): reuse useCurrentUser
for useMeCustomData
This commit is contained in:
parent
3cbfd7e712
commit
9f9446ecda
2 changed files with 14 additions and 35 deletions
|
@ -20,7 +20,7 @@ const useCurrentUser = () => {
|
|||
|
||||
const isLoading = !user && !error;
|
||||
|
||||
return { user, isLoading, error, reload: mutate };
|
||||
return { user, isLoading, error, reload: mutate, api };
|
||||
};
|
||||
|
||||
export default useCurrentUser;
|
||||
|
|
|
@ -1,57 +1,36 @@
|
|||
import { useLogto } from '@logto/react';
|
||||
import { isKeyInObject } from '@logto/shared/universal';
|
||||
import { conditional } from '@silverhand/essentials';
|
||||
import { useCallback } from 'react';
|
||||
import { toast } from 'react-hot-toast';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import useSWR from 'swr';
|
||||
|
||||
import { adminTenantEndpoint, meApi } from '@/consts';
|
||||
|
||||
import type { RequestError } from './use-api';
|
||||
import { useStaticApi } from './use-api';
|
||||
import useLogtoUserId from './use-logto-user-id';
|
||||
import useSwrFetcher from './use-swr-fetcher';
|
||||
import useCurrentUser from './use-current-user';
|
||||
|
||||
const useMeCustomData = () => {
|
||||
const { isAuthenticated, error: authError } = useLogto();
|
||||
const { user, isLoading, error, reload, api } = useCurrentUser();
|
||||
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
|
||||
const userId = useLogtoUserId();
|
||||
const shouldFetch = isAuthenticated && !authError && userId;
|
||||
const api = useStaticApi({ prefixUrl: adminTenantEndpoint, resourceIndicator: meApi.indicator });
|
||||
|
||||
const fetcher = useSwrFetcher(api);
|
||||
|
||||
const {
|
||||
data: meData,
|
||||
mutate,
|
||||
error,
|
||||
// Reuse the same key `me` as `useCurrentUser()` to avoid additional requests.
|
||||
} = useSWR<unknown, RequestError>(shouldFetch && 'me', fetcher);
|
||||
const data = conditional(isKeyInObject(meData, 'customData') && meData.customData);
|
||||
|
||||
const update = useCallback(
|
||||
async (data: Record<string, unknown>) => {
|
||||
if (!userId) {
|
||||
async (customData: Record<string, unknown>) => {
|
||||
if (!user) {
|
||||
toast.error(t('errors.unexpected_error'));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const updated = await api
|
||||
.patch(`me/custom-data`, {
|
||||
json: data,
|
||||
json: customData,
|
||||
})
|
||||
.json();
|
||||
await mutate(updated);
|
||||
.json<(typeof user)['customData']>();
|
||||
|
||||
await reload({ ...user, customData: updated });
|
||||
},
|
||||
[api, mutate, t, userId]
|
||||
[api, reload, t, user]
|
||||
);
|
||||
|
||||
return {
|
||||
data,
|
||||
data: user?.customData,
|
||||
error,
|
||||
isLoading: !data && !error,
|
||||
isLoaded: Boolean(data && !error),
|
||||
isLoading,
|
||||
isLoaded: !isLoading && !error,
|
||||
update,
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue