Skip to content

Commit

Permalink
Merge pull request #351 from SejongPeer/feature/15
Browse files Browse the repository at this point in the history
feat : [15] 이미지 업로드 & 교내 || 교외 통신 시 URl studyType설정
  • Loading branch information
AhnRian authored Jul 21, 2024
2 parents adceeda + 61c0739 commit 50690bb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/pages/study/studyList/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// src/api/study.js
import axios from 'axios';
import useStudyInfoStore from '../useStudyInfoStore';

export const fetchPosts = async () => {
const accessToken = localStorage.getItem('accessToken');
Expand Down
85 changes: 69 additions & 16 deletions src/pages/study/studyPostWrite/StudyPostWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,80 @@ const StudyPostWrite = props => {
const newImgFiles = imgFiles.filter((_, i) => i !== index);
setImgFiles(newImgFiles);
};
//이미지 업로드
const imgUpload = async id => {
const imgs = [...imgFiles];
const imgData = {
studyId: id,
base64ImagesList: imgs,
};
try {
const response = await fetch(
`${process.env.REACT_APP_BACK_SERVER}/image/study/upload`,
{
method: 'POST',
body: JSON.stringify(imgData),
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${localStorage.getItem('accessToken')}`,
'Refresh-Token': localStorage.getItem('refreshToken'),
},
}
);

const text = await response.text();
const data = text ? JSON.parse(text) : {};
console.log(data);
if (!response.ok) {
throw new Error(data.message || 'Something went wrong');
}

if (data.data !== null) {
errorClassName = data.data.errorClassName;
}
} catch (err) {
console.log('ErrorMessage : ', err.message);
}
};
const [isFilled, setIsFilled] = useState(true);

const submitHandler = async e => {
const formStartDate = format(startDate, 'yyyy-MM-dd HH:mm:ss');
const formEndDate = format(endDate, 'yyyy-MM-dd HH:mm:ss');
const studyData = {
title: title,
content: content,
recruitmentCount: memberNum,
method: selectedWay,
frequency: selectedFrequency,
kakaoLink: studyLink,
questionLink: questionLink,
lectureId: category,
recruitmentStartAt: formStartDate,
recruitmentEndAt: formEndDate,
tags: tags,
images: images,
};
const studyData =
studyType === 'lecture'
? {
title: title,
content: content,
recruitmentCount: memberNum,
method: selectedWay,
frequency: selectedFrequency,
kakaoLink: studyLink,
questionLink: questionLink,
lectureId: category,
recruitmentStartAt: formStartDate,
recruitmentEndAt: formEndDate,
tags: tags,
images: null,
}
: {
title: title,
content: content,
recruitmentCount: memberNum,
method: selectedWay,
frequency: selectedFrequency,
kakaoLink: studyLink,
questionLink: questionLink,
externalActivityId: category,
recruitmentStartAt: formStartDate,
recruitmentEndAt: formEndDate,
tags: tags,
images: null,
};
console.log(studyData);
try {
const response = await fetch(
process.env.REACT_APP_BACK_SERVER + '/study/lecture',
`${process.env.REACT_APP_BACK_SERVER}/study/${studyType}`,
{
method: 'POST',
body: JSON.stringify(studyData),
Expand All @@ -144,10 +195,12 @@ const StudyPostWrite = props => {
}
);

// 응답 본문이 비어있는지 확인
const text = await response.text();
const data = text ? JSON.parse(text) : {};
// console.log('data : ', data.data.id);

const studyId = data.data.id;
imgUpload(studyId);
if (!response.ok) {
throw new Error(data.message || 'Something went wrong');
}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/study/studyPostWrite/usePostStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { create } from 'zustand';

const usePostStore = create(set => ({
title: '',
category: 5,
category: 1,
startDate: null,
endDate: null,
memberNum: 1,
Expand Down

0 comments on commit 50690bb

Please sign in to comment.