Skip to content

Commit cd97318

Browse files
authored
Merge pull request #19 from husamahmud/18-refactor-optimise-daily-problem-fetching
[refactor]: Optimise daily problem fetching
2 parents f52c8e7 + 9772490 commit cd97318

File tree

1 file changed

+55
-24
lines changed

1 file changed

+55
-24
lines changed

popup/src/Components/DailyProblem.jsx

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,54 @@
11
import { useEffect, useState } from "react";
22
import PropTypes from "prop-types";
3+
import useLocalStorage from "./useLocalStorage";
34

45
DailyProblem.propTypes = {
56
setReqErr: PropTypes.func.isRequired,
67
};
78

8-
export default function DailyProblem({ setReqErr }) {
9-
const [data, setData] = useState({});
9+
function getGSTDate() {
10+
const now = new Date();
11+
return `${now.getUTCFullYear()}${now.getUTCMonth() + 1}${now.getUTCDate()}`;
12+
}
13+
14+
function useDailyProblem(setReqErr) {
15+
const [data, setData] = useState(null);
1016
const [isLoading, setIsLoading] = useState(false);
1117
const [error, setError] = useState("");
18+
const [daily, setDaily] = useLocalStorage(null, "dailyProblem");
1219

1320
useEffect(() => {
1421
const fetchDailyProblem = async () => {
22+
let dataJson;
1523
try {
1624
setIsLoading(true);
1725
setError("");
1826

19-
const res = await fetch(`https://alfa-leetcode-api.onrender.com/daily`);
20-
if (!res.ok) {
21-
if (res.status === 429) setReqErr(true);
22-
else setError("Failed to fetch the daily problem");
27+
if (daily && daily.date === getGSTDate()) {
28+
dataJson = daily.data;
29+
} else {
30+
const res = await fetch(
31+
`https://alfa-leetcode-api.onrender.com/daily`
32+
);
33+
if (!res.ok) {
34+
if (res.status === 429) setReqErr(true);
35+
else setError("Failed to fetch the daily problem");
36+
}
37+
dataJson = await res.json();
38+
setDaily({
39+
data: {
40+
questionLink: dataJson.questionLink,
41+
questionTitle: dataJson.questionTitle,
42+
difficulty: dataJson.difficulty,
43+
},
44+
date: getGSTDate(),
45+
});
2346
}
24-
25-
const data = await res.json();
26-
setData(data);
27-
setError("");
47+
setData({
48+
questionLink: dataJson.questionLink,
49+
questionTitle: dataJson.questionTitle,
50+
difficulty: dataJson.difficulty,
51+
});
2852
} catch (err) {
2953
setError(err);
3054
} finally {
@@ -35,17 +59,18 @@ export default function DailyProblem({ setReqErr }) {
3559
fetchDailyProblem();
3660
}, []);
3761

62+
return { data, isLoading, error };
63+
}
64+
65+
export default function DailyProblem({ setReqErr }) {
66+
const { data, isLoading, error } = useDailyProblem(setReqErr);
3867
const getDifficultyColor = (difficulty) => {
39-
switch (difficulty) {
40-
case "Easy":
41-
return "bg-lp-green-dark";
42-
case "Medium":
43-
return "bg-lp-yellow-dark";
44-
case "Hard":
45-
return "bg-lp-red-dark";
46-
default:
47-
return "";
48-
}
68+
const colors = {
69+
Easy: "bg-lp-green-dark",
70+
Medium: "bg-lp-yellow-dark",
71+
Hard: "bg-lp-red-dark",
72+
};
73+
return colors[difficulty] || "";
4974
};
5075

5176
return (
@@ -55,12 +80,18 @@ export default function DailyProblem({ setReqErr }) {
5580
<div className="flex flex-col mb-2">
5681
<p className="font-medium text-sm">Daily Problem</p>
5782
<div>
58-
<a href={data.questionLink}
59-
target="_blank"
60-
className="text-sm font-semibold underline mr-3 transition hover:text-[#1586ff]">
83+
<a
84+
href={data.questionLink}
85+
target="_blank"
86+
className="text-sm font-semibold underline mr-3 transition hover:text-[#1586ff]"
87+
>
6188
{data.questionTitle}
6289
</a>
63-
<span className={`text-sm font-semibold px-1 py-1 ${getDifficultyColor(data.difficulty)} rounded-md`}>
90+
<span
91+
className={`text-sm font-semibold px-1 py-1 ${getDifficultyColor(
92+
data.difficulty
93+
)} rounded-md`}
94+
>
6495
{data.difficulty}
6596
</span>
6697
</div>

0 commit comments

Comments
 (0)