Skip to content

Commit

Permalink
Merge pull request #295 from GDSC-PKNU-Official/refactor/#294
Browse files Browse the repository at this point in the history
  • Loading branch information
hwinkr authored Dec 12, 2023
2 parents 6d43064 + bcca8d6 commit 90efaca
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 28 deletions.
2 changes: 0 additions & 2 deletions src/components/List/DepartmentList/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ jest.mock('@hooks/useModals', () => {
});

describe.skip('학과선택 테스트', () => {
const mockGraduationLink = 'https://ce.pknu.ac.kr/ce/2889';
const mockUseMajor = useMajor as jest.MockedFunction<typeof useMajor>;
const mockSetMajor = jest.fn();

beforeEach(() => {
mockUseMajor.mockReturnValue({
setMajor: mockSetMajor,
major: '컴퓨터공학과',
graduationLink: mockGraduationLink,
});
});

Expand Down
23 changes: 2 additions & 21 deletions src/components/Providers/MajorProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,22 @@
import http from '@apis/http';
import MajorContext from '@contexts/major';
import { AxiosResponse } from 'axios';
import React, { useEffect, useState } from 'react';

interface GraduationLink {
department: string;
link: string | null;
}

interface MajorProviderProps {
children: React.ReactNode;
}

const MajorProvider = ({ children }: MajorProviderProps) => {
const [major, setMajor] = useState<string | null>(null);
const [graduationLink, setGraduationLink] = useState<string | null>('');

useEffect(() => {
const storedMajor = localStorage.getItem('major');
if (!storedMajor) return;

setMajor(storedMajor);
}, []);

useEffect(() => {
if (!major) return;

(async () => {
const response: AxiosResponse<GraduationLink> = await http.get(
`/api/graduation?major=${major}`,
);
const graduationLink = response.data.link;
setGraduationLink(graduationLink);
})();
}, [major]);

return (
<MajorContext.Provider value={{ major, setMajor, graduationLink }}>
<MajorContext.Provider value={{ major, setMajor }}>
{children}
</MajorContext.Provider>
);
Expand Down
1 change: 0 additions & 1 deletion src/contexts/major.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { createContext } from 'react';
interface MajorState {
major: Major;
setMajor: React.Dispatch<React.SetStateAction<Major>>;
graduationLink: string | null;
}

const MajorContext = createContext<MajorState | null>(null);
Expand Down
14 changes: 12 additions & 2 deletions src/pages/Home/components/InformCardList.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import http from '@apis/http';
import InformCard from '@components/Card/InformCard';
import { ANNOUNCEMENT_TITLE } from '@constants/announcement';
import PATH from '@constants/path';
import useMajor from '@hooks/useMajor';
import useRouter from '@hooks/useRouter';
import openLink from '@utils/router/openLink';
import { AxiosResponse } from 'axios';
import React from 'react';

const InformCardList = () => {
const { graduationLink } = useMajor();
const { major } = useMajor();
const { routerTo } = useRouter();

const onGraduationCardClick = async () => {
const response: AxiosResponse<string> = await http.get(
`/api/graduation?major=${major}`,
);
const graduationLink = response.data;
openLink(graduationLink);
};

return (
<>
<InformCard
Expand All @@ -28,7 +38,7 @@ const InformCardList = () => {
icon="school"
title="졸업요건"
majorRequired={true}
onClick={() => openLink(graduationLink)}
onClick={onGraduationCardClick}
/>
</>
);
Expand Down
2 changes: 0 additions & 2 deletions src/pages/MajorDecision/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ jest.mock('react-router-dom', () => ({
}));

describe.skip('학과선택 페이지 로직 테스트', () => {
const mockGraduationLink = 'https://ce.pknu.ac.kr/ce/2889';
const mockSetMajor = jest.fn();

beforeEach(() => {
Expand All @@ -36,7 +35,6 @@ describe.skip('학과선택 페이지 로직 테스트', () => {
value={{
major: null,
setMajor: mockSetMajor,
graduationLink: mockGraduationLink,
}}
>
<MajorDecision />
Expand Down

0 comments on commit 90efaca

Please sign in to comment.