0
0
mirror of https://github.com/yude-jp/yude.jp synced 2024-11-17 11:02:40 +09:00
yude.jp/pages/components/Discord/DiscordPlaying.js

44 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-05-28 11:43:00 +09:00
import React, { useState, useEffect } from 'react';
import axios from 'axios';
2021-05-25 16:45:46 +09:00
import useTranslation from 'next-translate/useTranslation'
import { useRouter } from 'next/router'
2021-05-28 11:43:00 +09:00
function App (){
2021-05-25 16:45:46 +09:00
const router = useRouter()
const { locale, locales, defaultLocale, pathname } = router
const { t, lang } = useTranslation("common")
2021-05-30 17:59:08 +09:00
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.")
2021-05-28 10:42:03 +09:00
return <p></p>
}else{
2021-05-30 17:59:08 +09:00
const str = JSON.stringify(data)
2021-05-28 10:42:03 +09:00
if (str.indexOf("game") !== -1){
2021-05-30 17:59:08 +09:00
const yes_playing = t('yes_playing', {playing: data.members[0].game.name})
2021-05-25 16:45:46 +09:00
return <p>{yes_playing}</p>
}else{
2021-09-24 17:25:26 +09:00
return <></>
2021-05-25 16:45:46 +09:00
}
2021-05-28 10:42:03 +09:00
}
2021-05-25 16:45:46 +09:00
};
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 } }
}
2021-05-25 16:45:46 +09:00
export default App;