-
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.
create hook to fetch showcase -- move req to server to prevent CORS i…
…ssue
- Loading branch information
Showing
3 changed files
with
98 additions
and
44 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,45 @@ | ||
import axios, { isAxiosError } from 'axios'; | ||
|
||
import { Showcase } from '@this/data/types/gradShowcase'; | ||
import { toDayJs } from '@this/src/helpers/time'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
const getShowcase = async () => { | ||
try { | ||
const { data } = await axios.get<Showcase>('/api/showcase'); | ||
|
||
if (!data || !data.startDateTime || !data.websiteActive || !data.active) { | ||
return null; | ||
} | ||
|
||
const disableTime = toDayJs(data.startDateTime).add(data.doorsOffset ?? 30, 'minutes'); | ||
|
||
if (disableTime.isBefore(toDayJs())) { | ||
console.info('Showcase inactive'); | ||
return null; | ||
} | ||
|
||
return data; | ||
} 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)), | ||
}; | ||
}; |