From 2845d21e9c17db5e6db7f0d91a7d81cb0b3cf8e5 Mon Sep 17 00:00:00 2001 From: Paul Scanlon Date: Tue, 23 May 2023 18:24:03 +0100 Subject: [PATCH] feat: fix errors with Twitter api --- src/components/error-state.js | 29 +++++++++++++++++++ src/components/recent-tweets.js | 49 ++++++++++++++++++++------------- 2 files changed, 59 insertions(+), 19 deletions(-) create mode 100644 src/components/error-state.js diff --git a/src/components/error-state.js b/src/components/error-state.js new file mode 100644 index 00000000..363ac309 --- /dev/null +++ b/src/components/error-state.js @@ -0,0 +1,29 @@ +import React from 'react'; + +const ErrorState = () => { + return ( +
+ + + + +
+ Error + Probably something to do with that Elon bloke. +
+
+ ); +}; + +export default ErrorState; diff --git a/src/components/recent-tweets.js b/src/components/recent-tweets.js index cb94167d..f1a4986e 100644 --- a/src/components/recent-tweets.js +++ b/src/components/recent-tweets.js @@ -1,6 +1,7 @@ import React, { useState, useEffect, useRef } from 'react'; import Loading from '../components/loading'; +import ErrorState from '../components/error-state'; import { formatDatestamp } from '../utils/format-date-stamp'; @@ -8,28 +9,37 @@ const RecentTweets = () => { const isMounted = useRef(false); const [isLoading, setIsLoading] = useState(true); + const [isError, setIsError] = useState(false); const [response, setResponse] = useState({}); useEffect(() => { const getGitHubData = async () => { try { - const tweetsData = await ( - await fetch('https://paulieapi.gatsbyjs.io/api/get-latest-tweets', { - method: 'POST', - body: JSON.stringify({ - id: 470012453 - }) + const tweetResponse = await fetch('https://paulieapi.gatsbyjs.io/api/get-latest-tweets', { + method: 'POST', + body: JSON.stringify({ + id: 470012453 }) - ).json(); - - const userData = await ( - await fetch('https://paulieapi.gatsbyjs.io/api/get-twitter-user', { - method: 'POST', - body: JSON.stringify({ - username: 'PaulieScanlon' - }) + }); + + if (!tweetResponse.ok) { + throw new Error('get-latest-tweets'); + } + + const tweetsData = await tweetResponse.json(); + + const twitterUser = await fetch('https://paulieapi.gatsbyjs.io/api/get-twitter-user', { + method: 'POST', + body: JSON.stringify({ + username: 'PaulieScanlon' }) - ).json(); + }); + + if (!twitterUser.ok) { + throw new Error('get-latest-tweets'); + } + + const userData = await twitterUser.json(); if (isMounted) { setResponse({ @@ -40,6 +50,7 @@ const RecentTweets = () => { } } catch (error) { console.error(error); + setIsError(true); } }; @@ -63,7 +74,7 @@ const RecentTweets = () => {
{isLoading ? (
- + {isError ? : }
) : (
@@ -104,7 +115,7 @@ const RecentTweets = () => {
{isLoading ? (
- + {isError ? : }
) : (
    @@ -120,12 +131,12 @@ const RecentTweets = () => {
)}
-
+ {/*
Powered by Paulie API -
+
*/}
); };