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

Dev #144

Merged
merged 2 commits into from
Nov 15, 2024
Merged

Dev #144

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
14 changes: 13 additions & 1 deletion src/component/admin/recruit/AdminRecruitContent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function AdminRecruitContent() {
if (res.data.success) {
setContentData(res.data.data);
// setTotalPages(res.data.data.totalPages);
console.log(res.data.data);
// console.log(res.data.data);
}
} catch (error) {
console.error('Error fetching data : ', error);
Expand All @@ -36,9 +36,21 @@ export default function AdminRecruitContent() {
const onClickList = () => {
navigate('/recruit');
};

const onClickEditContent = () => {
navigate('/admin/recruit/edit', {
state: { recruitId: recruitId },
});
};
return (
<>
<div className={styles.total_div}>
<div className={styles.edit_button_div}>
<button className={styles.edit_button} onClick={onClickEditContent}>
μˆ˜μ •ν•˜κΈ°
</button>
</div>

<p className={styles.content_title}>{contentData?.title}</p>
<div className={styles.date_div}>
<p className={styles.content_date}>{new Date(contentData?.createdAt).toLocaleDateString()}</p>
Expand Down
4 changes: 2 additions & 2 deletions src/component/admin/recruit/AdminRecruitList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export default function AdminRecruitList() {
},
});
if (res.data.success) {
console.log(res.data);
// console.log(res.data);
setPromoteData(res.data.data.content);
setTotalPages(res.data.data.totalPages);
console.log(res.data.data.content);
// console.log(res.data.data.content);
}
} catch (error) {
console.error('Error fetching data : ', error);
Expand Down
198 changes: 111 additions & 87 deletions src/component/admin/recruit/AdminRecruitWrite.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,56 @@ import { customAxios } from '../../../config/axios-config';
import { useEffect, useState, useRef } from 'react';
import ConfirmModal from '../../modal/ConfirmModal';
import ErrorModal from '../../modal/ErrorModal';
import { useNavigate } from 'react-router-dom';
import { useLocation, useNavigate } from 'react-router-dom';
import axios from 'axios';

export default function AdminRecruitWrite() {
const navigate = useNavigate();
const location = useLocation();
const accessToken = localStorage.getItem('accessToken');
const inputFileRef = useRef(null);
const [title, setTitle] = useState('');
const [content, setContent] = useState('');
const [selectedImages, setSelectedImages] = useState([]);
const [selectedFiles, setSelectedFiles] = useState([]);
const [presignedImages, setPresignedImages] = useState([]);
// const [inputValue, setInputValue] = useState('');

//const [presignedImages, setPresignedImages] = useState([]);
// const [editContent, setEditContent] = useState([]);
const [isModalOpen, setIsModalOpen] = useState(false);
const [isErrorModalOpen, setIsErrorModalOpen] = useState(false);
const [modalMessage, setModalMessage] = useState('');
//λͺ¨μ§‘κΈ€ μˆ˜μ •μΈ 경우둜 λ„˜μ–΄μ˜¬ λ•Œ recruitId 쑴재
//κ·Έλƒ₯ λͺ¨μ§‘κΈ€ μž‘μ„±μΈ κ²½μš°λŠ” recruitId 쑴재x
const recruitId = location.state?.recruitId;
//μ‚­μ œν•  νŒŒμΌλ“€μ„ μ €μž₯
const [deletedFiles, setDeletedFiles] = useState([]);
// μƒˆλ‘œ μΆ”κ°€λœ νŒŒμΌλ“€μ„ μ €μž₯
const [newAddedFiles, setNewAddedFiles] = useState([]);
// μƒˆλ‘œ μΆ”κ°€λœ 이미지 URL듀을 μ €μž₯
const [newAddedImages, setNewAddedImages] = useState([]);
// (μ‚­μ œx, μΆ”κ°€x)κΈ°μ‘΄ 이미지듀을 μ €μž₯
const [remainedImages, setRemainedImages] = useState([]);

//λͺ¨μ§‘κΈ€ μˆ˜μ • μ‹œ 데이터 get
const getRecruitData = async () => {
try {
const res = await customAxios.get(`/v1/admins/recruits/${recruitId}`);
if (res.data.success) {
console.log('edit content', res.data);
setTitle(res.data.data.title);
setContent(res.data.data.content);
setSelectedImages(res.data.data.imageUrls);
}
} catch (error) {
console.error(error);
}
};

useEffect(() => {
if (recruitId) {
getRecruitData();
}
}, []);

const closeModal = () => {
setIsModalOpen(false);
Expand Down Expand Up @@ -54,60 +88,52 @@ export default function AdminRecruitWrite() {

const newImageFiles = Array.from(files);
const newImageURLs = newImageFiles.map((file) => URL.createObjectURL(file));
console.log('url', newImageURLs);
console.log('img', newImageFiles);

//λͺ¨μ§‘κΈ€ μˆ˜μ • μ‹œ
if (recruitId) {
// μƒˆλ‘œ μΆ”κ°€λœ 파일과 이미지 URL μƒνƒœ μ—…λ°μ΄νŠΈ
setNewAddedFiles(newImageFiles);
setNewAddedImages(newImageURLs);
}

setSelectedFiles((prevFiles) => {
return [...prevFiles, ...newImageFiles];
});
setSelectedImages((prevImages) => {
return [...prevImages, ...newImageURLs];
});
};

console.log('SS', selectedImages);

// const extension = newImageFiles.map((file) => file.name.split('.').pop().toUpperCase());
// ν™•μž₯자 μΆ”μΆœ

// try {
// const { data } = await customAxios.post(
// '/v1/images/club/recruit',

// {
// recruitImageExtensions: extension,
// },

// {
// headers: {
// Authorization: Bearer ${accessToken},
// 'Content-Type': 'application/json',
// },
// // params: {
// // recruitImageExtensions: [extension],
// // },
// }
// );

// console.log(data);
const handleRemoveImage = (index) => {
setSelectedImages((prevImages) => {
const imageToDelete = prevImages[index]; // μ‚­μ œλ  이미지 찾음
//λͺ¨μ§‘κΈ€ μˆ˜μ • μ‹œ
if (recruitId) {
// deletedFiles 배열에 μ‚­μ œλ  이미지 μΆ”κ°€
setDeletedFiles((prevDeletedFiles) => [...prevDeletedFiles, imageToDelete]);
}

// const imagePresigned = data.data.map((file) => {
// return file.imageUrl;
// });
// ν•„ν„°λ₯Ό μ‚¬μš©ν•˜μ—¬ μ„ νƒλœ 이미지 λͺ©λ‘μ—μ„œ ν•΄λ‹Ή 이미지λ₯Ό 제거
const updatedImages = prevImages.filter((_, i) => i !== index);

// setPresignedImages(imagePresigned); // 이미지 미리보기 URL을 μƒνƒœμ— μ €μž₯
// } catch (error) {
// console.error('이미지 μ—…λ‘œλ“œ μ‹€νŒ¨:', error);
// }
};
//λͺ¨μ§‘κΈ€ μˆ˜μ • μ‹œ
if (recruitId) {
// 남은 이미지듀을 remainingImages μƒνƒœμ— μ €μž₯
setRemainedImages(updatedImages);
}

const handleRemoveImage = (index) => {
setSelectedImages((prevImages) => prevImages.filter((_, i) => i !== index));
// ν•„ν„°λ₯Ό μ‚¬μš©ν•˜μ—¬ μ„ νƒλœ 이미지 λͺ©λ‘μ—μ„œ ν•΄λ‹Ή 이미지λ₯Ό 제거
return updatedImages;
});
};

// console.log('img', selectedImages);

const uploadImages = async () => {
console.log('Ff', selectedFiles);
const extensions = selectedFiles.flat().map((File) => File.name.split('.').pop().toUpperCase());
// console.log('Ff', selectedFiles);
//μ„ νƒλœ 파일 λ°°μ—΄ ν™•μ •
const filesToUpload = recruitId ? newAddedFiles.flat() : selectedFiles.flat();

// 파일 ν™•μž₯자 μΆ”μΆœ
const extensions = filesToUpload.map((file) => file.name.split('.').pop().toUpperCase());

try {
const { data } = await customAxios.post(
Expand All @@ -118,17 +144,17 @@ export default function AdminRecruitWrite() {
}
);

console.log('data', data);

await Promise.all(
data.data.map(async (fileData, index) => {
const file = selectedFiles.flat()[index]; // 파일 κ°€μ Έμ˜€κΈ°
// 파일 κ°€μ Έμ˜€κΈ°
const file = selectedFiles.flat()[index];
await axios.put(fileData.presignedUrl, file, {
headers: { 'Content-Type': file.type },
});
})
);
return data.data.map((file) => file.imageKey); // μ—…λ‘œλ“œλœ 이미지 URL λ°˜ν™˜
// μ—…λ‘œλ“œλœ 이미지 URL λ°˜ν™˜
return data.data.map((file) => file.imageKey);
} catch (error) {
console.error('이미지 μ—…λ‘œλ“œ μ‹€νŒ¨:', error);
throw error;
Expand All @@ -155,54 +181,52 @@ export default function AdminRecruitWrite() {
} else {
try {
const imageUrls = await uploadImages();
const res = await customAxios.post(
`/v1/admins/recruits`,
{
title: title,
content: content,
imageKey: imageUrls,
},
{
headers: {
Authorization: ` Bearer ${accessToken}`,
if (recruitId) {
const combinedImages = [...remainedImages, ...imageUrls];

const res = await customAxios.patch(
`/v1/admins/recruits/${recruitId}`,
{
title: title,
content: content,
deletedImageUrls: deletedFiles,
newImageKeys: imageUrls,
remainImageUrls: remainedImages,
images: combinedImages,
},
{
headers: {
Authorization: ` Bearer ${accessToken}`,
},
}
);
if (res.data.success) {
setIsModalOpen(true);
}
} else {
const res = await customAxios.post(
`/v1/admins/recruits`,
{
title: title,
content: content,
imageKey: imageUrls,
},
{
headers: {
Authorization: ` Bearer ${accessToken}`,
},
}
);
if (res.data.success) {
setIsModalOpen(true);
}
);
if (res.data.success) {
// console.log(res.data);
setIsModalOpen(true);
}
} catch (error) {
console.error('이미지 μ—…λ‘œλ“œ μ‹€νŒ¨:', error);
}
// postRecruitData();
}
};

// const postRecruitData = async () => {
// try {
// const res = await customAxios.post(
// /v1/admins/recruits,
// {
// title: title,
// content: content,
// imageUrl: presignedImages,
// },
// {
// headers: {
// Authorization: Bearer ${accessToken},
// },
// }
// );
// if (res.data.success) {
// console.log(res.data);
// setIsModalOpen(true);
// }
// } catch (error) {
// console.error('Error fetching data : ', error);
// }
// };

return (
<>
<div className={styles.write_header}>λͺ¨μ§‘κΈ€ μž‘μ„±ν•˜κΈ°</div>
Expand Down
16 changes: 16 additions & 0 deletions src/component/admin/recruit/adminRecruitContent.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
margin-left: 20%;
margin-right: 20%;
margin-top: 100px;

padding-bottom: 30px;
}
.content_title {
Expand Down Expand Up @@ -108,3 +109,18 @@
background-color: #7bc8e0;
color: white;
}

.edit_button_div {
display: flex;
justify-content: flex-end;
background-color: #f9f9f9;
}

.edit_button {
padding: 8px 10px;
/* margin-top: 70px; */
border: none;
border-radius: 5px;
background-color: #7bc8e0;
color: white;
}
2 changes: 1 addition & 1 deletion src/component/recruit/RecruitContent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function RecruitContent() {
if (res.data.success) {
setContentData(res.data.data);
// setTotalPages(res.data.data.totalPages);
console.log(res.data.data);
// console.log(res.data.data);
}
} catch (error) {
console.error('Error fetching data : ', error);
Expand Down
2 changes: 1 addition & 1 deletion src/component/recruit/RecruitList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function RecruitList() {
if (res.data.success) {
setPromoteData(res.data.data.content);
setTotalPages(res.data.data.totalPages);
console.log(res.data.data);
// console.log(res.data.data);
}
} catch (error) {
console.error('Error fetching data : ', error);
Expand Down
Loading