Skip to content

Commit

Permalink
Merge pull request #152 from nori-dongsan/community/#146
Browse files Browse the repository at this point in the history
Community/#146
  • Loading branch information
aeuna authored Jul 22, 2022
2 parents 75ed990 + 528666b commit f53ac7d
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 70 deletions.
79 changes: 55 additions & 24 deletions components/common/WriteHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ export default function WriteHeader() {
});

const res = await postCommunity(formData);

const {
data: { data, status },
data: { status },
} = res;
setNewPostInfo({
category: 'ํ›„๊ธฐ',
title: '',
content: '',
});
if (status === 201) router.push(`/community/${data.boardId}`);

if (status === 201) {
setNewPostInfo({
category: 'ํ›„๊ธฐ',
title: '',
content: '',
});
router.push(`/community/${res.data.data.boardId}`);
}
};

const handleCancel = () => {
Expand All @@ -61,31 +65,58 @@ export default function WriteHeader() {
isChangeContent,
isChangeImageList,
} = isChangeCommunity;

const updatePostInfo: PutCommunityBody = {};
const formData = new FormData();

if (isChangeCategory) formData.append('category', category);
if (isChangeTitle) {
formData.append('title', title);
updatePostInfo.title = title;
}
if (isChangeContent) {
formData.append('content', content);
updatePostInfo.content = content;
}
if (isChangeImageList) {
if (imageList)
imageList.map((image) => {
formData.append('imageList', image.file);
});
}

if (isChangeCategory) updatePostInfo.category = category;
if (isChangeTitle) updatePostInfo.title = title;
if (isChangeContent) updatePostInfo.content = content;
if (isChangeImageList) updatePostInfo.imageList = imageList;
console.log('==check==');
console.log(formData.get('title'));
console.log(formData.get('content'));
console.log(formData.get('category'));
// console.log(imageList);

if (updatePostInfo.title === '' || updatePostInfo.content === '') {
alert('๋‚ด์šฉ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.');
return;
}

const data = await putCommunity(String(query.cid), updatePostInfo);
setNewPostInfo({
category: 'ํ›„๊ธฐ',
title: '',
content: '',
});
setIsChangeCommunity({
isChangeCategory: false,
isChangeTitle: false,
isChangeContent: false,
isChangeImageList: false,
});
router.push(`/community/${data.id}`);
const res = await putCommunity(String(query.cid), formData);
const {
data: { status },
} = res;

console.log('== check ์ˆ˜์ • ==');
console.log(res);
if (status === 200) {
setNewPostInfo({
category: 'ํ›„๊ธฐ',
title: '',
content: '',
});
setIsChangeCommunity({
isChangeCategory: false,
isChangeTitle: false,
isChangeContent: false,
isChangeImageList: false,
});
router.push(`/community/${res.data.data.boardId}`);
}
};

return (
Expand Down
13 changes: 6 additions & 7 deletions core/api/community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ export const deleteCommunity = async (id: string) => {
}
};

export const putCommunity = async (id: string, body: PutCommunityBody) => {
try {
const { data } = await baseInstance.put(`/board/${id}`, body);
return data;
} catch (e) {
console.log(e);
}
export const putCommunity = (id: string, body: FormData) => {
return baseInstance.put(`/board/${id}`, body, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
};

export const postReply = async (body: PostCommentBody) => {
Expand Down
22 changes: 20 additions & 2 deletions pages/community/[cid].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function CommunityDetail({

if (val && cid) {
const status = await deleteCommunity(cid);
if (status === 200) router.push('/community');
if (status === 200) router.replace('/community');
}
};

Expand Down Expand Up @@ -120,7 +120,13 @@ export default function CommunityDetail({
<DetailFloatingBtn heartNum={0} replyNum={replyCount} />
{isExpanded && (
<StExpandedImgWrapper onClick={() => handleExpanded()}>
<StExpandedImg src={expandedImg} alt="expanded" />
<StExpandedImg
src={
'https://nori-community.s3.ap-northeast-2.amazonaws.com/' +
expandedImg
}
alt="expanded"
/>
</StExpandedImgWrapper>
)}
</StCommunityMain>
Expand All @@ -138,6 +144,8 @@ export const getServerSideProps: GetServerSideProps<Props, Params> = async ({
params,
}) => {
const res = await getCommunityDetail(params!.cid);
console.log('==์ปค๋ฎค๋‹ˆํ‹ฐ ๋””ํ…Œ์ผ==');
console.log(res);
return {
props: {
data: { ...res.data.data, id: params!.cid },
Expand Down Expand Up @@ -308,3 +316,13 @@ const StExpandedImg = styled.img`
left: 50%;
transform: translate(-50%, -50%);
`;

// decodeURIComponent(
// 'https://nori-image.s3.ap-northeast-2.amazonaws.com/์žฅ๋‚œ๊ฐ์ ๋นต/[๋Œ€์—ฌ](๋ฏธ๊ฐœ๋ด‰์ƒˆ์ƒํ’ˆ) ํƒ€์ด๋‹ˆ๋Ÿฌ๋ธŒ ์ˆ˜๋”์•ค๊ทธ๋ฃจ๋ธŒ ํ”„๋ฆฐ์„ธ์Šคํ…Œ์ผ์ฆˆ๋ชจ๋นŒ (ํ‘๋ฐฑ+์นผ๋ผ+๊ฑฐ์น˜๋Œ€+๊ฑด์ „์ง€ํฌํ•จ) ํ”Œ๋Ÿฌ์ŠคํŒจํ‚ค์ง€.jpg',
// )

// encodeURIComponent(
// 'https://nori-image.s3.ap-northeast-2.amazonaws.com/์žฅ๋‚œ๊ฐ์ ๋นต/[๋Œ€์—ฌ](๋ฏธ๊ฐœ๋ด‰์ƒˆ์ƒํ’ˆ) ํƒ€์ด๋‹ˆ๋Ÿฌ๋ธŒ ์ˆ˜๋”์•ค๊ทธ๋ฃจ๋ธŒ ํ”„๋ฆฐ์„ธ์Šคํ…Œ์ผ์ฆˆ๋ชจ๋นŒ (ํ‘๋ฐฑ%2b์นผ๋ผ%2b๊ฑฐ์น˜๋Œ€%2b๊ฑด์ „์ง€ํฌํ•จ) ํ”Œ๋Ÿฌ์ŠคํŒจํ‚ค์ง€.jpg',
// )

// let replaced_str = str.replace(/\+/g, '%2b');
72 changes: 35 additions & 37 deletions pages/write/[cid].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,30 @@ export default function UpdateForm({
setTitle(data.title);

const imageList: ImgData[] = [];
data.imageList.forEach(async (image, index) => {
const file = await convertURLtoFile(image);
setImagesSize((prev) => prev + file.size);
imageList.push({
id: index,
src: image,
file: file,
});
});
// data.imageList.forEach(async (image, index) => {
// const file = await convertURLtoFile(
// 'https://nori-community.s3.ap-northeast-2.amazonaws.com/' + image,
// );
// setImagesSize((prev) => prev + file.size);
// imageList.push({
// id: index,
// src: image,
// file: file,
// });
// });
setImages(imageList);
}, []);

const menu = ['ํ›„๊ธฐ', '์งˆ๋ฌธ', '์ •๋ณด ๊ณต์œ '];

const convertURLtoFile = async (url: string) => {
const response = await fetch(url);
const data = await response.blob();
const ext = url.split('.').pop(); // url ๊ตฌ์กฐ์— ๋งž๊ฒŒ ์ˆ˜์ •ํ•  ์˜ˆ์ •
const filename = url.split('/').pop(); // url ๊ตฌ์กฐ์— ๋งž๊ฒŒ ์ˆ˜์ •ํ•  ์˜ˆ์ •
const metadata = { type: `image/${ext}` };
return new File([data], filename!, metadata);
};
// const convertURLtoFile = async (url: string) => {
// const response = await fetch(url);
// const data = await response.blob();
// const ext = url.split('.').pop(); // url ๊ตฌ์กฐ์— ๋งž๊ฒŒ ์ˆ˜์ •ํ•  ์˜ˆ์ •
// const filename = url.split('/').pop(); // url ๊ตฌ์กฐ์— ๋งž๊ฒŒ ์ˆ˜์ •ํ•  ์˜ˆ์ •
// const metadata = { type: `image/${ext}` };
// return new File([data], filename!, metadata);
// };

const handleIsCategory = () => {
setIsCategory((prev) => !prev);
Expand Down Expand Up @@ -90,18 +92,13 @@ export default function UpdateForm({
return;
}
setImagesSize(totalImagesSize);

const formData = new FormData();
images.map((image) => formData.append(image.id + '', image.file));
imageList.map((image) => formData.append(image.id + '', image.file));

setIsChangeCommunity({
...isChangeCommunity,
isChangeImageList: true,
});
setNewPostInfo({
...newPostInfo,
imageList: formData,
imageList,
});
setImages([...images, ...imageList]);
};
Expand All @@ -111,16 +108,13 @@ export default function UpdateForm({
const delImg = images.filter((image) => image.id === id);

setImagesSize((prev) => prev - delImg[0].file.size);

const formData = new FormData();
imgDelData.map((image) => formData.append(image.id + '', image.file));
setIsChangeCommunity({
...isChangeCommunity,
isChangeImageList: true,
});
setNewPostInfo({
...newPostInfo,
imageList: formData,
imageList: imgDelData,
});
setImages(imgDelData);
};
Expand Down Expand Up @@ -212,7 +206,13 @@ export default function UpdateForm({
{images.length > 0 &&
images.map((image) => (
<StPreviewImgWrapper key={image.id}>
<StPreviewImg src={image.src} alt={String(image.id)} />
<StPreviewImg
src={
'https://nori-community.s3.ap-northeast-2.amazonaws.com/' +
image.src
}
alt={String(image.id)}
/>
<StIcDelete onClick={() => handleDeleteImg(image.id)} />
</StPreviewImgWrapper>
))}
Expand All @@ -234,18 +234,16 @@ interface Params extends ParsedUrlQuery {
export const getServerSideProps: GetServerSideProps<Props, Params> = async ({
params,
}) => {
const data = await getCommunityDetail(params!.cid);
const res = await getCommunityDetail(params!.cid);
console.log('==์ปค๋ฎค๋‹ˆํ‹ฐ ๋””ํ…Œ์ผ ์ˆ˜์ •==');
console.log(res);
return {
props: {
data: {
title: '์žฅ๋‚œ๊ฐ ํ›„๊ธฐ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค.',
category: '์ •๋ณด ๊ณต์œ ',
content:
'๊ตฐ์ธ ๋˜๋Š” ๊ตฐ๋ฌด์›์ด ์•„๋‹Œ ๊ตญ๋ฏผ์€ ๋Œ€ํ•œ๋ฏผ๊ตญ์˜ ์˜์—ญ์•ˆ์—์„œ๋Š” ์ค‘๋Œ€ํ•œ ๊ตฐ์‚ฌ์ƒ ๊ธฐ๋ฐ€ยท์ดˆ๋ณ‘ยท์ดˆ์†Œยท์œ ๋…์Œ์‹๋ฌผ๊ณต๊ธ‰ยทํฌ๋กœยท๊ตฐ์šฉ๋ฌผ์— ๊ด€ํ•œ ์ฃ„์ค‘ ๋ฒ•๋ฅ ์ด ์ •ํ•œ ๊ฒฝ์šฐ์™€ ๋น„์ƒ๊ณ„์—„์ด ์„ ํฌ๋œ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ๊ตฐ์‚ฌ๋ฒ•์›์˜ ์žฌํŒ์„ ๋ฐ›์ง€ ์•„๋‹ˆํ•œ๋‹ค.\n ์„ ๊ฑฐ์— ๊ด€ํ•œ ๊ฒฝ๋น„๋Š” ๋ฒ•๋ฅ ์ด ์ •ํ•˜๋Š” ๊ฒฝ์šฐ....๊ฒฝ์šฐ์™€ ๋น„์ƒ๊ณ„์—„์ด ์„ ํฌ๋œ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ๊ตฐ์‚ฌ๋ฒ•์›์˜ ์žฌํŒ์„ ๋ฐ›์ง€ ์•„๋‹ˆํ•œ๋‹ค.\n ์„ ๊ฑฐ์— ๊ด€ํ•œ ๊ฒฝ๋น„๋Š” ๋ฒ•๋ฅ ์ด ์ •ํ•˜๋Š” ๊ฒฝ์šฐ....',
imageList: [
'https://img.huffingtonpost.com/asset/5d703563250000ad0003e5bd.jpeg?ops=scalefit_630_noupscale',
'http://image.auction.co.kr/itemimage/24/af/15/24af15b716.jpg',
],
title: res.data.data.title,
category: res.data.data.category,
content: res.data.data.content,
imageList: res.data.data.imageList,
},
},
};
Expand Down

0 comments on commit f53ac7d

Please sign in to comment.