Skip to content

Commit

Permalink
feat : 모집글 작성 API 구현을 위한 기본 코드 작성
Browse files Browse the repository at this point in the history
ref : #18
  • Loading branch information
minseoKim-11 committed Nov 9, 2024
1 parent 73ea608 commit 5e1f370
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { verifyCEOAPI, signupUserAPI, signupCEOAPI } from "./signupAPI";
import { loginAPI } from "./loginAPI";
import{} from "./postingAPI";

export {
verifyCEOAPI, // 사업자 인증 API
Expand Down
53 changes: 53 additions & 0 deletions src/api/postingAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { privateAxios } from "../utils/customAxios";

export const postJobPosting = async (accessToken, dispatch, formData) => {
const { doName, siName, detailName } = formData.workLocation;
const [startHour, startMinute] = formData.workTime.start.split(":").map(Number);
const [endHour, endMinute] = formData.workTime.end.split(":").map(Number);
const workDays = formData.workDays.join(", ");

const body = {
postId: 0,
userId: 1,
storeName: formData.storeName,
postData: {
doName,
siName,
detailName,
workType: formData.workTags[0] || "기타",
title: formData.title,
content: formData.description,
pay: parseInt(formData.pay, 10),
workStartHour: startHour,
workStartMinute: startMinute,
workEndHour: endHour,
workEndTimeMinute: endMinute,
isNegotiable: formData.isNegotiable || false,
applyNumber: formData.applyNumber,
workDays,
isShortTermJob: formData.workPeriod === "단기",
payType: formData.payType,
isNumberPublic: formData.isNumberPublic,
imageList: [""], // 일단 이미지는 빈 배열로 냅둠
},
};

const response = {
isSuccess: false,
message: "",
};

try {
// 서버로 API 호출
const result = await privateAxios(accessToken, dispatch).post("/post", body);

if (result.status === 200) {
response.isSuccess = true;
response.message = result.data.message;
}
} catch (error) {
response.message = error.message;
}

return response;
};
23 changes: 18 additions & 5 deletions src/pages/recruitment/Posting.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {InputField,Tag,Toggle,WeekdayPicker,WorkTimePicker,PayPicker,AddressInpu
import "../../styles/posting/Posting.css";
import { POSTING_UPMU_TAG } from "../../constants";

import { postJobPosting } from "../../api";
import { getAccessToken } from "../../utils/getAccessToken";
import { useDispatch } from "react-redux";

const Posting = () => {
const [isOptionSelected, setIsOptionSelected] = useState(false);
Expand Down Expand Up @@ -126,19 +129,29 @@ const Posting = () => {
}
return true; // 유효성 검사 성공
};

const handleSubmit = () => {
const dispatch = useDispatch();
const accessToken = getAccessToken();

const handleSubmit = async () => {
const allValid = Object.values(validStates).every((isValid) => isValid);
if (!allValid) {
alert("모든 필드를 올바르게 입력해주세요.");
return;
}

const payload = createPayload(); // payload 생성 로직 분리
const payload = createPayload(); // payload 생성
if (!validateForm(payload, formData)) return; // 유효성 검사 실패 시 중단
console.log("Payload:", payload);

// API 호출
// axios.post('/api/posting', payload).then(...);
const response = await postJobPosting(accessToken, dispatch, payload.postData);
if (response.isSuccess) {
console.log("성공:", response.message);
alert("성공");// 성공 처리 (예: 알림창, 페이지 이동)
} else {
console.error("실패:", response.message);
alert("실패");
// 실패 처리 (예: 에러 메시지 표시)
}
};

return (
Expand Down

0 comments on commit 5e1f370

Please sign in to comment.