-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #153 from OperationSpark/change-static-props-to-se…
…rver-side change static to server side props
- Loading branch information
Showing
3 changed files
with
88 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { NextApiRequest as Req, NextApiResponse as Res } from 'next'; | ||
|
||
import { Showcase } from '@this/data/types/gradShowcase'; | ||
import axios, { isAxiosError } from 'axios'; | ||
|
||
import dayjs from 'dayjs'; | ||
|
||
export default async function showcaseHandler(req: Req, res: Res) { | ||
if (req.method === 'GET') { | ||
try { | ||
const { data: showcase } = await axios.get<Showcase>( | ||
'https://showcase.operationspark.org/api/showcases/active', | ||
); | ||
if (!showcase) { | ||
res.status(404).end('Showcase inactive'); | ||
return; | ||
} | ||
|
||
const { startDateTime, doorsOffset, websiteActive, active } = showcase; | ||
|
||
if (!startDateTime || !websiteActive || !active) { | ||
res.status(404).end('Showcase inactive'); | ||
return; | ||
} | ||
|
||
const disableTime = dayjs(startDateTime).add(doorsOffset ?? 30, 'minutes'); | ||
|
||
if (disableTime.isBefore(dayjs())) { | ||
console.info('Showcase inactive'); | ||
res.status(404).end('Showcase inactive'); | ||
return; | ||
} | ||
|
||
res.status(200).json(showcase); | ||
} catch (err) { | ||
if (isAxiosError(err) && err.response?.status === 404) { | ||
console.info('Showcase inactive'); | ||
res.status(404).end('Showcase inactive'); | ||
return; | ||
} | ||
console.error('Showcase fetch error: ', err); | ||
res.status(500).end('Showcase fetch error'); | ||
return; | ||
} | ||
} | ||
|
||
res.status(405).end('Method Not Allowed'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import axios, { isAxiosError } from 'axios'; | ||
|
||
import { Showcase } from '@this/data/types/gradShowcase'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
const getShowcase = async () => { | ||
try { | ||
const { data: showcase } = await axios.get<Showcase>('/api/showcase'); | ||
|
||
if (!showcase) { | ||
return null; | ||
} | ||
|
||
return showcase; | ||
} catch (err) { | ||
if (isAxiosError(err) && err.response?.status === 404) { | ||
console.info('Showcase inactive'); | ||
return null; | ||
} | ||
console.error('Showcase fetch error: ', err); | ||
return null; | ||
} | ||
}; | ||
|
||
export const useShowcase = () => { | ||
const [showcase, setShowcase] = useState<Showcase | null>(); | ||
|
||
useEffect(() => { | ||
getShowcase().then((showcase) => showcase && setShowcase(showcase)); | ||
}, []); | ||
|
||
return { | ||
showcase, | ||
clearShowcase: () => setShowcase(null), | ||
refreshShowcase: () => getShowcase().then((showcase) => showcase && setShowcase(showcase)), | ||
}; | ||
}; |