Skip to content

Commit

Permalink
Merge branch 'main' into feat/#7-todo
Browse files Browse the repository at this point in the history
  • Loading branch information
salmontaker authored Aug 24, 2023
2 parents 74e392e + b0d2132 commit c69b640
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 29 deletions.
21 changes: 8 additions & 13 deletions src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,30 @@ function Home() {
<StyledHome>
소개글~
<ButtonWrapper>
<Button
<SignButton
isSignin={false}
onClick={() => {
navigate('/signup')
}}
>
회원가입
</Button>
<Button
</SignButton>
<SignButton
isSignin={true}
onClick={() => {
navigate('/signin')
}}
>
로그인
</Button>

<Button
</SignButton>
<SignButton
isSignin={true}
onClick={() => {
navigate('/todo')
}}
>
투두 테스트 버튼
</Button>
</SignButton>
</ButtonWrapper>
</StyledHome>
)
Expand All @@ -44,7 +43,6 @@ const StyledHome = styled.div`
align-items: center;
flex-direction: column;
gap: 20px;
width: 100%;
height: 100vh;
`
Expand All @@ -55,19 +53,16 @@ const ButtonWrapper = styled.div`
align-items: center;
flex-direction: column;
gap: 10px;
width: 100%;
`

const Button = styled.button<{ isSignin: boolean }>`
const SignButton = styled.button<{ isSignin: boolean }>`
width: 100%;
height: 32px;
max-width: 425px;
cursor: pointer;
border: 1px solid gray;
border-radius: 5px;
cursor: pointer;
${({ isSignin }) => css`
background-color: ${isSignin ? 'black' : 'white'};
Expand Down
47 changes: 35 additions & 12 deletions src/pages/PageRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,50 @@ import Todo from './Todo'
import Signin from './Signin'
import Signup from './Signup'

interface IPublicRoute {
isSignIn: boolean
interface RouteProps {
isAuth: string | null
fallback: string
children: JSX.Element
}

function ProtectedRoute({ isAuth, fallback, children }: RouteProps) {
return isAuth ? children : <Navigate to={fallback} replace />
}
function PublicRoute({ isAuth, fallback, children }: RouteProps) {
return isAuth ? <Navigate to={fallback} replace /> : children
}

function PageRouter() {
const isAuth = localStorage.getItem('access_token')

function ProtectedRoute() {
return isAuth ? <Todo /> : <Navigate to="/signin" />
}
function PublicRoute({ isSignIn }: IPublicRoute) {
return isAuth ? <Navigate to="/todo" /> : isSignIn ? <Signin /> : <Signup />
}

return (
<BrowserRouter>
<Routes>
<Route index element={<Home />} />
<Route path="/todo" element={<ProtectedRoute />} />
<Route path="/signin" element={<PublicRoute isSignIn={true} />} />
<Route path="/signup" element={<PublicRoute isSignIn={false} />} />
<Route
path="/todo"
element={
<ProtectedRoute isAuth={isAuth} fallback="/signin">
<Todo />
</ProtectedRoute>
}
/>
<Route
path="/signin"
element={
<PublicRoute isAuth={isAuth} fallback="/todo">
<Signin />
</PublicRoute>
}
/>
<Route
path="/signup"
element={
<PublicRoute isAuth={isAuth} fallback="/todo">
<Signup />
</PublicRoute>
}
/>
</Routes>
</BrowserRouter>
)
Expand Down
10 changes: 8 additions & 2 deletions src/pages/Signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@ function Signin() {
>
홈으로
</SampleButton>
<button
onClick={() => {
localStorage.setItem('access_token', '12340')
navigate(0)
}}
>
accessToken 추가
</button>
</div>
)
}

const SampleButton = styled.div`
width: fit-content;
height: fit-content;
background-color: skyblue;
cursor: pointer;
`

Expand Down
2 changes: 0 additions & 2 deletions src/pages/Signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ function Signup() {
const SampleButton = styled.div`
width: fit-content;
height: fit-content;
background-color: skyblue;
cursor: pointer;
`

Expand Down
12 changes: 12 additions & 0 deletions src/pages/Todo.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { useNavigate } from 'react-router-dom'
import TodoList from '../components/TodoList/TodoList'

function Todo() {
const navigate = useNavigate()

return (
<div>
투두리스트 페이지입니다.
<TodoList />
<button
onClick={() => {
localStorage.removeItem('access_token')
navigate(0)
}}
>
accessToken 삭제
</button>
</div>
)
}
Expand Down

0 comments on commit c69b640

Please sign in to comment.