Skip to content

Commit

Permalink
Merge pull request #264 from mash-up-kr/feature/schedule-api
Browse files Browse the repository at this point in the history
모집 관련 시간 상수값을 모두 recruit schedule api 응답의 시간값으로 대체한다
  • Loading branch information
HaJunRyu authored Jan 28, 2024
2 parents 8d34dbe + 951ce33 commit 23f330e
Show file tree
Hide file tree
Showing 30 changed files with 483 additions and 191 deletions.
30 changes: 26 additions & 4 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import { HOME_PAGE, PREFIX } from '@/constants';
import { getRecruitingProgressStatusFromRecruitingPeriod } from '@/utils/date';
import { CURRENT_GENERATION, HOME_PAGE, PREFIX } from '@/constants';
import { RecruitScheduleArray } from '@/types/dto';
import {
generateRecruitSchedule,
getRecruitingProgressStatusFromRecruitingPeriod,
} from '@/utils/date';
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';

const blockedPaths = Object.values(PREFIX);

export function middleware(request: NextRequest) {
const recruitingProgressStatus = getRecruitingProgressStatusFromRecruitingPeriod(new Date());
export async function middleware(request: NextRequest) {
const recruitScheduleResponse = await fetch(
`${process.env.BASE_URL}/api/applications/schedule/${CURRENT_GENERATION}`,
);

if (!recruitScheduleResponse.ok) {
const url = request.nextUrl.clone();
url.pathname = HOME_PAGE;
return NextResponse.redirect(url);
}

const { data: recruitSchedules }: { data: RecruitScheduleArray } =
await recruitScheduleResponse.json();

const recruitSchedule = generateRecruitSchedule(recruitSchedules);

const recruitingProgressStatus = getRecruitingProgressStatusFromRecruitingPeriod({
date: new Date(),
recruitSchedule,
});
const { pathname } = request.nextUrl;

const isBlockedPath = blockedPaths.find((path) => pathname.includes(path));
Expand Down
74 changes: 43 additions & 31 deletions pages/apply/[platformName].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import { Application, TeamName } from '@/types/dto';
import { GetServerSideProps } from 'next';
import { getSession } from 'next-auth/react';
import { useRouter } from 'next/router';
import { getRecruitingProgressStatusFromRecruitingPeriod } from '@/utils/date';
import {
generateRecruitSchedule,
getRecruitingProgressStatusFromRecruitingPeriod,
} from '@/utils/date';

interface ApplyProps {
application: Application;
Expand All @@ -38,39 +41,26 @@ const Apply = ({ application, isSubmitted }: ApplyProps) => {
};

export const getServerSideProps: GetServerSideProps = async (context) => {
const recruitingProgressStatus = getRecruitingProgressStatusFromRecruitingPeriod(new Date());

if (recruitingProgressStatus !== 'IN-RECRUITING') {
return {
redirect: {
permanent: false,
destination: HOME_PAGE,
},
};
}
try {
const { data: recruitScheduleResponse } = await applicationApiService.getRecruitSchedule({
generationNumber: CURRENT_GENERATION,
});

const session = await getSession(context);
const recruitSchedule = generateRecruitSchedule(recruitScheduleResponse);

if (!session) {
return {
redirect: {
permanent: false,
destination: HOME_PAGE,
},
};
}

try {
const teams = (
await teamApiService.getTeams({
accessToken: session?.accessToken,
generationNumber: CURRENT_GENERATION,
})
).data;
const recruitingProgressStatus = getRecruitingProgressStatusFromRecruitingPeriod({
date: new Date(),
recruitSchedule,
});

const teamIds = teams.reduce<Record<TeamName, number>>((acc, { name, teamId }) => {
return { ...acc, [name]: teamId };
}, {} as Record<TeamName, number>);
if (recruitingProgressStatus !== 'IN-RECRUITING') {
return {
redirect: {
permanent: false,
destination: HOME_PAGE,
},
};
}

const currentApplyPlatform = teamNames[context.params?.platformName as Teams];

Expand All @@ -83,6 +73,28 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
};
}

const session = await getSession(context);

if (!session) {
return {
redirect: {
permanent: false,
destination: HOME_PAGE,
},
};
}

const teams = (
await teamApiService.getTeams({
accessToken: session?.accessToken,
generationNumber: CURRENT_GENERATION,
})
).data;

const teamIds = teams.reduce<Record<TeamName, number>>((acc, { name, teamId }) => {
return { ...acc, [name]: teamId };
}, {} as Record<TeamName, number>);

const applications = (
await applicationApiService.getApplications({
accessToken: session?.accessToken,
Expand Down
1 change: 0 additions & 1 deletion pages/faq/[platformName].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export const getStaticProps: GetStaticProps<PlatformProps, Params> = async (cont
platformName,
questions: transformer({ blocks: data.valueMap.editorData.blocks }),
},
revalidate: 60 * 60 * 24,
};
};

Expand Down
46 changes: 38 additions & 8 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { applicationApiService } from '@/api/services';
import {
HomeLayout,
RecruitingDetailNavigation,
Expand All @@ -7,32 +8,51 @@ import {
RecruitingPeriod,
RecruitingRemainder,
} from '@/components';
import { CURRENT_GENERATION } from '@/constants';

import { useAOS } from '@/hooks';
import { getRecruitingProgressStatusFromRecruitingPeriod } from '@/utils/date';
import { RecruitScheduleArray } from '@/types/dto';
import {
generateRecruitSchedule,
getRecruitingProgressStatusFromRecruitingPeriod,
} from '@/utils/date';
import type { RecruitingProgressStatus } from '@/utils/date';
import { GetStaticProps } from 'next';
import { useEffect, useState } from 'react';

const Home = () => {
interface HomeProps {
recruitScheduleArray: RecruitScheduleArray;
}

const Home = ({ recruitScheduleArray }: HomeProps) => {
useAOS();

const recruitSchedule = generateRecruitSchedule(recruitScheduleArray);

const [recruitingProgressStatus, setRecruitingProgressStatus] = useState<
RecruitingProgressStatus | 'NOT_INITIALIZED'
>('NOT_INITIALIZED');

useEffect(() => {
setRecruitingProgressStatus(getRecruitingProgressStatusFromRecruitingPeriod(new Date()));
}, []);
setRecruitingProgressStatus(
getRecruitingProgressStatusFromRecruitingPeriod({
date: new Date(),
recruitSchedule,
}),
);
}, [recruitSchedule]);

return (
<>
{recruitingProgressStatus === 'PREVIOUS' && <RecruitingRemainder />}
{recruitingProgressStatus === 'PREVIOUS' && (
<RecruitingRemainder recruitSchedule={recruitSchedule} />
)}
{recruitingProgressStatus !== 'PREVIOUS' && (
<HomeLayout visibility={recruitingProgressStatus !== 'NOT_INITIALIZED'}>
<WelcomeHero />
<RecruitingOpenHero />
<RecruitingPeriod />
<RecruitingProcess />
<RecruitingOpenHero recruitSchedule={recruitSchedule} />
<RecruitingPeriod recruitSchedule={recruitSchedule} />
<RecruitingProcess recruitSchedule={recruitSchedule} />
<RecruitingDetailNavigation />
</HomeLayout>
)}
Expand All @@ -41,3 +61,13 @@ const Home = () => {
};

export default Home;

export const getStaticProps: GetStaticProps<HomeProps> = async () => {
const { data: recruitScheduleResponse } = await applicationApiService.getRecruitSchedule({
generationNumber: CURRENT_GENERATION,
});

return {
props: { recruitScheduleArray: recruitScheduleResponse },
};
};
32 changes: 16 additions & 16 deletions pages/my-page/apply-status.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import { applicationApiService } from '@/api/services';
import { ApplyStatusLayout } from '@/components';
import { ERROR_PAGE, HOME_PAGE } from '@/constants';
import { Application } from '@/types/dto';
import { CURRENT_GENERATION, ERROR_PAGE, HOME_PAGE } from '@/constants';
import { Application, RecruitScheduleArray } from '@/types/dto';
import { sortByGenerationAndTeam } from '@/utils/application';
import {
getRecruitingProgressStatusFromRecruitingPeriod,
RecruitingProgressStatus,
} from '@/utils/date';
import { generateRecruitSchedule } from '@/utils/date';
import { GetServerSideProps } from 'next';
import { getSession } from 'next-auth/react';

interface ApplyStatusProps {
applications: Application[];
recruitingProgressStatus: RecruitingProgressStatus;
recruitScheduleArray: RecruitScheduleArray;
}

const ApplyStatus = ({ applications, recruitingProgressStatus }: ApplyStatusProps) => {
return (
<ApplyStatusLayout
applications={applications}
recruitingProgressStatus={recruitingProgressStatus}
/>
);
const ApplyStatus = ({
applications,

recruitScheduleArray,
}: ApplyStatusProps) => {
const recruitSchedule = generateRecruitSchedule(recruitScheduleArray);

return <ApplyStatusLayout applications={applications} recruitSchedule={recruitSchedule} />;
};

export default ApplyStatus;
Expand All @@ -41,9 +39,11 @@ export const getServerSideProps: GetServerSideProps = async (context) => {

const applications = sortByGenerationAndTeam(applicationsRes.data);

const recruitingProgressStatus = getRecruitingProgressStatusFromRecruitingPeriod(new Date());
const { data: recruitScheduleResponse } = await applicationApiService.getRecruitSchedule({
generationNumber: CURRENT_GENERATION,
});

return { props: { applications, recruitingProgressStatus } };
return { props: { applications, recruitScheduleArray: recruitScheduleResponse } };
} catch (error) {
return {
redirect: { destination: ERROR_PAGE, permanent: false },
Expand Down
24 changes: 18 additions & 6 deletions pages/recruit/[platformName].tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GetStaticPaths, GetStaticProps, NextPage } from 'next';
import { ParsedUrlQuery } from 'querystring';
import { PlatformKey, platformKeys, platformMap, platforms } from '@/constants';
import { CURRENT_GENERATION, PlatformKey, platformKeys, platformMap, platforms } from '@/constants';
import parser from '@/utils/editorjs-html';
import { unescape, flow } from 'lodash-es';
import {
Expand All @@ -16,19 +16,22 @@ import {
RecruitEditorContainer,
SEO,
} from '@/components';
import { adminApiService } from '@/api/services';
import { adminApiService, applicationApiService } from '@/api/services';
import { objectKeys } from '@/utils/object';
import { RecruitScheduleArray } from '@/types/dto';
import { generateRecruitSchedule } from '@/utils/date';

interface Params extends ParsedUrlQuery {
platformName: PlatformKey;
}

interface PlatformProps {
platformName: PlatformKey;
recruitScheduleArray: RecruitScheduleArray;
html: string;
}

const Platform: NextPage<PlatformProps> = ({ platformName, html }) => {
const Platform: NextPage<PlatformProps> = ({ platformName, recruitScheduleArray, html }) => {
const {
name: currentPlatformName,
role: currentPlatformRole,
Expand All @@ -38,6 +41,8 @@ const Platform: NextPage<PlatformProps> = ({ platformName, html }) => {

const otherPlatforms = platforms.filter(({ key }) => key !== platformName);

const recruitSchedule = generateRecruitSchedule(recruitScheduleArray);

return (
<RecruitLayout>
<SEO
Expand All @@ -49,12 +54,15 @@ const Platform: NextPage<PlatformProps> = ({ platformName, html }) => {
<RecruitContents>
<RecruitEditorContainer dangerouslySetInnerHTML={{ __html: html }} />
<ActionGroup>
<ApplyLinkButton applyPath={currentPlatformPath.apply} />
<ApplyLinkButton
applyPath={currentPlatformPath.apply}
recruitSchedule={recruitSchedule}
/>
</ActionGroup>
</RecruitContents>
<Divider />
<NavigationHeader />
<BottomNavigation platforms={otherPlatforms} />
<BottomNavigation platforms={otherPlatforms} recruitSchedule={recruitSchedule} />
</RecruitLayout>
);
};
Expand All @@ -75,6 +83,10 @@ export const getStaticProps: GetStaticProps<PlatformProps, Params> = async (cont

const removeWrongAmpString = (value: string) => value.replace(/&amp;/g, '&');

const { data: recruitScheduleResponse } = await applicationApiService.getRecruitSchedule({
generationNumber: CURRENT_GENERATION,
});

const { data } = await adminApiService.getRecruitDataFromStorage({
accessToken: process.env.ADMIN_TOKEN,
key: platformName,
Expand All @@ -88,9 +100,9 @@ export const getStaticProps: GetStaticProps<PlatformProps, Params> = async (cont
return {
props: {
platformName,
recruitScheduleArray: recruitScheduleResponse,
html,
},
revalidate: 60 * 60 * 24,
};
};

Expand Down
11 changes: 11 additions & 0 deletions src/api/services/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
ConfirmApplicantResponse,
CreateMyApplicationRequest,
CreateMyApplicationResponse,
RecruitScheduleRequest,
RecruitScheduleResponse,
SubmitApplicationRequest,
SubmitApplicationResponse,
TempSaveApplicationRequest,
Expand Down Expand Up @@ -86,6 +88,15 @@ class ApplicationApiService extends BaseApiService {
.then(BaseApiService.handleResponse)
.catch(BaseApiService.handleError);
}

public getRecruitSchedule({
generationNumber,
}: RecruitScheduleRequest): Promise<RecruitScheduleResponse> {
return this.http
.get(`/schedule/${generationNumber}`)
.then(BaseApiService.handleResponse)
.catch(BaseApiService.handleError);
}
}

export default new ApplicationApiService();
Loading

0 comments on commit 23f330e

Please sign in to comment.