forked from lucianavlop/electricity-prices-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from daithihearn/react-query
feat: using react-query
- Loading branch information
Showing
16 changed files
with
744 additions
and
29,867 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v18.18.0 | ||
v20.11.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# build | ||
FROM node:18.18 AS builder | ||
FROM node:20.11 AS builder | ||
|
||
WORKDIR /app | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { Container, Typography } from "@mui/material" | ||
import React, { useCallback, useMemo } from "react" | ||
import PriceChart from "./PriceChart" | ||
import { DayType } from "models/DayType" | ||
import { useI18nContext } from "i18n/i18n-react" | ||
import { DayRating } from "models/DayRating" | ||
import { usePriceInfo } from "hooks/usePriceInfo" | ||
import DailyInfo from "./DailyInfo" | ||
import { useDateTime } from "hooks/RegionalDateTime" | ||
import WithLoading from "./WithLoading" | ||
|
||
export const PricesWrapper: React.FC<{ day: DayType }> = ({ day }) => { | ||
const { LL } = useI18nContext() | ||
const { now } = useDateTime() | ||
const { isLoading, dailyInfo, thirtyDayAverage } = usePriceInfo(day) | ||
|
||
const isToday = useMemo(() => { | ||
if (day === "today") { | ||
return true | ||
} | ||
if (day === "tomorrow") { | ||
return false | ||
} | ||
return day.hasSame(now(), "day") | ||
}, [day, now]) | ||
|
||
const isTomorrow = useMemo(() => { | ||
if (day === "tomorrow") { | ||
return true | ||
} | ||
return false | ||
}, [day]) | ||
|
||
const parseDateText = useCallback( | ||
(dayType: DayType) => { | ||
if (dayType === "today" || isToday) { | ||
return LL.TODAY() | ||
} | ||
if (dayType === "tomorrow") { | ||
return LL.TOMORROW() | ||
} | ||
return dayType.toFormat("dd/MM") | ||
}, | ||
[LL, isToday], | ||
) | ||
|
||
const ratingText = useMemo(() => { | ||
const date = parseDateText(day) | ||
|
||
switch (dailyInfo?.dayRating) { | ||
case DayRating.BAD: | ||
return LL.CURRENT_RATING_BAD({ | ||
currentDate: date, | ||
}) | ||
case DayRating.GOOD: | ||
return LL.CURRENT_RATING_GOOD({ | ||
currentDate: date, | ||
}) | ||
default: | ||
return LL.CURRENT_RATING_NORMAL({ | ||
currentDate: date, | ||
}) | ||
} | ||
}, [parseDateText, day, dailyInfo?.dayRating, LL]) | ||
|
||
if (isTomorrow && !dailyInfo) { | ||
return ( | ||
<WithLoading isLoading={isLoading}> | ||
<Container sx={{ p: 2 }}> | ||
<Typography | ||
variant="h2" | ||
component="h2" | ||
align="left" | ||
gutterBottom> | ||
{LL.TOMORROW_NO_DATA()} | ||
</Typography> | ||
</Container> | ||
</WithLoading> | ||
) | ||
} | ||
return ( | ||
<WithLoading isLoading={isLoading}> | ||
<Container sx={{ p: 2 }}> | ||
<Typography | ||
variant="h2" | ||
component="h2" | ||
align="left" | ||
gutterBottom> | ||
{ratingText} | ||
</Typography> | ||
</Container> | ||
|
||
<Container sx={{ p: 2 }}> | ||
{dailyInfo && ( | ||
<DailyInfo | ||
dailyInfo={dailyInfo} | ||
thirtyDayAverage={thirtyDayAverage} | ||
/> | ||
)} | ||
</Container> | ||
|
||
<Container sx={{ p: 2, height: "400px" }}> | ||
{dailyInfo && ( | ||
<PriceChart | ||
prices={dailyInfo.prices} | ||
average={thirtyDayAverage} | ||
chartId={`price-chart-${parseDateText(day)}`} | ||
dateFormat="HH:mm" | ||
showCurrentPrice={isToday} | ||
cheapestPeriods={dailyInfo.cheapestPeriods} | ||
expensivePeriods={dailyInfo.expensivePeriods} | ||
/> | ||
)} | ||
</Container> | ||
</WithLoading> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useQuery } from "@tanstack/react-query" | ||
import { useDateTime } from "./RegionalDateTime" | ||
import { getDailyAverages, getDailyPriceInfo } from "services/PriceService" | ||
import { useCallback, useMemo } from "react" | ||
import { DailyPriceInfo } from "models/DailyPriceInfo" | ||
import { DailyAverage } from "models/DailyAverage" | ||
import { DayType } from "models/DayType" | ||
|
||
export const usePriceInfo = (day: DayType) => { | ||
const { now } = useDateTime() | ||
|
||
const getDateTime = useCallback(() => { | ||
if (day === "today") { | ||
return now() | ||
} | ||
if (day === "tomorrow") { | ||
return now().plus({ days: 1 }) | ||
} | ||
return day | ||
}, [day, now]) | ||
|
||
const dailyInfoResp = useQuery<DailyPriceInfo | null>({ | ||
queryKey: ["dailyInfo", day], | ||
queryFn: async () => getDailyPriceInfo(getDateTime()), | ||
refetchInterval: 5 * 60_000, | ||
}) | ||
|
||
const averagesResp = useQuery<DailyAverage[]>({ | ||
queryKey: ["dailyAverages", day], | ||
queryFn: async () => getDailyAverages(getDateTime()), | ||
refetchInterval: 5 * 60_000, | ||
}) | ||
|
||
const thirtyDayAverage = useMemo(() => { | ||
if (averagesResp.isLoading || !averagesResp.data) { | ||
return 0 | ||
} | ||
return ( | ||
averagesResp.data.reduce( | ||
(accumulator, med) => accumulator + med.average, | ||
0, | ||
) / averagesResp.data.length | ||
) | ||
}, [averagesResp]) | ||
|
||
return { | ||
isLoading: dailyInfoResp.isLoading || averagesResp.isLoading, | ||
dailyInfo: dailyInfoResp.data, | ||
dailyAverages: averagesResp.data ?? [], | ||
thirtyDayAverage, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.