Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change static to server side props #153

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions pages/api/showcase.ts
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');
}
45 changes: 3 additions & 42 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import axios, { isAxiosError } from 'axios';
import type { GetStaticProps, NextPage } from 'next';
import { useState } from 'react';

import { IHome } from '../data/types/home';
import { getStaticAsset } from './api/static/[asset]';
Expand All @@ -20,9 +18,7 @@ import { ILogo } from '@this/data/types/logos';
import AtlantaPromo from '@this/src/components/Elements/AtlantaPromo';
import PromoVideo from '@this/src/components/Home/PromoVideo';
import ShowcasePromo from '@this/src/components/Home/ShowcasePromo';
import { toDayJs } from '@this/src/helpers/time';

// const gCloudBaseUrl = 'https://storage.googleapis.com/operationspark-org';
import { useShowcase } from '@this/src/hooks/useShowcase';

interface HomeProps extends IHome {
logos: ILogo[];
Expand All @@ -35,17 +31,12 @@ const Home: NextPage<HomeProps> = ({
greatCompanies,
programsForAll,
teamEffort,
showcase,
}) => {
const [showcaseInfo, setShowcaseInfo] = useState<Showcase | null>(showcase || null);
const { showcase, clearShowcase } = useShowcase();

return (
<Main style={{ paddingTop: 0 }}>
{showcaseInfo ? (
<ShowcasePromo info={showcaseInfo} clearShowcase={() => setShowcaseInfo(null)} />
) : (
<TopCard />
)}
{showcase ? <ShowcasePromo info={showcase} clearShowcase={clearShowcase} /> : <TopCard />}

<AtlantaPromo />
<PromoVideo />
Expand All @@ -59,49 +50,19 @@ const Home: NextPage<HomeProps> = ({
);
};

const getShowcase = async () => {
try {
const { data } = await axios.get<Showcase>(
'https://showcase.operationspark.org/api/showcases/active',
);

if (!data || !data.startDateTime || !data.websiteActive || !data.active) {
return null;
}

const disableTime = toDayJs(data.startDateTime).add(data.doorsOffset ?? 30, 'minutes');

if (disableTime.isBefore(toDayJs())) {
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 getStaticProps: GetStaticProps<HomeProps> = async () => {
const { greatCompanies, programsForAll, igniteCareer, teamEffort }: IHome = await getStaticAsset(
'index',
);
const logos: ILogo[] = await getStaticAsset('logos', 'partners');

const showcase = await getShowcase();

return {
props: {
greatCompanies,
programsForAll,
igniteCareer,
teamEffort,
logos,
showcase,
},
};
};
Expand Down
37 changes: 37 additions & 0 deletions src/hooks/useShowcase.ts
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)),
};
};
Loading