feat: install swr and fetch data with it

This commit is contained in:
vidvidvid
2021-11-23 10:59:12 +01:00
committed by Alec LaLonde
parent 110300a2c4
commit 5e7f6bb2aa
3 changed files with 34 additions and 23 deletions

View File

@@ -1,6 +1,7 @@
import { Box, Heading, LoadingState, Text } from '@metafam/ds';
import React, { useEffect, useState } from 'react';
import { parse } from 'rss-to-json';
import useSWR from 'swr';
export const Listen: React.FC = () => {
const [items, setItems] = useState<
@@ -14,31 +15,28 @@ export const Listen: React.FC = () => {
const [isLoading, setIsLoading] = useState(true);
const { data, error } = useSWR(
'https://anchor.fm/s/57a641c/podcast/rss',
parse,
);
if (error) {
console.log(error);
setIsLoading(false);
}
useEffect(() => {
const getData = async () => {
try {
const podcastDataRss = await parse(
'https://anchor.fm/s/57a641c/podcast/rss',
{},
);
if (data) {
data.items = data.items.map((item) => ({
title: item.title,
description: item.description.replace(/<\/?[^>]+(>|$)/g, ''),
src: item.enclosures[0].url,
showMore: false,
}));
const podcasts = podcastDataRss.items.map((item) => ({
title: item.title,
description: item.description.replace(/<\/?[^>]+(>|$)/g, ''),
src: item.enclosures[0].url,
showMore: false,
}));
setIsLoading(false);
setItems(podcasts);
} catch (error) {
console.log(error);
setIsLoading(false);
}
};
getData();
}, []);
setIsLoading(false);
}
}, [data]);
const findAndReplace = (src: string) => {
const foundIndex = items.findIndex((x) => x.src === src);

View File

@@ -47,6 +47,7 @@
"react-resizable": "^3.0.4",
"react-vis": "^1.11.7",
"rss-to-json": "2.0.2",
"swr": "1.0.1",
"urql": "2.0.2",
"web3modal": "1.9.4"
},

View File

@@ -12551,6 +12551,11 @@ deprecation@^2.0.0, deprecation@^2.3.1:
resolved "https://registry.yarnpkg.com/deprecation/-/deprecation-2.3.1.tgz#6368cbdb40abf3373b525ac87e4a260c3a700919"
integrity sha512-xmHIy4F3scKVwMsQ4WnVaS8bHOx0DmVwRywosKhaILI0ywMDWPtBSku2HNxRvF7jtwDRsoEwYQSfbxj8b7RlJQ==
dequal@2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/dequal/-/dequal-2.0.2.tgz#85ca22025e3a87e65ef75a7a437b35284a7e319d"
integrity sha512-q9K8BlJVxK7hQYqa6XISGmBZbtQQWVXSrRrWreHC94rMt1QL/Impruc+7p2CYSYuVIUr+YCt6hjrs1kkdJRTug==
des.js@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/des.js/-/des.js-1.0.1.tgz#5382142e1bdc53f85d86d53e5f4aa7deb91e0843"
@@ -28284,6 +28289,13 @@ swap-case@^2.0.2:
dependencies:
tslib "^2.0.3"
swr@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/swr/-/swr-1.0.1.tgz#15f62846b87ee000e52fa07812bb65eb62d79483"
integrity sha512-EPQAxSjoD4IaM49rpRHK0q+/NzcwoT8c0/Ylu/u3/6mFj/CWnQVjNJ0MV2Iuw/U+EJSd2TX5czdAwKPYZIG0YA==
dependencies:
dequal "2.0.2"
symbol-observable@1.2.0, symbol-observable@^1.1.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804"