Skip to content

Commit 3cbf1af

Browse files
committed
Feat: 이미지 업로드 2 #65
1 parent 430b214 commit 3cbf1af

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

components/pages/main/Feed/FeedHeader/Upload.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { useEffect, useState } from 'react';
44
import usePostBoard from '@/hooks/mutations/usePostBoard';
55
import useImageUpload from '@/hooks/common/useImageUpload';
6+
import Image from 'next/image';
67
import Avatar from '../../../../ui/Avatar';
78
import Button from '../../../../ui/Button';
89
import FlexBox from '../../../../ui/FlexBox';
@@ -52,6 +53,17 @@ export default function Upload({
5253
) : null}
5354
</div>
5455
</FlexBox>
56+
{imagePreview && (
57+
<div className="relative w-full m-5 h-60 ">
58+
<Image
59+
fill
60+
priority
61+
alt="image"
62+
src={imagePreview}
63+
className="rounded-[10px] object-contain"
64+
/>
65+
</div>
66+
)}
5567
<FlexBox justify="end" className="gap-[10px] w-full">
5668
<label
5769
htmlFor="image"

hooks/mutations/usePostImageBoard.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { postImageBoard } from '@/service/board';
2+
// import Toast from '@/utils/notification';
3+
import { useMutation, useQueryClient } from '@tanstack/react-query';
4+
5+
export default function usePostImageBoard(
6+
boardId: number,
7+
postImageUrl: string[],
8+
) {
9+
const queryClient = useQueryClient();
10+
const { mutate, isLoading, isSuccess } = useMutation({
11+
mutationFn: () => postImageBoard(boardId, { files: postImageUrl }),
12+
onSuccess: () => {
13+
// Toast.success('게시글이 성공적으로 업로드 되었습니다.');
14+
queryClient.invalidateQueries(['boardList']);
15+
},
16+
onError: () => {
17+
// Toast.error(
18+
// '게시글을 업로드하지 못했습니다. 잠시후 다시 시도해주세요.🥲',
19+
// );
20+
},
21+
});
22+
return { mutate, isLoading, isSuccess };
23+
}

service/board.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { AuthError } from '@/lib/error';
2-
import { CommentList, PostBoardType, PostCommentType } from '@/types/types';
2+
import {
3+
CommentList,
4+
PostBoardType,
5+
PostCommentType,
6+
PostImageType,
7+
} from '@/types/types';
38

49
export async function postBoard(postBoardData: PostBoardType) {
510
const url = `endpoint/api/board/register`;
@@ -15,6 +20,22 @@ export async function postBoard(postBoardData: PostBoardType) {
1520
return response.json();
1621
}
1722

23+
export async function postImageBoard(
24+
boardId: number,
25+
postImageData: PostImageType,
26+
) {
27+
const url = `endpoint/api/file/upload?boardId=${boardId}`;
28+
const response = await fetch(url, {
29+
method: 'POST',
30+
credentials: 'include',
31+
headers: {
32+
'Content-Type': 'application/json',
33+
},
34+
body: JSON.stringify(postImageData),
35+
});
36+
return response.json();
37+
}
38+
1839
export async function getBoardList(pageParam: number) {
1940
const url = `/endpoint/api/board/list?pageSize=5&pageNumber=${pageParam}`;
2041
try {

types/types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ export interface PostBoardType {
9696
content: string;
9797
}
9898

99+
export interface PostImageType {
100+
files: string[];
101+
}
102+
99103
export interface PostCommentType {
100104
boardId: number;
101105
parentId: number;

0 commit comments

Comments
 (0)