From 47ce415c5f76c4dd241ee1674a237a1eea2917f2 Mon Sep 17 00:00:00 2001 From: solufa Date: Thu, 7 Nov 2024 00:08:02 +0900 Subject: [PATCH] refactor: split fetching me function --- client/features/auth/AuthLoader.tsx | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/client/features/auth/AuthLoader.tsx b/client/features/auth/AuthLoader.tsx index 56a3fd5..7c7e6f8 100644 --- a/client/features/auth/AuthLoader.tsx +++ b/client/features/auth/AuthLoader.tsx @@ -12,6 +12,10 @@ export const AuthLoader = () => { const { user, setUser } = useUser(); const { setLoading } = useLoading(); const { setAlert } = useAlert(); + const fetchMe = useCallback( + () => apiClient.private.me.$get().catch(catchApiErr).then(setUser), + [setUser], + ); const updateCookie = useCallback(async () => { const tokens = await fetchAuthSession() .then((e) => e.tokens) @@ -25,14 +29,12 @@ export const AuthLoader = () => { }) .catch(catchApiErr); - if (result?.status === 'success') { - await apiClient.private.me.$get().catch(catchApiErr).then(setUser); - } - }, [setUser]); + if (result?.status === 'success') await fetchMe(); + }, [setUser, fetchMe]); useEffect(() => { - updateCookie(); - }, [updateCookie]); + fetchMe(); + }, [fetchMe]); useEffect(() => { // eslint-disable-next-line complexity