Skip to content

Commit

Permalink
✨ feat: 학력 추가 api 연결(7.6) #73
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeona01 committed Oct 31, 2024
1 parent 67a4599 commit edc73c0
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 102 deletions.
10 changes: 2 additions & 8 deletions src/api/resumes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,8 @@ export const postWorkExperience = async (
};

// 7.6 학력 생성하기
export const postEducation = async ({
id,
education,
}: {
id: number;
education: EducationRequest;
}) => {
const response = await api.post(`/users/resumes/educations/${id}`, education);
export const postEducation = async (education: EducationRequest) => {
const response = await api.post('/users/resumes/educations', education);
return response.data;
};

Expand Down
8 changes: 5 additions & 3 deletions src/components/SetEducation/EducationPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ const EducationPost = ({
<Input
inputType={InputType.TEXT}
placeholder="Grade"
value={String(educationData.grade)}
value={
educationData.grade ? String(educationData.grade) : undefined
}
onChange={(value) => handleInputChange('grade', value)}
canDelete={false}
/>
Expand All @@ -116,7 +118,7 @@ const EducationPost = ({
<Input
inputType={InputType.TEXT}
placeholder="0.0"
value={String(educationData.gpa)}
value={educationData.gpa ? String(educationData.gpa) : undefined}
onChange={(value) => handleInputChange('gpa', value)}
canDelete={false}
/>
Expand All @@ -127,7 +129,7 @@ const EducationPost = ({
Entrance Date <span className="text-[#EE4700] body-1">*</span>
</p>
<Dropdown
value={educationData.start_date.replace(/-/g, '/')}
value={educationData.start_date?.replace(/-/g, '/')}
placeholder="Select Date"
options={[]}
isCalendar={true}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/SetEducation/PatchEducationPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
GetEducationType,
PostEducationType,
} from '@/types/postResume/postEducation';
import { transformToPostEducation } from '@/utils/editResume';
import { transformToPatchEducation } from '@/utils/editResume';
import { useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';

Expand Down Expand Up @@ -52,7 +52,7 @@ const PatchEducationPage = () => {
setFetchData(data);

// patch 타입 initialData 설정
const transformedData = transformToPostEducation(data);
const transformedData = transformToPatchEducation(data);
setInitialData(transformedData);
};

Expand Down
41 changes: 22 additions & 19 deletions src/pages/SetEducation/PostEducationPage.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
import { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import BaseHeader from '@/components/Common/Header/BaseHeader';
import Button from '@/components/Common/Button';
import EducationPost from '@/components/SetEducation/EducationPost';
import { buttonTypeKeys } from '@/constants/components';
import useNavigateBack from '@/hooks/useNavigateBack';
import { InitailEducationType } from '@/types/postResume/postEducation';
import { isPostEducationType } from '@/utils/introduction';

// input 기본값 설정
const InitailEducation = (): InitailEducationType => ({
education_level: '',
school_id: 0,
major: '',
gpa: 0.0,
start_date: '',
end_date: '',
grade: 0,
});
import {
InitailEducationType,
InitialEducationData,
} from '@/types/postResume/postEducation';
import { usePostEducation } from '@/hooks/api/useResume';
import { educationDataValidation } from '@/utils/editResume';
import { EducationRequest } from '@/types/api/resumes';

const PostEducationPage = () => {
const { mutate } = usePostEducation();

const handleBackButtonClick = useNavigateBack();
const navigate = useNavigate();
const [educationData, setEducationData] =
useState<InitailEducationType>(InitailEducation());
useState<InitailEducationType>(InitialEducationData);
const [isValid, setIsValid] = useState<boolean>(false);

const handleSubmit = () => {
// TODO: API - 7.6 학력 생성하기
navigate('/profile/manage-resume');
// 유효성 검사
if (!educationDataValidation(educationData)) return;
// API - 7.6 학력 생성하기
const formattedEducationData = {
...educationData,
gpa: educationData.gpa ? parseFloat(String(educationData.gpa)) : 0, // gpa가 없을 경우 기본값 0 할당
grade: educationData.grade ? Number(educationData.grade) : 0, // grade가 없을 경우 기본값 0 할당
};

mutate(formattedEducationData as EducationRequest);
};

useEffect(() => {
setIsValid(isPostEducationType(educationData));
setIsValid(educationDataValidation(educationData));
console.log(educationData);
}, [educationData]);

return (
Expand Down
62 changes: 36 additions & 26 deletions src/types/postResume/postEducation.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
import { School } from "@/types/api/document"
import { EducationLevelType } from "@/types/postApply/resumeDetailItem"
import { School } from '@/types/api/document';
import { EducationLevelType } from '@/types/postApply/resumeDetailItem';

export type PostEducationType = {
education_level : EducationLevelType // Enum(BACHELOR, ASSOCIATE, HIGHSCHOOL),
school_id : number,
major : string,
gpa : number,
start_date : string // yyyy-MM-dd
end_date : string // yyyy-MM-dd
grade : number
}
education_level: EducationLevelType; // Enum(BACHELOR, ASSOCIATE, HIGHSCHOOL),
school_id: number;
major: string;
gpa: number;
start_date: string; // yyyy-MM-dd
end_date: string; // yyyy-MM-dd
grade: number;
};

export type GetEducationType = {
education_level : EducationLevelType // Enum(BACHELOR, ASSOCIATE, HIGHSCHOOL),
school: School,
major : string,
gpa : number,
start_date : string // yyyy-MM-dd
end_date : string // yyyy-MM-dd
grade : number
}
education_level: EducationLevelType; // Enum(BACHELOR, ASSOCIATE, HIGHSCHOOL),
school: School;
major: string;
gpa: number;
start_date: string; // yyyy-MM-dd
end_date: string; // yyyy-MM-dd
grade: number;
};

export type InitailEducationType = {
education_level : EducationLevelType | '' // Enum(BACHELOR, ASSOCIATE, HIGHSCHOOL),
school_id : number,
major : string,
gpa : number,
start_date : string // yyyy-MM-dd
end_date : string // yyyy-MM-dd
grade : number
}
education_level: string | undefined; // Enum(BACHELOR, ASSOCIATE, HIGHSCHOOL),
school_id: number | undefined;
major: string | undefined;
gpa: number | undefined;
start_date: string | undefined; // yyyy-MM-dd
end_date: string | undefined; // yyyy-MM-dd
grade: number | undefined;
};

export const InitialEducationData = {
education_level: '', // Enum(BACHELOR, ASSOCIATE, HIGHSCHOOL),
school_id: undefined,
major: '',
gpa: undefined,
start_date: '', // yyyy-MM-dd
end_date: '', // yyyy-MM-dd
grade: undefined,
};
79 changes: 66 additions & 13 deletions src/utils/editResume.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
import { MypageCardData } from "@/types/manageResume/manageResume";
import { GetEducationType, PostEducationType } from "@/types/postResume/postEducation";
import { EducationLevels } from '@/constants/manageResume';
import { MypageCardData } from '@/types/manageResume/manageResume';
import {
GetEducationType,
InitailEducationType,
PostEducationType,
} from '@/types/postResume/postEducation';

// 비어있는지 확인
export const isEmptyData = (data: MypageCardData) => {
return (
data === null ||
(Array.isArray(data) && data.length === 0) ||
(typeof data === 'string' && data.trim() === '')
);
};

// 날짜 형식 변경
export const formatDate = (dateString: string) => {
const date = new Date(dateString);
return date.toLocaleDateString("ko-KR", {
year: "numeric",
month: "2-digit",
}).replace(/\.$/, "");
return date
.toLocaleDateString('ko-KR', {
year: 'numeric',
month: '2-digit',
})
.replace(/\.$/, '');
};


// 날짜 형식 포맷팅(서버 데이터와 랜딩되는 데이터간의 변환
// 날짜 형식 포맷팅(서버 데이터와 랜딩되는 데이터간의 변환
export const formatDateToDash = (date: string) => date.replace(/\//g, '-');
export const formatDateToSlash = (date: string) => date.replace(/-/g, '/');


// 변환 함수
export const transformToPostEducation = (data: GetEducationType): PostEducationType => {
export const transformToPatchEducation = (
data: GetEducationType,
): PostEducationType => {
return {
education_level: data.education_level,
school_id: data.school.id, // school 객체에서 id 추출
Expand All @@ -34,4 +41,50 @@ export const transformToPostEducation = (data: GetEducationType): PostEducationT
end_date: data.end_date,
grade: data.grade,
};
};
};

// 학력 추가 데이터 유효성 검사
export const educationDataValidation = (
data: InitailEducationType,
): boolean => {
if (!data) return false;

// 1. education_level은 EducationLevels 배열 안에 있는 값이어야 함
if (data.education_level && !EducationLevels.includes(data.education_level)) {
return false;
}

// 2. school_id는 0 이상의 숫자 값이어야 함
if (typeof data.school_id !== 'number' || data.school_id < 0) {
return false;
}

// 3. major는 string값으로 비어있지 않아야 함
if (typeof data.major !== 'string' || data.major.trim() === '') {
return false;
}

// 4. gpa는 string 형태이지만, 숫자여야 함
if (typeof data.gpa !== 'string' || isNaN(Number(data.gpa))) {
return false;
}

// 5. start_date는 yyyy-mm-dd 형식이어야 함
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
if (data.start_date && !dateRegex.test(data.start_date)) {
return false;
}

// 6. end_date도 start_date와 동일한 형식이어야 함
if (data.end_date && !dateRegex.test(data.end_date)) {
return false;
}

// 7. grade는 숫자여야 함
if (typeof data.grade !== 'string') {
return false;
}

// 모든 조건을 통과하면 true 반환
return true;
};
31 changes: 0 additions & 31 deletions src/utils/introduction.ts

This file was deleted.

0 comments on commit edc73c0

Please sign in to comment.