Skip to content

Commit

Permalink
fix: 🐛isLogin from useGlobalContext => useLogin
Browse files Browse the repository at this point in the history
  • Loading branch information
ldu1020 authored and AlgoRoots committed Oct 27, 2023
1 parent 3836c2b commit bdd4704
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 33 deletions.
23 changes: 1 addition & 22 deletions src/contexts/global/hooks/useGlobalEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,7 @@ interface useGlobalEffectParams extends ReturnType<typeof useGlobalState> {
webStorage: ReturnType<typeof useWebStorage>;
}

export const useGlobalEffect = ({
webStorage,
dispatch,
}: useGlobalEffectParams) => {
// const { mutate } = useUserRefreshCreateMutation({
// options: {
// enabled: !!tokenStorage,
// onSuccess: (res) => {
// tokenStorage?.set(res);
// },
// onError: () => {
// tokenStorage?.remove();
// },
// },
// });

// For: sync isLogin by access token
useEffect(() => {
const isLogin = !!webStorage.token?.access;
dispatch({ type: 'SET_IS_LOGIN', payload: isLogin });
}, [dispatch, webStorage.token?.access]);

export const useGlobalEffect = ({ dispatch }: useGlobalEffectParams) => {
// For: refresh before expired
useEffect(() => {
const refresh = tokenStorage?.get()?.refresh;
Expand Down
6 changes: 0 additions & 6 deletions src/contexts/global/hooks/useGlobalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import { createSlice } from '@/utils/react/create-slice';
import { ConfirmAlertState } from './types/confirm-alert-state';

export type GlobalStateType = {
isLogin: null | boolean;
confirmAlert: ConfirmAlertState;
count: number;
};

const initialState: GlobalStateType = {
isLogin: null,
count: 0,
confirmAlert: {
isOpen: false,
Expand All @@ -25,10 +23,6 @@ const initialState: GlobalStateType = {
export const globalSlice = createSlice({
initialState,
reducers: {
RESET: () => initialState,
SET_IS_LOGIN: (state, isLogin: boolean) => {
state.isLogin = isLogin;
},
SET_COUNT: (state, count: number) => {
state.count = count;
},
Expand Down
4 changes: 2 additions & 2 deletions src/hocs/withAuthGuard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ComponentProps, ComponentType, useEffect } from 'react';

import { useRouter } from 'next/router';

import { useGlobalContext } from '@/contexts/global/useGlobalStoreContext';
import { useLogin } from '@/hooks/useLogin';

import { ROUTES } from '@/constants/routes';

Expand All @@ -11,7 +11,7 @@ export default function withAuthGuard<T extends ComponentType<any>>(
) {
return function WrappedAppComponent(props: ComponentProps<T>) {
const router = useRouter();
const isLogin = useGlobalContext((ctx) => ctx.state.isLogin);
const { isLogin } = useLogin();

useEffect(() => {
if (isLogin === false)
Expand Down
4 changes: 2 additions & 2 deletions src/hocs/withUnAuthGuard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { ComponentProps, ComponentType, useEffect } from 'react';

import { useRouter } from 'next/router';

import { useGlobalContext } from '@/contexts/global/useGlobalStoreContext';
import { useLogin } from '@/hooks/useLogin';

export default function withUnAuthGuard<T extends ComponentType<any>>(
AppComponent: T,
) {
return function WrappedAppComponent(props: ComponentProps<T>) {
const router = useRouter();
const isLogin = useGlobalContext((store) => store.state.isLogin);
const { isLogin } = useLogin();

useEffect(() => {
if (isLogin === true)
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { useGlobalContext } from '@/contexts/global/useGlobalStoreContext';
import { tokenStorage } from '@/utils/web-storage/token';

export const useLogin = () => {
const isLogin = useGlobalContext((ctx) => ctx.state.isLogin);
const token = useGlobalContext((ctx) => ctx.webStorage.token);
const isLogin = !!token;

const queryClient = useQueryClient();
const router = useRouter();
Expand Down

0 comments on commit bdd4704

Please sign in to comment.