diff --git a/frontend/src/components/NewModalSubscripton.js b/frontend/src/components/NewModalSubscripton.js index 14b1536c..6f1d070b 100644 --- a/frontend/src/components/NewModalSubscripton.js +++ b/frontend/src/components/NewModalSubscripton.js @@ -94,13 +94,12 @@ const NewSubscription = ({ isFreeOption, onClose }) => { - {typesCache.data.subscriptions.map((type) => { + {typesCache.data.map((type) => { const radio = getRadioProps({ value: type.id, isDisabled: !type.active || - (isFreeOption && - type.subscription_type !== "ethereum_blockchain"), + (isFreeOption && type.id !== "ethereum_blockchain"), }); return ( diff --git a/frontend/src/components/NewSubscription.js b/frontend/src/components/NewSubscription.js index 8ff6223a..4b931fd4 100644 --- a/frontend/src/components/NewSubscription.js +++ b/frontend/src/components/NewSubscription.js @@ -103,13 +103,12 @@ const _NewSubscription = ({ isFreeOption, onClose, setIsLoading }) => { - {typesCache.data.subscriptions.map((type) => { + {typesCache.data.map((type) => { const radio = getRadioProps({ value: type.id, isDisabled: !type.active || - (isFreeOption && - type.subscription_type !== "ethereum_blockchain"), + (isFreeOption && type.id !== "ethereum_blockchain"), }); return ( diff --git a/frontend/src/components/SubscriptionsList.js b/frontend/src/components/SubscriptionsList.js index 738f27af..13b50dc5 100644 --- a/frontend/src/components/SubscriptionsList.js +++ b/frontend/src/components/SubscriptionsList.js @@ -22,8 +22,12 @@ import ConfirmationRequest from "./ConfirmationRequest"; import ColorSelector from "./ColorSelector"; const SubscriptionsList = ({ emptyCTA }) => { - const { subscriptionsCache, updateSubscription, deleteSubscription } = - useSubscriptions(); + const { + subscriptionsCache, + updateSubscription, + deleteSubscription, + subscriptionTypeIcons, + } = useSubscriptions(); const updateCallback = ({ id, label, color }) => { const data = { id: id }; @@ -61,27 +65,8 @@ const SubscriptionsList = ({ emptyCTA }) => { {subscriptionsCache.data.subscriptions.map((subscription) => { - let iconLink; - switch (subscription.subscription_type_id) { - case "0": - iconLink = - "https://ethereum.org/static/c48a5f760c34dfadcf05a208dab137cc/31987/eth-diamond-rainbow.png"; - break; - case `ethereum_txpool`: - iconLink = - "https://ethereum.org/static/a183661dd70e0e5c70689a0ec95ef0ba/31987/eth-diamond-purple.png"; - break; - case `algorand_blockchain`: - iconLink = - "https://www.algorand.com/assets/media-kit/logos/logo-marks/png/algorand_logo_mark_black.png"; - break; - case `algorand_txpool`: - iconLink = - "https://www.algorand.com/assets/media-kit/logos/logo-marks/png/algorand_logo_mark_white.png"; - break; - default: - console.error("no icon found for this pool"); - } + const iconLink = + subscriptionTypeIcons[subscription.subscription_type_id]; return ( diff --git a/frontend/src/core/hooks/useSubscriptions.js b/frontend/src/core/hooks/useSubscriptions.js index 9148c9e6..b5e52b1e 100644 --- a/frontend/src/core/hooks/useSubscriptions.js +++ b/frontend/src/core/hooks/useSubscriptions.js @@ -1,14 +1,17 @@ +import { useState, useEffect } from "react"; +import { useQuery } from "react-query"; import { SubscriptionsService } from "../services"; import { useMutation } from "react-query"; import { useToast } from "."; import { queryCacheProps } from "./hookCommon"; import useStripe from "./useStripe"; -import { useQuery } from "react-query"; const useSubscriptions = () => { const toast = useToast(); const stripe = useStripe(); + const [subscriptionTypeIcons, setSubscriptionTypeIcons] = useState({}); + const getSubscriptions = async () => { const response = await SubscriptionsService.getSubscriptions(); return response.data; @@ -23,7 +26,11 @@ const useSubscriptions = () => { const getSubscriptionTypes = async () => { const response = await SubscriptionsService.getTypes(); - return response.data; + let result = []; + if (response.data.subscription_types) { + result = response.data.subscription_types; + } + return result; }; const typesCache = useQuery(["subscription_types"], getSubscriptionTypes, { @@ -33,6 +40,17 @@ const useSubscriptions = () => { }, }); + useEffect(() => { + let icons = {}; + if (typesCache.data) { + typesCache.data.forEach( + (subscriptionType) => + (icons[subscriptionType.id] = subscriptionType.icon_url) + ); + setSubscriptionTypeIcons(icons); + } + }, [typesCache.data]); + const createSubscription = useMutation( SubscriptionsService.createSubscription(), { @@ -76,6 +94,7 @@ const useSubscriptions = () => { typesCache, updateSubscription, deleteSubscription, + subscriptionTypeIcons, }; };