From 62188ffdc752382fa8fbd7c56c22ceb8063e7e44 Mon Sep 17 00:00:00 2001 From: Ben Phelps Date: Wed, 14 Sep 2022 09:09:11 +0300 Subject: [PATCH] cleanup --- src/components/services/dropdown.jsx | 24 ++++++++------ src/components/services/item.jsx | 16 +--------- src/components/services/widget.jsx | 4 +-- .../widgets/service/coinmarketcap.jsx | 32 ++++++++++++++----- src/components/services/widgets/widget.jsx | 2 +- 5 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/components/services/dropdown.jsx b/src/components/services/dropdown.jsx index c7aad96e5..4bc25a8fd 100644 --- a/src/components/services/dropdown.jsx +++ b/src/components/services/dropdown.jsx @@ -1,14 +1,15 @@ import { Fragment } from "react"; import { Menu, Transition } from "@headlessui/react"; import { BiCog } from "react-icons/bi"; +import classNames from "classnames"; -export default function Dropdown({ options, state }) { +export default function Dropdown({ options, value, setValue }) { return (
- - {state.value.label} -
@@ -21,18 +22,21 @@ export default function Dropdown({ options, state }) { leaveFrom="transform opacity-100 scale-100" leaveTo="transform opacity-0 scale-95" > - +
- {options.map((i) => ( - + {options.map((option) => ( + ))} diff --git a/src/components/services/item.jsx b/src/components/services/item.jsx index 1c4475970..06cc1629d 100644 --- a/src/components/services/item.jsx +++ b/src/components/services/item.jsx @@ -33,19 +33,6 @@ export default function Item({ service }) { } }; - const cmcValues = [ - { label: t("coinmarketcap.1hour"), value: "1h" }, - { label: t("coinmarketcap.1day"), value: "24h" }, - { label: t("coinmarketcap.7days"), value: "7d" }, - { label: t("coinmarketcap.30days"), value: "30d" }, - ]; - - const [cmcV, cmcSet] = useState(cmcValues[0]); - - const states = { - coinmarketcap: { value: cmcV, set: cmcSet }, - }; - const hasLink = service.href && service.href !== "#"; return ( @@ -100,7 +87,6 @@ export default function Item({ service }) { )} - {service?.widget?.type === "coinmarketcap" && }
@@ -109,7 +95,7 @@ export default function Item({ service }) { - {service.widget && } + {service.widget && } diff --git a/src/components/services/widget.jsx b/src/components/services/widget.jsx index 415b24f32..df1243240 100644 --- a/src/components/services/widget.jsx +++ b/src/components/services/widget.jsx @@ -48,13 +48,13 @@ const widgetMappings = { prowlarr: Prowlarr, }; -export default function Widget({ service, state }) { +export default function Widget({ service }) { const { t } = useTranslation("common"); const ServiceWidget = widgetMappings[service.widget.type]; if (ServiceWidget) { - return ; + return ; } return ( diff --git a/src/components/services/widgets/service/coinmarketcap.jsx b/src/components/services/widgets/service/coinmarketcap.jsx index 5262eb026..8fc30f525 100644 --- a/src/components/services/widgets/service/coinmarketcap.jsx +++ b/src/components/services/widgets/service/coinmarketcap.jsx @@ -1,14 +1,26 @@ import useSWR from "swr"; +import { useState } from "react"; import { useTranslation } from "react-i18next"; import Widget from "../widget"; import Block from "../block"; +import Dropdown from "components/services/dropdown"; import { formatApiUrl } from "utils/api-helpers"; +import classNames from "classnames"; -export default function CoinMarketCap({ service, state }) { +export default function CoinMarketCap({ service }) { const { t } = useTranslation(); + const dateRangeOptions = [ + { label: t("coinmarketcap.1hour"), value: "1h" }, + { label: t("coinmarketcap.1day"), value: "24h" }, + { label: t("coinmarketcap.7days"), value: "7d" }, + { label: t("coinmarketcap.30days"), value: "30d" }, + ]; + + const [dateRange, setDateRange] = useState(dateRangeOptions[0].value); + const config = service.widget; const currencyCode = config.currency ?? "USD"; const { symbols } = config; @@ -29,7 +41,7 @@ export default function CoinMarketCap({ service, state }) { return ; } - if (!statsData) { + if (!statsData || !dateRange) { return ( @@ -41,29 +53,33 @@ export default function CoinMarketCap({ service, state }) { return ( +
+ +
+
- {symbols.map((key) => ( + {symbols.map((symbol) => (
-
{data[key].name}
+
{data[symbol].name}
{t("common.number", { - value: data[key].quote[currencyCode].price, + value: data[symbol].quote[currencyCode].price, style: "currency", currency: currencyCode, })}
0 + data[symbol].quote[currencyCode][`percent_change_${dateRange}`] > 0 ? "text-emerald-300" : "text-rose-300" }`} > - {data[key].quote[currencyCode][`percent_change_${state.value.value}`].toFixed(2)}% + {data[symbol].quote[currencyCode][`percent_change_${dateRange}`].toFixed(2)}%
diff --git a/src/components/services/widgets/widget.jsx b/src/components/services/widgets/widget.jsx index 98e4683eb..12202c05f 100644 --- a/src/components/services/widgets/widget.jsx +++ b/src/components/services/widgets/widget.jsx @@ -7,5 +7,5 @@ export default function Widget({ error = false, children }) { ); } - return
{children}
; + return
{children}
; }