forked from Team-Shaka/Breifing-WEB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHome.js
163 lines (149 loc) · 5.65 KB
/
Home.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import React, { useEffect, useState, useRef } from "react";
import Header from "../components/aboutComps/Header";
import { SocialBox } from "../components/homeComps/SocialBox";
import { GlobalBox } from "../components/homeComps/GlobalBox";
import { EconomyBox } from "../components/homeComps/EconomyBox";
import { ScienceBox } from "../components/homeComps/ScienceBox";
import axios from "axios";
import { formatDateWithDay } from "../utils/dateFormatter";
import { useIntersectionObserver } from "../hooks/useIntersectionObserver";
import { useRecoilValue } from "recoil";
import { categoryState } from "../recoil/atoms/categoryState";
import getCurrentTimeOfDay from "../utils/getCurrentTimeOfDay";
const Home = () => {
const [news, setNews] = useState([]);
const [date, setDate] = useState(new Date());
const [timeOfDay, setTimeOfDay] = useState(getCurrentTimeOfDay());
const [isLoading, setIsLoading] = useState(false);
const lastElementRef = useRef(null);
const category = useRecoilValue(categoryState);
useEffect(() => {
fetchNews(date, timeOfDay);
}, [date, timeOfDay]);
const loadMoreNews = () => {
setIsLoading(true);
let newDate = new Date(date);
let newTimeOfDay = timeOfDay === "Evening" ? "Morning" : "Evening";
if (timeOfDay === "Morning") {
newDate.setDate(newDate.getDate() - 1);
}
setDate(newDate);
setTimeOfDay(newTimeOfDay);
};
const fetchNews = async (currentDate, currentTimeOfDay) => {
const dateString = currentDate.toISOString().split("T")[0];
try {
const responses = await Promise.all([
axios.get(
`${process.env.REACT_APP_BASE_URL}/v2/briefings?type=SOCIAL&date=${dateString}&timeOfDay=${currentTimeOfDay}`
),
axios.get(
`${process.env.REACT_APP_BASE_URL}/v2/briefings?type=GLOBAL&date=${dateString}&timeOfDay=${currentTimeOfDay}`
),
axios.get(
`${process.env.REACT_APP_BASE_URL}/v2/briefings?type=ECONOMY&date=${dateString}&timeOfDay=${currentTimeOfDay}`
),
axios.get(
`${process.env.REACT_APP_BASE_URL}/v2/briefings?type=SCIENCE&date=${dateString}&timeOfDay=${currentTimeOfDay}`
),
]);
const newNews = responses.map(
(response) => response.data.result.briefings
);
setNews((prevNews) => [
...prevNews,
{
date: dateString,
timeOfDay: currentTimeOfDay,
social: newNews[0],
global: newNews[1],
economy: newNews[2],
science: newNews[3],
},
]);
} catch (error) {
console.error("Error fetching news:", error);
} finally {
setIsLoading(false);
}
};
useIntersectionObserver(lastElementRef, loadMoreNews, isLoading);
return (
<div>
<Header />
{news.map((newsItem, index) => (
<React.Fragment key={index}>
{newsItem.date && (
<>
<div
className={`my-4 flex justify-center sm:justify-start mx-auto sm:px-16 py-2 text-base text-[#306DAB] w-[calc(100%-4rem)] xl:w-[1200px] rounded-lg bg-[#0072E721] ${
index === 0 ? "flex sm:hidden" : ""
}`}
>
{formatDateWithDay(new Date(newsItem.date))}{" "}
{newsItem.timeOfDay === "Morning" ? "아침" : "저녁"} 브리핑
</div>
<div
className={`bg-black h-[1px] my-4 mx-auto px-2 w-[calc(100%-1rem)] xl:w-[1170px] ${
index === 0 ? "flex sm:hidden" : ""
}`}
></div>
</>
)}
{category === "전체" || category === "사회" ? (
<React.Fragment>
<SocialBox
briefingList={newsItem.social}
date={date.toISOString().split("T")[0]}
timeOfDay={timeOfDay}
/>
{category === "전체" && (
<div className="bg-black h-[1px] my-4 mx-auto px-2 w-[calc(100%-1rem)] xl:w-[1170px]"></div>
)}{" "}
</React.Fragment>
) : null}
{category === "전체" || category === "글로벌" ? (
<React.Fragment>
<GlobalBox
briefingList={newsItem.social}
date={date.toISOString().split("T")[0]}
timeOfDay={timeOfDay}
/>
{category === "전체" && (
<div className="bg-black h-[1px] my-4 mx-auto px-2 w-[calc(100%-1rem)] xl:w-[1170px]"></div>
)}{" "}
</React.Fragment>
) : null}
{category === "전체" || category === "경제" ? (
<React.Fragment>
<EconomyBox
briefingList={newsItem.social}
date={date.toISOString().split("T")[0]}
timeOfDay={timeOfDay}
/>
{category === "전체" && (
<div className="bg-black h-[1px] my-4 mx-auto px-2 w-[calc(100%-1rem)] xl:w-[1170px]"></div>
)}{" "}
</React.Fragment>
) : null}
{category === "전체" || category === "과학" ? (
<React.Fragment>
<ScienceBox
briefingList={newsItem.social}
date={date.toISOString().split("T")[0]}
timeOfDay={timeOfDay}
/>
</React.Fragment>
) : null}
</React.Fragment>
))}
{isLoading && (
<div className="w-full h-full flex justify-center items-center">
<span className="loading loading-spinner loading-lg text-primaryBgColor"></span>
</div>
)}
<div ref={lastElementRef} />
</div>
);
};
export default Home;