HyeonJun Jeon

[Add] Pages for login, setting

1 -import "./styles/App.css";
2 import { BrowserRouter, Route, Routes } from "react-router-dom"; 1 import { BrowserRouter, Route, Routes } from "react-router-dom";
2 +import React from "react";
3 3
4 +import "./styles/App.css";
4 import Calendar from "./pages/Calendar"; 5 import Calendar from "./pages/Calendar";
5 import Home from "./pages/Home"; 6 import Home from "./pages/Home";
7 +import Login from "./pages/Login";
8 +import Settings from "./pages/Settings";
9 +
10 +export const AppStateContext = React.createContext();
6 11
7 function App() { 12 function App() {
8 return ( 13 return (
9 <BrowserRouter> 14 <BrowserRouter>
10 - <div className="App"> 15 + <AppStateContext.Provider value={[]}>
11 - <Routes> 16 + <div className="App">
12 - <Route path="/calendar/*" element={<Calendar />} /> 17 + <Routes>
13 - <Route exact path="/login" element={<></>} /> 18 + <Route path="*" element={<Home />} />
14 - <Route exact path="/setting" element={<></>} /> 19 + <Route path="/calendar/*" element={<Calendar />} />
15 - <Route path="*" element={<Home />} /> 20 + <Route path="/login" element={<Login />} />
16 - </Routes> 21 + <Route path="/settings" element={<Settings />} />
17 - </div> 22 + </Routes>
23 + </div>
24 + </AppStateContext.Provider>
18 </BrowserRouter> 25 </BrowserRouter>
19 ); 26 );
20 } 27 }
......
...@@ -59,6 +59,9 @@ const Header = () => { ...@@ -59,6 +59,9 @@ const Header = () => {
59 <span className="hcs">{headLabel}</span> 59 <span className="hcs">{headLabel}</span>
60 </div> 60 </div>
61 <div className="hr"> 61 <div className="hr">
62 + <button className="hrb_l" onClick={() => navigate("/settings")}>
63 + 설정
64 + </button>
62 <div className="hrd"> 65 <div className="hrd">
63 <button 66 <button
64 disabled={state.scope === "day"} 67 disabled={state.scope === "day"}
...@@ -85,6 +88,15 @@ const Header = () => { ...@@ -85,6 +88,15 @@ const Header = () => {
85 88
86 </button> 89 </button>
87 </div> 90 </div>
91 + <button
92 + className="hrb_r"
93 + onClick={() => {
94 + localStorage.setItem("session", "");
95 + navigate("/");
96 + }}
97 + >
98 + 로그아웃
99 + </button>
88 </div> 100 </div>
89 </header> 101 </header>
90 ); 102 );
......
1 import React, { useState } from "react"; 1 import React, { useState } from "react";
2 -import { Route, Routes } from "react-router-dom"; 2 +import { Navigate, Route, Routes } from "react-router-dom";
3 3
4 import Grid from "../components/Grid"; 4 import Grid from "../components/Grid";
5 import Header from "../components/Header"; 5 import Header from "../components/Header";
...@@ -8,7 +8,10 @@ import "../styles/Home.css"; ...@@ -8,7 +8,10 @@ import "../styles/Home.css";
8 export const CalendarStateContext = React.createContext(); 8 export const CalendarStateContext = React.createContext();
9 9
10 const Calendar = () => { 10 const Calendar = () => {
11 - //scope는 day, state는 date 11 + console.log("visit Calendar");
12 +
13 + const session = localStorage.getItem("session");
14 +
12 const [state, setState] = useState({ 15 const [state, setState] = useState({
13 scope: "month", 16 scope: "month",
14 date: new Date(), 17 date: new Date(),
...@@ -16,14 +19,18 @@ const Calendar = () => { ...@@ -16,14 +19,18 @@ const Calendar = () => {
16 19
17 return ( 20 return (
18 <CalendarStateContext.Provider value={[state, setState]}> 21 <CalendarStateContext.Provider value={[state, setState]}>
19 - <div className="Calendar"> 22 + {session ? (
20 - <Header /> 23 + <div className="Calendar">
21 - <Routes> 24 + <Header />
22 - <Route path="/month/*" element={<Grid />} /> 25 + <Routes>
23 - <Route path="/week/*" element={<></>} /> 26 + <Route path="/month/*" element={<Grid />} />
24 - <Route path="/day/*" element={<></>} /> 27 + <Route path="/week/*" element={<></>} />
25 - </Routes> 28 + <Route path="/day/*" element={<></>} />
26 - </div> 29 + </Routes>
30 + </div>
31 + ) : (
32 + <Navigate to="/login" />
33 + )}
27 </CalendarStateContext.Provider> 34 </CalendarStateContext.Provider>
28 ); 35 );
29 }; 36 };
......
...@@ -2,8 +2,18 @@ import { useEffect } from "react"; ...@@ -2,8 +2,18 @@ import { useEffect } from "react";
2 import { useNavigate } from "react-router-dom"; 2 import { useNavigate } from "react-router-dom";
3 3
4 const Home = () => { 4 const Home = () => {
5 + const session = localStorage.getItem("session");
6 + console.log("visit Home");
7 +
8 + let destination;
9 + if (session) {
10 + destination = "/calendar/month";
11 + } else {
12 + destination = "/login";
13 + }
14 +
5 const navigate = useNavigate(); 15 const navigate = useNavigate();
6 - useEffect(() => navigate("/calendar/month"), [navigate]); 16 + useEffect(() => navigate(destination), [destination, navigate]);
7 }; 17 };
8 18
9 export default Home; 19 export default Home;
......
1 +import { Navigate, useNavigate } from "react-router-dom";
2 +
3 +const Login = () => {
4 + console.log("visit Login");
5 + const session = localStorage.getItem("session");
6 +
7 + const navigate = useNavigate();
8 + const login = () => {
9 + if (true) {
10 + //login success
11 + localStorage.setItem("session", true);
12 +
13 + navigate("/");
14 + }
15 + };
16 +
17 + return session ? (
18 + <Navigate to="/" />
19 + ) : (
20 + <div className="Login">
21 + <button onClick={login}>로그인</button>
22 + </div>
23 + );
24 +};
25 +
26 +export default Login;
1 +import { Navigate, useNavigate } from "react-router-dom";
2 +
3 +const Settings = () => {
4 + const session = localStorage.getItem("session");
5 +
6 + const navigate = useNavigate();
7 +
8 + return session ? (
9 + <div className="Settings">
10 + <button
11 + onClick={() => {
12 + navigate(-1);
13 + }}
14 + >
15 + {"<"}
16 + </button>
17 + </div>
18 + ) : (
19 + <Navigate to="/login" />
20 + );
21 +};
22 +
23 +export default Settings;
...@@ -41,7 +41,16 @@ header { ...@@ -41,7 +41,16 @@ header {
41 margin: 10px; 41 margin: 10px;
42 } 42 }
43 43
44 +.hrd {
45 + margin-right: 10px;
46 + margin-left: 10px;
47 +}
48 +
44 .hrd > button { 49 .hrd > button {
45 padding: 8px 14px 8px 14px; 50 padding: 8px 14px 8px 14px;
46 font-size: medium; 51 font-size: medium;
47 } 52 }
53 +
54 +.hr {
55 + display: flex;
56 +}
......