Fix state persistence
This commit is contained in:
@@ -70,7 +70,7 @@ const Base = function ({ children }) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const App: React.FC = () => {
|
const App: React.FC = () => {
|
||||||
const [state, setState] = useState(loadStorageOrDefault);
|
const [state, setState] = useState(loadStorageOrDefault());
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
persistStorage(state);
|
persistStorage(state);
|
||||||
|
|||||||
@@ -32,5 +32,5 @@ export const loadStorage = function () {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const loadStorageOrDefault = function (state) {
|
export const loadStorageOrDefault = function (state) {
|
||||||
return mergeDeepRight(loadStorage(), defaultState);
|
return mergeDeepRight(defaultState, loadStorage());
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
minDate,
|
minDate,
|
||||||
ForecastDatePicker,
|
ForecastDatePicker,
|
||||||
} from "@/components/ui/DatePicker";
|
} from "@/components/ui/DatePicker";
|
||||||
|
import { path } from "ramda";
|
||||||
|
|
||||||
const fetchWeekForecastData = async function (dateTime, cb) {
|
const fetchWeekForecastData = async function (dateTime, cb) {
|
||||||
const formattedDate = formatDate(dateTime);
|
const formattedDate = formatDate(dateTime);
|
||||||
@@ -55,9 +56,21 @@ export const Dashboard = function ({ state, updateState }) {
|
|||||||
minDate(state?.options?.startDate || new Date()),
|
minDate(state?.options?.startDate || new Date()),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const dateKey = formatDate(dateTime);
|
||||||
|
const data = path(["dataCache", dateKey], state);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchWeekForecastData(dateTime, (data) => updateState({ dataCache: data }));
|
console.log("DASHBOARD DATA", state);
|
||||||
}, [dateTime]);
|
if (!data) {
|
||||||
|
fetchWeekForecastData(dateTime, (data) =>
|
||||||
|
updateState({
|
||||||
|
dataCache: {
|
||||||
|
[dateKey]: data,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}, [state.dataCache, dateTime]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex p-5 bg-white border border-solid border-slate-300 rounded-lg space-y-5">
|
<div className="flex p-5 bg-white border border-solid border-slate-300 rounded-lg space-y-5">
|
||||||
@@ -65,7 +78,12 @@ export const Dashboard = function ({ state, updateState }) {
|
|||||||
<h1 className="text-lg font-medium mb-2">Forecast</h1>
|
<h1 className="text-lg font-medium mb-2">Forecast</h1>
|
||||||
<ForecastDatePicker dateTime={dateTime} onChange={setDateTime} />
|
<ForecastDatePicker dateTime={dateTime} onChange={setDateTime} />
|
||||||
</div>
|
</div>
|
||||||
{state.dataCache ? <WeatherChart data={state.dataCache} /> : "Loading..."}
|
<div
|
||||||
|
className="flex items-center justify-center"
|
||||||
|
style={{ minHeight: 400, minWidth: 400 }}
|
||||||
|
>
|
||||||
|
{data ? <WeatherChart data={data} /> : "Loading..."}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user