From 48fc60bd070e4273006bf33de47d2724b422047e Mon Sep 17 00:00:00 2001 From: yudejp Date: Sun, 30 May 2021 17:59:08 +0900 Subject: [PATCH] Change data retrieving method --- pages/components/DiscordPlaying.js | 24 +++++++++++++----------- pages/components/Minecraft.js | 1 - pages/components/Spotify.js | 25 +++++++++++++------------ 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/pages/components/DiscordPlaying.js b/pages/components/DiscordPlaying.js index 920cafa..b08f79b 100644 --- a/pages/components/DiscordPlaying.js +++ b/pages/components/DiscordPlaying.js @@ -7,22 +7,24 @@ function App (){ const router = useRouter() const { locale, locales, defaultLocale, pathname } = router const { t, lang } = useTranslation("common") - const [dataPlaying, setDataPlaying] = useState({ hits: [] }); - useEffect(async () => { - const result = await axios( - 'https://discord.com/api/guilds/723409709306216498/widget.json', - ); - - setDataPlaying(result.dataPlaying); - }, []); - if (dataPlaying === undefined){ + const [data, setData] = useState({ hits: [] }); + useEffect(() => { + const fetchData = async () => { + const result = await axios( + 'https://discord.com/api/guilds/723409709306216498/widget.json', + ); + setData(result.data); + }; + fetchData(); + }, []); + if (data === undefined){ console.log("[Discord API] データの取得に失敗しました。 / Failed to retrieve data.") return

}else{ - const str = JSON.stringify(dataPlaying) + const str = JSON.stringify(data) if (str.indexOf("game") !== -1){ - const yes_playing = t('yes_playing', {playing: dataPlaying.members[0].game.name}) + const yes_playing = t('yes_playing', {playing: data.members[0].game.name}) return

{yes_playing}

}else{ return

diff --git a/pages/components/Minecraft.js b/pages/components/Minecraft.js index 36a0353..60bf663 100644 --- a/pages/components/Minecraft.js +++ b/pages/components/Minecraft.js @@ -8,7 +8,6 @@ export default function Minecraft(props) { const { locale, locales, defaultLocale, pathname } = router const { t, lang } = useTranslation("minecraft") - const [dataMinecraft, setDataMinecraft] = useState({ hits: [] }); const [data, setData] = useState({ hits: [] }); const fail = t('minecraft:fail') useEffect(() => { diff --git a/pages/components/Spotify.js b/pages/components/Spotify.js index 356f7ed..deb8f1f 100644 --- a/pages/components/Spotify.js +++ b/pages/components/Spotify.js @@ -7,21 +7,22 @@ function App () { const router = useRouter() const { locale, locales, defaultLocale, pathname } = router const { t, lang } = useTranslation("common") - const [dataSpotify, setDataSpotify] = useState({ hits: [] }); - useEffect(async () => { - const result = await axios( - '/api/Spotify', - ); - - setDataSpotify(result.dataSpotify); - }, []); - - if (dataSpotify === undefined){ + const [data, setData] = useState({ hits: [] }); + useEffect(() => { + const fetchData = async () => { + const result = await axios( + '/api/Spotify', + ); + setData(result.data); + }; + fetchData(); + }, []); + if (data === undefined){ console.log("[Spotify Web API] データの取得に失敗しました。 / Failed to retrieve data.") return

}else{ - if (dataSpotify.isPlaying){ - const status = dataSpotify.artist + ' / ' + dataSpotify.title + if (data.isPlaying){ + const status = data.artist + ' / ' + data.title const listening = t('listening', {listening: status}) return

{listening}

}else{