|
| 1 | +import { useCallback, useState, useMemo } from 'react' |
| 2 | +import { Dialog, DialogContent } from '@/shared/ui/dialog' |
| 3 | +import { CheckCircle2, Loader2 } from 'lucide-react' |
| 4 | +import { toast } from 'sonner' |
| 5 | +import { instance } from '@/shared/api/ky' |
| 6 | +import { useQueryClient } from '@tanstack/react-query' |
| 7 | + |
| 8 | +import { CreateModalHeader } from '@/features/create-post/ui/CreateModalHeader' |
| 9 | +import { PreviewPane } from '@/features/create-post/ui/PreviewPane' |
| 10 | +import { PostDetailsPane } from '@/features/create-post/ui/PostDetailsPane' |
| 11 | +import { useImageCarousel } from '@/features/create-post/model/hooks/useImageCarousel' |
| 12 | +import { useUpdatePostMutation } from '@/entities/post/model/hooks/useUpdatePostMutation' |
| 13 | +import type { PostData } from './PostDetail' |
| 14 | + |
| 15 | +type EditPostModalProps = { |
| 16 | + open: boolean |
| 17 | + onOpenChange: (open: boolean) => void |
| 18 | + initialData: PostData |
| 19 | + onSuccess?: (updatedData: PostData) => void |
| 20 | +} |
| 21 | + |
| 22 | +export function EditPostModal({ |
| 23 | + open, |
| 24 | + onOpenChange, |
| 25 | + initialData, |
| 26 | + onSuccess, |
| 27 | +}: EditPostModalProps) { |
| 28 | + const [caption, setCaption] = useState(initialData.content) |
| 29 | + const [selectedAlbumId, setSelectedAlbumId] = useState<number>( |
| 30 | + initialData.albumId ?? -1 |
| 31 | + ) |
| 32 | + const [isSyncing, setIsSyncing] = useState(false) |
| 33 | + const [isSuccessState, setIsSuccessState] = useState(false) |
| 34 | + |
| 35 | + const queryClient = useQueryClient() |
| 36 | + const updatePost = useUpdatePostMutation(initialData.id) |
| 37 | + const carousel = useImageCarousel(initialData.images.length) |
| 38 | + |
| 39 | + const isDirty = useMemo(() => { |
| 40 | + return ( |
| 41 | + caption !== initialData.content || |
| 42 | + selectedAlbumId !== (initialData.albumId ?? -1) |
| 43 | + ) |
| 44 | + }, [caption, selectedAlbumId, initialData]) |
| 45 | + |
| 46 | + const handleUpdate = useCallback(async () => { |
| 47 | + if (!isDirty) return |
| 48 | + setIsSyncing(true) |
| 49 | + |
| 50 | + try { |
| 51 | + let updatedData: PostData = { ...initialData } |
| 52 | + |
| 53 | + if (caption !== initialData.content) { |
| 54 | + const result = await updatePost.mutateAsync({ |
| 55 | + content: caption, |
| 56 | + albumId: initialData.albumId ?? null, |
| 57 | + imageUrls: initialData.images.map((img) => img.url), |
| 58 | + }) |
| 59 | + updatedData = { ...updatedData, content: result.content } |
| 60 | + } |
| 61 | + |
| 62 | + if (selectedAlbumId !== (initialData.albumId ?? -1)) { |
| 63 | + if (selectedAlbumId === -1) { |
| 64 | + await instance.delete( |
| 65 | + `api/v1/albums/${initialData.albumId}/posts/${initialData.id}` |
| 66 | + ) |
| 67 | + updatedData = { ...updatedData, albumId: null as unknown as number } |
| 68 | + } else { |
| 69 | + await instance.post( |
| 70 | + `api/v1/albums/${selectedAlbumId}/posts/${initialData.id}` |
| 71 | + ) |
| 72 | + updatedData = { ...updatedData, albumId: selectedAlbumId } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + setIsSuccessState(true) |
| 77 | + |
| 78 | + setTimeout(() => { |
| 79 | + queryClient.invalidateQueries({ queryKey: ['posts'] }) |
| 80 | + queryClient.invalidateQueries({ queryKey: ['albums'] }) |
| 81 | + |
| 82 | + onSuccess?.(updatedData) |
| 83 | + onOpenChange(false) |
| 84 | + }, 500) |
| 85 | + } catch { |
| 86 | + toast.error('정보 수정에 실패했습니다.') |
| 87 | + } finally { |
| 88 | + setIsSyncing(false) |
| 89 | + } |
| 90 | + }, [ |
| 91 | + caption, |
| 92 | + selectedAlbumId, |
| 93 | + isDirty, |
| 94 | + updatePost, |
| 95 | + onOpenChange, |
| 96 | + initialData, |
| 97 | + onSuccess, |
| 98 | + queryClient, |
| 99 | + ]) |
| 100 | + |
| 101 | + const activePreviewUrl = initialData.images[carousel.activeIndex]?.url |
| 102 | + |
| 103 | + return ( |
| 104 | + <Dialog open={open} onOpenChange={onOpenChange}> |
| 105 | + <DialogContent |
| 106 | + showCloseButton={false} |
| 107 | + overlayClassName="z-[110]" |
| 108 | + className="z-[120] flex flex-col gap-0 overflow-hidden rounded-4xl bg-white p-0 sm:w-[calc(80vh-51px+340px)] sm:max-w-[849px]" |
| 109 | + > |
| 110 | + <CreateModalHeader |
| 111 | + isUploaded={true} |
| 112 | + step="details" |
| 113 | + title="정보 수정" |
| 114 | + onBack={() => onOpenChange(false)} |
| 115 | + onShare={handleUpdate} |
| 116 | + isShareDisabled={!isDirty || isSyncing} |
| 117 | + isSharing={isSyncing} |
| 118 | + isShareSuccess={isSuccessState} |
| 119 | + /> |
| 120 | + <div className="h-px w-full bg-zinc-200" /> |
| 121 | + |
| 122 | + {isSyncing ? ( |
| 123 | + <div className="flex aspect-square w-full items-center justify-center sm:h-[calc(80vh-51px)] sm:max-h-[509px]"> |
| 124 | + <Loader2 className="size-16 animate-spin text-zinc-400" /> |
| 125 | + </div> |
| 126 | + ) : isSuccessState ? ( |
| 127 | + <div className="flex aspect-square w-full flex-col items-center justify-center gap-4 sm:h-[calc(80vh-51px)] sm:max-h-[509px]"> |
| 128 | + <CheckCircle2 className="size-24 text-green-500" /> |
| 129 | + <p className="text-lg font-medium text-zinc-700"> |
| 130 | + 정보가 수정되었습니다. |
| 131 | + </p> |
| 132 | + </div> |
| 133 | + ) : ( |
| 134 | + <div className="flex min-h-0 flex-1 flex-col sm:flex-row"> |
| 135 | + <div className="flex aspect-square w-full sm:h-[calc(80vh-51px)] sm:max-h-[509px] sm:w-[calc(80vh-51px)] sm:max-w-[509px] sm:flex-none"> |
| 136 | + <PreviewPane |
| 137 | + activePreviewUrl={activePreviewUrl} |
| 138 | + filesCount={initialData.images.length} |
| 139 | + activeIndex={carousel.activeIndex} |
| 140 | + canGoPrev={carousel.canGoPrev} |
| 141 | + canGoNext={carousel.canGoNext} |
| 142 | + dots={carousel.dots} |
| 143 | + goPrev={carousel.goPrev} |
| 144 | + goNext={carousel.goNext} |
| 145 | + /> |
| 146 | + </div> |
| 147 | + |
| 148 | + <div className="min-h-0 flex-1 sm:w-[340px] sm:shrink-0"> |
| 149 | + <PostDetailsPane |
| 150 | + profileName={initialData.nickname} |
| 151 | + profileImageUrl={initialData.profileImageUrl} |
| 152 | + caption={caption} |
| 153 | + onCaptionChange={setCaption} |
| 154 | + selectedAlbumId={selectedAlbumId} |
| 155 | + onAlbumSelect={setSelectedAlbumId} |
| 156 | + /> |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + )} |
| 160 | + </DialogContent> |
| 161 | + </Dialog> |
| 162 | + ) |
| 163 | +} |
0 commit comments