Skip to content

Commit 789f46f

Browse files
committed
feat: PUT /shared/posts/studio/{postId} API (kookmin-sw#75)
1 parent 2d85bbb commit 789f46f

File tree

5 files changed

+155
-77
lines changed

5 files changed

+155
-77
lines changed

src/app/pages/writing-post-page.tsx

Lines changed: 122 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
useCreateSharedPost,
2727
usePostMateCardInputSection,
2828
useSharedPostProps,
29+
useUpdateSharedPost,
2930
type ImageFile,
3031
} from '@/features/shared';
3132
import { useToast } from '@/features/toast';
@@ -421,7 +422,7 @@ export function WritingPostPage() {
421422
useState<boolean>(false);
422423

423424
const {
424-
mode,
425+
postId,
425426
title,
426427
content,
427428
images,
@@ -453,7 +454,9 @@ export function WritingPostPage() {
453454
handleOptionalFeatureChange,
454455
} = usePostMateCardInputSection();
455456

456-
const { mutate } = useCreateSharedPost();
457+
const { mutate: createSharedPost } = useCreateSharedPost();
458+
const { mutate: updateSharedPost } = useUpdateSharedPost();
459+
457460
const { createToast } = useToast();
458461

459462
const auth = useAuthValue();
@@ -586,7 +589,7 @@ export function WritingPostPage() {
586589

587590
const putResults = await Promise.allSettled(
588591
urls.map(async ({ url, fileName, file, uploaded }) => {
589-
if (uploaded) return { fileName };
592+
if (uploaded) return { fileName: url };
590593

591594
if (file != null) await putImage(url, file);
592595
return { fileName };
@@ -605,70 +608,128 @@ export function WritingPostPage() {
605608
});
606609
}, []);
607610

608-
console.log(selectedExtraOptions, {
609-
canPark: selectedExtraOptions.canPark ?? false,
610-
hasAirConditioner: selectedExtraOptions.hasAirConditioner ?? false,
611-
hasRefrigerator: selectedExtraOptions.hasRefrigerator ?? false,
612-
hasWasher: selectedExtraOptions.hasWasher ?? false,
613-
hasTerrace: selectedExtraOptions.hasTerrace ?? false,
614-
});
611+
console.log(uploadedImages);
615612

616-
mutate(
617-
{
618-
imageFilesData: uploadedImages,
619-
postData: { title, content },
620-
transactionData: {
621-
rentalType: dealTypeValue,
622-
expectedPayment: expectedMonthlyFee,
623-
},
624-
roomDetailData: {
625-
roomType: roomTypeValue,
626-
floorType: floorTypeValue,
627-
size: houseSize,
628-
numberOfRoom,
629-
numberOfBathRoom,
630-
hasLivingRoom: selectedOptions.livingRoom === '유',
631-
recruitmentCapacity: mateLimit,
632-
extraOption: {
633-
canPark: selectedExtraOptions.canPark ?? false,
634-
hasAirConditioner:
635-
selectedExtraOptions.hasAirConditioner ?? false,
636-
hasRefrigerator: selectedExtraOptions.hasRefrigerator ?? false,
637-
hasWasher: selectedExtraOptions.hasWasher ?? false,
638-
hasTerrace: selectedExtraOptions.hasTerrace ?? false,
613+
if (postId == null) {
614+
createSharedPost(
615+
{
616+
imageFilesData: uploadedImages,
617+
postData: { title, content },
618+
transactionData: {
619+
rentalType: dealTypeValue,
620+
expectedPayment: expectedMonthlyFee,
639621
},
622+
roomDetailData: {
623+
roomType: roomTypeValue,
624+
floorType: floorTypeValue,
625+
size: houseSize,
626+
numberOfRoom,
627+
numberOfBathRoom,
628+
hasLivingRoom: selectedOptions.livingRoom === '유',
629+
recruitmentCapacity: mateLimit,
630+
extraOption: {
631+
canPark: selectedExtraOptions.canPark ?? false,
632+
hasAirConditioner:
633+
selectedExtraOptions.hasAirConditioner ?? false,
634+
hasRefrigerator:
635+
selectedExtraOptions.hasRefrigerator ?? false,
636+
hasWasher: selectedExtraOptions.hasWasher ?? false,
637+
hasTerrace: selectedExtraOptions.hasTerrace ?? false,
638+
},
639+
},
640+
locationData: {
641+
oldAddress: address?.jibunAddress,
642+
roadAddress: address?.roadAddress,
643+
},
644+
roomMateCardData: {
645+
location: address?.roadAddress,
646+
features: derivedFeatures,
647+
},
648+
participationMemberIds:
649+
auth?.user != null ? [auth.user.memberId] : [],
640650
},
641-
locationData: {
642-
oldAddress: address?.jibunAddress,
643-
roadAddress: address?.roadAddress,
644-
},
645-
roomMateCardData: {
646-
location: address?.roadAddress,
647-
features: derivedFeatures,
651+
{
652+
onSuccess: () => {
653+
createToast({
654+
message: '게시글이 정상적으로 업로드되었습니다.',
655+
option: {
656+
duration: 3000,
657+
},
658+
});
659+
router.back();
660+
},
661+
onError: () => {
662+
createToast({
663+
message: '게시글 업로드에 실패했습니다.',
664+
option: {
665+
duration: 3000,
666+
},
667+
});
668+
},
648669
},
649-
participationMemberIds:
650-
auth?.user != null ? [auth.user.memberId] : [],
651-
},
652-
{
653-
onSuccess: () => {
654-
createToast({
655-
message: '게시글이 정상적으로 업로드되었습니다.',
656-
option: {
657-
duration: 3000,
670+
);
671+
} else if (postId != null) {
672+
updateSharedPost(
673+
{
674+
postId,
675+
postData: {
676+
imageFilesData: uploadedImages,
677+
postData: { title, content },
678+
transactionData: {
679+
rentalType: dealTypeValue,
680+
expectedPayment: expectedMonthlyFee,
658681
},
659-
});
660-
router.back();
661-
},
662-
onError: () => {
663-
createToast({
664-
message: '게시글 업로드에 실패했습니다.',
665-
option: {
666-
duration: 3000,
682+
roomDetailData: {
683+
roomType: roomTypeValue,
684+
floorType: floorTypeValue,
685+
size: houseSize,
686+
numberOfRoom,
687+
numberOfBathRoom,
688+
hasLivingRoom: selectedOptions.livingRoom === '유',
689+
recruitmentCapacity: mateLimit,
690+
extraOption: {
691+
canPark: selectedExtraOptions.canPark ?? false,
692+
hasAirConditioner:
693+
selectedExtraOptions.hasAirConditioner ?? false,
694+
hasRefrigerator:
695+
selectedExtraOptions.hasRefrigerator ?? false,
696+
hasWasher: selectedExtraOptions.hasWasher ?? false,
697+
hasTerrace: selectedExtraOptions.hasTerrace ?? false,
698+
},
699+
},
700+
locationData: {
701+
oldAddress: address?.jibunAddress,
702+
roadAddress: address?.roadAddress,
667703
},
668-
});
704+
roomMateCardData: {
705+
location: address?.roadAddress,
706+
features: derivedFeatures,
707+
},
708+
participationMemberIds:
709+
auth?.user != null ? [auth.user.memberId] : [],
710+
},
669711
},
670-
},
671-
);
712+
{
713+
onSuccess: () => {
714+
createToast({
715+
message: '게시글이 정상적으로 수정되었습니다.',
716+
option: {
717+
duration: 3000,
718+
},
719+
});
720+
router.back();
721+
},
722+
onError: () => {
723+
createToast({
724+
message: '게시글 수정에 실패했습니다.',
725+
option: {
726+
duration: 3000,
727+
},
728+
});
729+
},
730+
},
731+
);
732+
}
672733
} catch (error) {
673734
createToast({
674735
message: '게시글 업로드에 실패했습니다.',
@@ -696,7 +757,7 @@ export function WritingPostPage() {
696757
<styles.row>
697758
<styles.optionCategory>기본 정보</styles.optionCategory>
698759
<styles.createButton onClick={handleCreatePost}>
699-
{mode === 'create' ? '작성하기' : '수정하기'}
760+
{postId == null ? '작성하기' : '수정하기'}
700761
</styles.createButton>
701762
</styles.row>
702763
<styles.optionCategory>제목</styles.optionCategory>

src/features/shared/shared.api.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ import {
66
type GetSharedPostDTO,
77
type GetSharedPostsDTO,
88
} from './shared.dto';
9-
import {
10-
type CreateSharedPostProps,
11-
type GetSharedPostsProps,
12-
} from './shared.type';
9+
import { type SharedPostProps, type GetSharedPostsProps } from './shared.type';
1310

1411
import { type SuccessBaseDTO } from '@/shared/types';
1512

@@ -38,9 +35,21 @@ export const getSharedPosts = async ({
3835
return await axios.get<GetSharedPostsDTO>(getURI());
3936
};
4037

41-
export const createSharedPost = async (postData: CreateSharedPostProps) =>
38+
export const createSharedPost = async (postData: SharedPostProps) =>
4239
await axios.post<SuccessBaseDTO>(`/maru-api/shared/posts/studio`, postData);
4340

41+
export const updateSharedPost = async ({
42+
postId,
43+
postData,
44+
}: {
45+
postId: number;
46+
postData: SharedPostProps;
47+
}) =>
48+
await axios.put<SuccessBaseDTO>(
49+
`/maru-api/shared/posts/studio/${postId}`,
50+
postData,
51+
);
52+
4453
export const getSharedPost = async (postId: number) =>
4554
await axios.get<GetSharedPostDTO>(`/maru-api/shared/posts/studio/${postId}`);
4655

src/features/shared/shared.atom.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import { type NaverAddress } from '@/features/geocoding';
1010

1111
export const sharedPostPropState = atom<{
12-
mode: 'create' | 'modify';
12+
postId?: number;
1313
title: string;
1414
content: string;
1515
images: ImageFile[];
@@ -22,7 +22,7 @@ export const sharedPostPropState = atom<{
2222
}>({
2323
key: 'sharedPostPropState',
2424
default: {
25-
mode: 'create',
25+
postId: undefined,
2626
title: '',
2727
content: '',
2828
images: [],

src/features/shared/shared.hook.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ import {
1313
getSharedPosts,
1414
scrapDormitoryPost,
1515
scrapPost,
16+
updateSharedPost,
1617
} from './shared.api';
1718
import { sharedPostPropState } from './shared.atom';
1819
import { type GetSharedPostDTO } from './shared.dto';
1920
import {
20-
type SelectedExtraOptions,
21-
type CreateSharedPostProps,
2221
type GetSharedPostsProps,
22+
type SelectedExtraOptions,
2323
type SelectedOptions,
24+
type SharedPostProps,
2425
} from './shared.type';
2526
import { fromAddrToCoord } from '../geocoding';
2627

@@ -118,7 +119,7 @@ export const useSharedPostProps = () => {
118119
else if (data.roomInfo.numberOfBathRoom === 3) restRoomCount = '3개';
119120

120121
setState({
121-
mode: 'modify',
122+
postId: data.id,
122123
title: data.title,
123124
content: data.content,
124125
images: data.roomImages.map(({ fileName }) => ({
@@ -300,11 +301,18 @@ export const usePostMateCardInputSection = () => {
300301
};
301302

302303
export const useCreateSharedPost = () =>
303-
useMutation<AxiosResponse<SuccessBaseDTO>, FailureDTO, CreateSharedPostProps>(
304-
{
305-
mutationFn: createSharedPost,
306-
},
307-
);
304+
useMutation<AxiosResponse<SuccessBaseDTO>, FailureDTO, SharedPostProps>({
305+
mutationFn: createSharedPost,
306+
});
307+
308+
export const useUpdateSharedPost = () =>
309+
useMutation<
310+
AxiosResponse<SuccessBaseDTO>,
311+
FailureDTO,
312+
{ postId: number; postData: SharedPostProps }
313+
>({
314+
mutationFn: updateSharedPost,
315+
});
308316

309317
export const useSharedPosts = ({
310318
filter,

src/features/shared/shared.type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface ImageFile {
4444
extension?: string;
4545
}
4646

47-
export interface CreateSharedPostProps {
47+
export interface SharedPostProps {
4848
imageFilesData: Array<{
4949
fileName: string;
5050
isThumbNail: boolean;

0 commit comments

Comments
 (0)