0
0
mirror of https://github.com/yude-jp/yude.jp synced 2024-06-09 07:16:02 +09:00

Fix: Discord playing data fetching

This commit is contained in:
yude 2021-12-11 21:13:44 +09:00
parent 22385a42c8
commit 52c8cd8d1c
Signed by: yude
GPG Key ID: EB0FE5D925C4A968

View File

@ -2,44 +2,37 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
// Data fetching // Data fetching
import axios from 'axios'; import useSwr from 'swr'
const fetcher = (url) => fetch(url).then((res) => res.json())
// i18n // i18n
import { useTranslation, useLanguageQuery } from 'next-export-i18n'; import { useTranslation, useLanguageQuery } from 'next-export-i18n';
function App (){ export default function DiscordPlaying() {
const [data, setData] = useState({ hits: [] }); const { t } = useTranslation();
useEffect(() => { const [query] = useLanguageQuery();
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 <p></p>
}else{
const str = JSON.stringify(data)
if (str.indexOf("game") !== -1){ const { data, error } = useSwr(
const yes_playing = t('yes_playing', {playing: data.members[0].game.name}) `https://discord.com/api/guilds/723409709306216498/widget.json`,
return <p>{yes_playing}</p> fetcher
}else{ )
return <></> if (error){
} console.log("[Discord API] データの取得に失敗しました。 / Failed to retrieve data.")
return <></>
} else {
if (!data) {
return <p>Loading...</p>
} else {
if (data.members) {
if (data.members[0].game) {
const yes_playing = t('yes_playing', {playing: data.members[0].game.name})
return <p>{yes_playing}</p>
} else {
return <></>
}
} else {
return <></>
}
}
} }
};
export async function getServerSideProps() {
// Fetch data from external API
const res = await fetch(url)
const data = await res.json()
// Pass data to the page via props
return { props: { data } }
} }
export default App;