HyeonJun Jeon

[Style] Set state type from int to Date

...@@ -4,18 +4,19 @@ import GridRow from "./GridRow.js"; ...@@ -4,18 +4,19 @@ import GridRow from "./GridRow.js";
4 4
5 import React, { useContext } from "react"; 5 import React, { useContext } from "react";
6 import { CalendarStateContext } from "../pages/Calendar"; 6 import { CalendarStateContext } from "../pages/Calendar";
7 -import { moveDate, toSunday, toYMD } from "../utils/Dates"; 7 +import { moveDate, toSunday } from "../utils/Dates";
8 8
9 const Grid = () => { 9 const Grid = () => {
10 const [state] = useContext(CalendarStateContext); 10 const [state] = useContext(CalendarStateContext);
11 11
12 const renderRows = () => { 12 const renderRows = () => {
13 const rows = []; 13 const rows = [];
14 - const ndate = new Date(state.year, state.month - 1, 1); 14 + const ndate = new Date(state.date);
15 + ndate.setDate(1);
15 toSunday(ndate); 16 toSunday(ndate);
16 17
17 for (let i = 0; i < 5; i++) { 18 for (let i = 0; i < 5; i++) {
18 - rows.push(<GridRow key={i} startDate={toYMD(ndate)} />); 19 + rows.push(<GridRow key={i} startDate={new Date(ndate)} />);
19 moveDate(ndate, "week", 1); 20 moveDate(ndate, "week", 1);
20 } 21 }
21 22
......
1 import { useContext } from "react"; 1 import { useContext } from "react";
2 import { CalendarStateContext } from "../pages/Calendar"; 2 import { CalendarStateContext } from "../pages/Calendar";
3 +import { toYMD } from "../utils/Dates";
3 4
4 const GridItem = ({ targetDate }) => { 5 const GridItem = ({ targetDate }) => {
5 const [state] = useContext(CalendarStateContext); 6 const [state] = useContext(CalendarStateContext);
6 - const { month, date } = targetDate; 7 + const { month } = toYMD(state.date);
8 + const { month: tmonth, date: tdate } = toYMD(targetDate);
7 9
8 const displayDate = () => { 10 const displayDate = () => {
9 - if (state.month !== month) return month + "/" + date; 11 + if (month !== tmonth) return tmonth + "/" + tdate;
10 - else return date; 12 + else return tdate;
11 }; 13 };
12 return ( 14 return (
13 - <div className="GridItem" relative={month - state.month || null}> 15 + <div className="GridItem" relative={tmonth - month || null}>
14 <span>{displayDate()}</span> 16 <span>{displayDate()}</span>
15 </div> 17 </div>
16 ); 18 );
......
1 -import { moveDate, toYMD } from "../utils/Dates"; 1 +import { moveDate } from "../utils/Dates";
2 import GridItem from "./GridItem"; 2 import GridItem from "./GridItem";
3 3
4 const GridRow = ({ startDate }) => { 4 const GridRow = ({ startDate }) => {
5 - const { year, month, date } = startDate;
6 -
7 const renderItems = () => { 5 const renderItems = () => {
8 const items = []; 6 const items = [];
9 - const ndate = new Date(year, month - 1, date); 7 + const ndate = new Date(startDate);
10 8
11 for (let i = 0; i < 7; i++) { 9 for (let i = 0; i < 7; i++) {
12 - items.push(<GridItem key={i} targetDate={toYMD(ndate)} />); 10 + items.push(<GridItem key={i} targetDate={new Date(ndate)} />);
13 moveDate(ndate, "day", 1); 11 moveDate(ndate, "day", 1);
14 } 12 }
15 13
......
...@@ -6,6 +6,7 @@ import { moveDate, toYMD } from "../utils/Dates"; ...@@ -6,6 +6,7 @@ import { moveDate, toYMD } from "../utils/Dates";
6 6
7 const Header = () => { 7 const Header = () => {
8 const [state, setState] = useContext(CalendarStateContext); 8 const [state, setState] = useContext(CalendarStateContext);
9 + const { year, month, date } = toYMD(state.date);
9 10
10 const navigate = useNavigate(); 11 const navigate = useNavigate();
11 const setScope = (e) => { 12 const setScope = (e) => {
...@@ -17,19 +18,19 @@ const Header = () => { ...@@ -17,19 +18,19 @@ const Header = () => {
17 }; 18 };
18 19
19 const move = (e) => { 20 const move = (e) => {
20 - const current = new Date(state.year, state.month - 1, state.date); 21 + const current = new Date(state.date);
21 moveDate(current, state.scope, Number(e.target.value)); 22 moveDate(current, state.scope, Number(e.target.value));
22 - setState({ scope: state.scope, ...toYMD(current) }); 23 + setState({ scope: state.scope, date: current });
23 }; 24 };
24 25
25 let headLabel; 26 let headLabel;
26 switch (state.scope) { 27 switch (state.scope) {
27 case "month": 28 case "month":
28 case "week": 29 case "week":
29 - headLabel = state.year + "년 " + state.month + "월"; 30 + headLabel = year + "년 " + month + "월";
30 break; 31 break;
31 case "day": 32 case "day":
32 - headLabel = state.year + "년 " + state.month + "월 " + state.date + "일"; 33 + headLabel = year + "년 " + month + "월 " + date + "일";
33 break; 34 break;
34 default: 35 default:
35 headLabel = "unexpected scope"; 36 headLabel = "unexpected scope";
...@@ -43,7 +44,7 @@ const Header = () => { ...@@ -43,7 +44,7 @@ const Header = () => {
43 <div className="hc"> 44 <div className="hc">
44 <button 45 <button
45 className="hcb" 46 className="hcb"
46 - onClick={() => setState({ scope: state.scope, ...toYMD(new Date()) })} 47 + onClick={() => setState({ scope: state.scope, date: new Date() })}
47 > 48 >
48 오늘 49 오늘
49 </button> 50 </button>
......
...@@ -4,7 +4,6 @@ import { Route, Routes } from "react-router-dom"; ...@@ -4,7 +4,6 @@ import { Route, Routes } from "react-router-dom";
4 import Grid from "../components/Grid"; 4 import Grid from "../components/Grid";
5 import Header from "../components/Header"; 5 import Header from "../components/Header";
6 import "../styles/Home.css"; 6 import "../styles/Home.css";
7 -import { toYMD } from "../utils/Dates";
8 7
9 export const CalendarStateContext = React.createContext(); 8 export const CalendarStateContext = React.createContext();
10 9
...@@ -12,7 +11,7 @@ const Calendar = () => { ...@@ -12,7 +11,7 @@ const Calendar = () => {
12 //scope는 day, state는 date 11 //scope는 day, state는 date
13 const [state, setState] = useState({ 12 const [state, setState] = useState({
14 scope: "month", 13 scope: "month",
15 - ...toYMD(new Date()), 14 + date: new Date(),
16 }); 15 });
17 16
18 return ( 17 return (
......