Skip to content

Commit

Permalink
Merge branch 'feature-community' into comment/#138
Browse files Browse the repository at this point in the history
  • Loading branch information
Brokyeom authored Jul 21, 2022
2 parents 6285093 + 49849d5 commit af6fbe1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 27 deletions.
19 changes: 16 additions & 3 deletions components/common/WriteHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,32 @@ export default function WriteHeader() {
const { pathname, query } = useRouter();

const handleRegister = async () => {
const { title, content } = newPostInfo;
const { title, content, imageList, category } = newPostInfo;
if (title === '' || content === '') {
alert('내용을 입력해주세요.');
return;
}

const data = await postCommunity(newPostInfo);
const formData = new FormData();
formData.append('title', title);
formData.append('content', content);
formData.append('section', category);

if (imageList)
imageList.map((image) => {
formData.append('imageList', image.file);
});

const res = await postCommunity(formData);
const {
data: { data, status },
} = res;
setNewPostInfo({
category: '후기',
title: '',
content: '',
});
router.push(`/community/${data.id}`);
if (status === 201) router.push(`/community/${data.boardId}`);
};

const handleCancel = () => {
Expand Down
19 changes: 7 additions & 12 deletions core/api/community.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import useSWR from 'swr';
import {
PostCommunityBody,
PutCommunityBody,
PostCommentBody,
} from '../../types/community';
import { PutCommunityBody, PostCommentBody } from '../../types/community';
import { baseInstance } from '../axios';

export const useGetCollectionProduct = (key: string) => {
Expand All @@ -20,13 +16,12 @@ export const getCommunity = () => {
return baseInstance.get(`/board`);
};

export const postCommunity = async (body: PostCommunityBody) => {
try {
const { data } = await baseInstance.post('/board', body);
return data;
} catch (e) {
console.log(e);
}
export const postCommunity = (body: FormData) => {
return baseInstance.post('/board', body, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
};

export const getCommunityDetail = (id: string) => {
Expand Down
3 changes: 2 additions & 1 deletion core/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ const baseInstance = axios.create({
'Content-Type': 'application/json',
},
});

// // client side base instance (default)
// // 로컬스토리지 접근이 가능하고 token이 필요한 api 호출에서 사용
baseInstance.interceptors.request.use((config) => {
const headers = {
...config.headers,
accessToken:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MzAsImlhdCI6MTY1ODQwNzIwMiwiZXhwIjoxNjU4NDE0NDAyLCJpc3MiOiJub3JpIn0.YdQo6BWChvNyqT8ptY8RwAN7bytNW38RUQOLASlpO0o',
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MzAsImlhdCI6MTY1ODQxNDg2NiwiZXhwIjoxNjU4NDIyMDY2LCJpc3MiOiJub3JpIn0.lD7a--F1g5oEIqDvXNrRjbuLGp5S7Y1TxwRz8mXbJvY',
refreshToken:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MzAsImlhdCI6MTY1ODM4NDEwMCwiZXhwIjoxNjU5NTkzNzAwLCJpc3MiOiJub3JpIn0.AeuhSmM1ZqItojeM3O0SwrELog-Qfq91r_ii0EMgPig',
};
Expand Down
12 changes: 2 additions & 10 deletions pages/write/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,9 @@ export default function WriteForm() {
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));

setNewPostInfo({
...newPostInfo,
imageList: formData,
imageList,
});
setImages([...images, ...imageList]);
};
Expand All @@ -69,12 +64,9 @@ export default function WriteForm() {
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));
setNewPostInfo({
...newPostInfo,
imageList: formData,
imageList: imgDelData,
});
setImages(imgDelData);
};
Expand Down
18 changes: 17 additions & 1 deletion types/community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,24 @@ export interface PostCommunityBody {
category: string;
title: string;
content: string;
imageList?: FormData;
imageList?: ImgData[];
}

// 커뮤니티 수정 put body
export interface PutCommunityBody {
category?: string;
title?: string;
content?: string;
imageList?: ImgData[];
}
// 커뮤니티 변경된 state 판단
export interface IsChangeCommunity {
isChangeCategory: boolean;
isChangeTitle: boolean;
isChangeContent: boolean;
isChangeImageList: boolean;
}

// 커뮤니티 댓글
export interface PostCommentBody {
boardId?: string;
Expand Down

0 comments on commit af6fbe1

Please sign in to comment.