Add state persistance function
This commit is contained in:
@@ -2,68 +2,31 @@ import React, { useState, useEffect } from "react";
|
||||
import { LineChart, Line, CartesianGrid, XAxis, YAxis } from "recharts";
|
||||
import { format, addDays } from "date-fns";
|
||||
import { Dashboard } from "@/components/main/routes/Dashboard";
|
||||
import {
|
||||
loadStorageOrDefault,
|
||||
persistStorage,
|
||||
} from "@/components/main/lib/state";
|
||||
import { mergeDeepRight } from "ramda";
|
||||
|
||||
const formatDate = function (dateTime) {
|
||||
return format(dateTime, "yyyy-MM-dd");
|
||||
};
|
||||
|
||||
const fetchWeekForecastData = async function (dateTime, cb) {
|
||||
const formattedDate = formatDate(dateTime);
|
||||
|
||||
const forecastRequests = Array.from({ length: 7 }).map((_, index) => {
|
||||
const date = addDays(dateTime, index);
|
||||
const formattedDate = format(date, "yyyy-MM-d");
|
||||
const url = `http://api.weatherapi.com/v1/future.json?key=bfca7632f8dc4253859120435242301&q=Vienna&dt=${formattedDate}`;
|
||||
const dataPromise = fetch(url).then((x) => x.json());
|
||||
|
||||
return dataPromise;
|
||||
});
|
||||
|
||||
const data = await Promise.all(forecastRequests);
|
||||
|
||||
console.log(data);
|
||||
|
||||
const chartData = data.map((forecastData) => {
|
||||
const dayData = forecastData.forecast.forecastday[0];
|
||||
const date = dayData.date;
|
||||
const { avgtemp_c, mintemp_c, maxtemp_c } = dayData.day;
|
||||
|
||||
return {
|
||||
name: date,
|
||||
avgtemp_c,
|
||||
mintemp_c,
|
||||
maxtemp_c,
|
||||
};
|
||||
});
|
||||
|
||||
cb(chartData);
|
||||
};
|
||||
|
||||
const WeatherChart = function ({ data }) {
|
||||
return (
|
||||
<LineChart width={400} height={400} data={data}>
|
||||
<Line type="monotone" dataKey="avgtemp_c" stroke="#8884d8" />
|
||||
<Line type="monotone" dataKey="mintemp_c" stroke="#8884d8" />
|
||||
<Line type="monotone" dataKey="maxtemp_c" stroke="#8884d8" />
|
||||
<CartesianGrid stroke="#ccc" />
|
||||
<XAxis dataKey="name" />
|
||||
<YAxis />
|
||||
</LineChart>
|
||||
);
|
||||
};
|
||||
const STORAGE_KEY = "ecoplanet";
|
||||
|
||||
const App: React.FC = () => {
|
||||
const [options, setOptions] = useState({
|
||||
lineWidth: undefined,
|
||||
defaultDate: undefined,
|
||||
});
|
||||
const [state, setState] = useState(loadStorageOrDefault);
|
||||
|
||||
useEffect(() => {
|
||||
persistStorage(state);
|
||||
}, [state]);
|
||||
|
||||
const updateState = function (newState) {
|
||||
setState(mergeDeepRight(state, newState));
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="fixed top-0 right-0 bottom-0 left-0 flex">
|
||||
<main className="flex grow flex-col">
|
||||
<nav className="flex p-3 border-b border-solid border-slate-300"></nav>
|
||||
<div className="flex grow items-center justify-center h-full w-full">
|
||||
<Dashboard startDate={options.defaultDate} />
|
||||
<Dashboard state={state} updateState={updateState} />
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user