From 9772490f532384b5af81e9b5abbc558500719da3 Mon Sep 17 00:00:00 2001 From: Mahmoud Hamdy Date: Sat, 6 Apr 2024 18:38:55 +0200 Subject: [PATCH] [refactor]: Optimise daily problem fetching --- popup/src/Components/DailyProblem.jsx | 79 +++++++++++++++++++-------- 1 file changed, 55 insertions(+), 24 deletions(-) diff --git a/popup/src/Components/DailyProblem.jsx b/popup/src/Components/DailyProblem.jsx index 107bd85..79e4392 100644 --- a/popup/src/Components/DailyProblem.jsx +++ b/popup/src/Components/DailyProblem.jsx @@ -1,30 +1,54 @@ import { useEffect, useState } from "react"; import PropTypes from "prop-types"; +import useLocalStorage from "./useLocalStorage"; DailyProblem.propTypes = { setReqErr: PropTypes.func.isRequired, }; -export default function DailyProblem({ setReqErr }) { - const [data, setData] = useState({}); +function getGSTDate() { + const now = new Date(); + return `${now.getUTCFullYear()}${now.getUTCMonth() + 1}${now.getUTCDate()}`; +} + +function useDailyProblem(setReqErr) { + const [data, setData] = useState(null); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(""); + const [daily, setDaily] = useLocalStorage(null, "dailyProblem"); useEffect(() => { const fetchDailyProblem = async () => { + let dataJson; try { setIsLoading(true); setError(""); - const res = await fetch(`https://alfa-leetcode-api.onrender.com/daily`); - if (!res.ok) { - if (res.status === 429) setReqErr(true); - else setError("Failed to fetch the daily problem"); + if (daily && daily.date === getGSTDate()) { + dataJson = daily.data; + } else { + const res = await fetch( + `https://alfa-leetcode-api.onrender.com/daily` + ); + if (!res.ok) { + if (res.status === 429) setReqErr(true); + else setError("Failed to fetch the daily problem"); + } + dataJson = await res.json(); + setDaily({ + data: { + questionLink: dataJson.questionLink, + questionTitle: dataJson.questionTitle, + difficulty: dataJson.difficulty, + }, + date: getGSTDate(), + }); } - - const data = await res.json(); - setData(data); - setError(""); + setData({ + questionLink: dataJson.questionLink, + questionTitle: dataJson.questionTitle, + difficulty: dataJson.difficulty, + }); } catch (err) { setError(err); } finally { @@ -35,17 +59,18 @@ export default function DailyProblem({ setReqErr }) { fetchDailyProblem(); }, []); + return { data, isLoading, error }; +} + +export default function DailyProblem({ setReqErr }) { + const { data, isLoading, error } = useDailyProblem(setReqErr); const getDifficultyColor = (difficulty) => { - switch (difficulty) { - case "Easy": - return "bg-lp-green-dark"; - case "Medium": - return "bg-lp-yellow-dark"; - case "Hard": - return "bg-lp-red-dark"; - default: - return ""; - } + const colors = { + Easy: "bg-lp-green-dark", + Medium: "bg-lp-yellow-dark", + Hard: "bg-lp-red-dark", + }; + return colors[difficulty] || ""; }; return ( @@ -55,12 +80,18 @@ export default function DailyProblem({ setReqErr }) {

Daily Problem

- + {data.questionTitle} - + {data.difficulty}