Skip to content

Commit

Permalink
Merge pull request kookmin-sw#6 from capstone-maru/chore/recoil-templ…
Browse files Browse the repository at this point in the history
…ate-code

Chore/recoil template code
  • Loading branch information
cjeongmin authored Mar 18, 2024
2 parents d26ade9 + 97656ce commit db50dfd
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/features/auth/auth.action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useCallback, useMemo } from 'react';
import { useRecoilState } from 'recoil';

import { authState } from './auth.atom';

export const useAuthActions = () => {
const [, setAuth] = useRecoilState(authState);

const login = useCallback(() => {
setAuth(prev => ({ ...prev, isLogin: true }));
}, [setAuth]);
const logout = useCallback(() => {
setAuth(prev => ({ ...prev, isLogin: false }));
}, [setAuth]);

useMemo(
() => ({
login,
logout,
}),
[login, logout],
);
};
Empty file removed src/features/auth/auth.api.ts
Empty file.
11 changes: 11 additions & 0 deletions src/features/auth/auth.atom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { atom } from 'recoil';

import { type AuthState } from './auth.model';

export const authState = atom<AuthState>({
key: 'authState',
default: {
isLogin: false,
token: undefined,
},
});
7 changes: 7 additions & 0 deletions src/features/auth/auth.hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useRecoilState, useRecoilValue } from 'recoil';

import { authState } from './auth.atom';
import { getIsLogin } from './auth.selector';

export const useAuthState = () => useRecoilState(authState);
export const useAuthIsLogin = () => useRecoilValue(getIsLogin);
4 changes: 4 additions & 0 deletions src/features/auth/auth.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface AuthState {
isLogin: boolean;
token?: string;
}
11 changes: 11 additions & 0 deletions src/features/auth/auth.selector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { selector } from 'recoil';

import { authState } from './auth.atom';

export const getIsLogin = selector<boolean>({
key: 'authState/isLogin',
get: ({ get }) => {
const auth = get(authState);
return auth.isLogin;
},
});
6 changes: 5 additions & 1 deletion src/features/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@

export * from './auth.action';
export * from './auth.atom';
export * from './auth.hook';
export * from './auth.model';
export * from './auth.selector';

0 comments on commit db50dfd

Please sign in to comment.