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

Refactor/#294: MajorProvider에서 졸업요건 링크 상태 삭제 #295

Merged
merged 4 commits into from
Dec 12, 2023
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
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