0
0
mirror of https://github.com/yude-jp/yude.jp synced 2024-12-23 04:30:11 +09:00

Try to use getServerSideProps(), Cleanup

This commit is contained in:
yude 2021-05-28 11:26:28 +09:00
parent ebf88ba96b
commit 32064d4ba2
Signed by: yude
GPG Key ID: EB0FE5D925C4A968
3 changed files with 26 additions and 41 deletions

View File

@ -1,24 +1,21 @@
import React from "react"; import React from "react";
import useTranslation from 'next-translate/useTranslation' import useTranslation from 'next-translate/useTranslation'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import useRequest from '../lib/useRequest' const url = 'https://discord.com/api/guilds/723409709306216498/widget.json';
const App = () => { function App ({data}){
const router = useRouter() const router = useRouter()
const { locale, locales, defaultLocale, pathname } = router const { locale, locales, defaultLocale, pathname } = router
const { t, lang } = useTranslation("common") const { t, lang } = useTranslation("common")
const { data } = useRequest({
url: 'https://discord.com/api/guilds/723409709306216498/widget.json'
})
if (data === undefined){ if (data === undefined){
console.log("Discord API: データの取得に失敗しました。 / Failed to retrieve data.") console.log("Discord API: データの取得に失敗しました。 / Failed to retrieve data.")
return <p></p> return <p></p>
}else{ }else{
const str = JSON.stringify(data) const str = JSON.stringify(data)
const yes_playing = t('yes_playing', {playing: data.members[0].game.name})
if (str.indexOf("game") !== -1){ if (str.indexOf("game") !== -1){
const yes_playing = t('yes_playing', {playing: data.members[0].game.name})
return <p>{yes_playing}</p> return <p>{yes_playing}</p>
}else{ }else{
return <p></p> return <p></p>
@ -26,4 +23,13 @@ const App = () => {
} }
}; };
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; export default App;

View File

@ -1,18 +1,13 @@
import React from "react"; import React from "react";
import useTranslation from 'next-translate/useTranslation' import useTranslation from 'next-translate/useTranslation'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import useRequest from '../lib/useRequest' const url = '/api/Spotify';
const App = () => { function App ({data}) {
const router = useRouter() const router = useRouter()
const { locale, locales, defaultLocale, pathname } = router const { locale, locales, defaultLocale, pathname } = router
const { t, lang } = useTranslation("common") const { t, lang } = useTranslation("common")
const [isPlaying, setIsPlaying] = React.useState(0);
const { data } = useRequest({
url: '/api/Spotify'
})
if (data === undefined){ if (data === undefined){
console.log("Spotify Web API: データの取得に失敗しました。 / Failed to retrieve data.") console.log("Spotify Web API: データの取得に失敗しました。 / Failed to retrieve data.")
return <p></p> return <p></p>
@ -27,4 +22,13 @@ const App = () => {
}; };
} }
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; export default App;

View File

@ -1,25 +0,0 @@
import useSWR from 'swr'
import axios from 'axios'
export default function useRequest(request, { initialData, ...config } = {}) {
return useSWR(
request && JSON.stringify(request),
() => axios(request || {}).then(response => response.data),
{
...config,
initialData: initialData && {
status: 200,
statusText: 'InitialData',
headers: {},
data: initialData
},
revalidateOnFocus: false,
revalidateOnMount:false,
revalidateOnReconnect: false,
refreshWhenOffline: false,
refreshWhenHidden: false,
refreshInterval: 0,
shouldRetryOnError: false
},
)
}