|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useState } from 'react'; |
| 4 | + |
| 5 | +import { useMutation, useQuery } from '@tanstack/react-query'; |
| 6 | +import { useRouter } from 'next/navigation'; |
| 7 | + |
| 8 | +import Button from '@/components/Button'; |
| 9 | +import CheckListDetail from '@/components/CheckListDetail'; |
| 10 | +import ImageUploader from '@/components/ImageUploader'; |
| 11 | +import LoadingOverlay from '@/components/LoadingOverlay'; |
| 12 | +import Memo from '@/components/Memo'; |
| 13 | +import { useItemStore } from '@/store/itemStore'; |
| 14 | +import { UploadImageResponse } from '@/types/TodoTypes'; |
| 15 | + |
| 16 | +import { deleteItem, getItem, updateItem, uploadImage } from '../api/todo'; |
| 17 | + |
| 18 | +interface ItemDetailContentProps { |
| 19 | + itemId: number; |
| 20 | +} |
| 21 | + |
| 22 | +const ItemDetailContent = ({ itemId }: ItemDetailContentProps) => { |
| 23 | + const router = useRouter(); |
| 24 | + const { data, isLoading, isFetching } = useQuery({ |
| 25 | + queryKey: ['itemDetail', itemId], |
| 26 | + queryFn: () => getItem(itemId), |
| 27 | + }); |
| 28 | + |
| 29 | + const updateItemMutation = useMutation({ |
| 30 | + mutationFn: () => updateItem(itemId, detailData), |
| 31 | + retry: 1, |
| 32 | + retryDelay: 300, |
| 33 | + onSuccess: () => { |
| 34 | + setDetailData({ name: '', memo: '', imageUrl: '', isCompleted: false }); |
| 35 | + router.push('/'); |
| 36 | + }, |
| 37 | + onError: (error) => { |
| 38 | + console.log(error); |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + const uploadImageMutation = useMutation<UploadImageResponse, Error, File>({ |
| 43 | + mutationFn: (file: File) => uploadImage(file), |
| 44 | + retry: 1, |
| 45 | + retryDelay: 300, |
| 46 | + onSuccess: (response) => { |
| 47 | + const { url } = response; |
| 48 | + setDetailData({ imageUrl: url }); |
| 49 | + updateItemMutation.mutate(); |
| 50 | + }, |
| 51 | + onError: (error) => { |
| 52 | + console.log(error); |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + const deleteItemMutation = useMutation({ |
| 57 | + mutationFn: () => deleteItem(itemId), |
| 58 | + retry: 1, |
| 59 | + retryDelay: 300, |
| 60 | + onSuccess: () => { |
| 61 | + setDetailData({ name: '', memo: '', imageUrl: '', isCompleted: false }); |
| 62 | + router.push('/'); |
| 63 | + }, |
| 64 | + onError: (error) => { |
| 65 | + console.log(error); |
| 66 | + }, |
| 67 | + }); |
| 68 | + |
| 69 | + const { detailData, setDetailData } = useItemStore(); |
| 70 | + |
| 71 | + const [file, setFile] = useState<File | null>(null); |
| 72 | + |
| 73 | + const disableEditButton = |
| 74 | + !detailData.name || |
| 75 | + (detailData.isCompleted === data?.isCompleted && |
| 76 | + detailData.name === data?.name && |
| 77 | + detailData.memo === data?.memo && |
| 78 | + !file); |
| 79 | + |
| 80 | + const onUploadFile = (fileData: File) => { |
| 81 | + setFile(fileData); |
| 82 | + }; |
| 83 | + |
| 84 | + const onClickEditDetail = () => { |
| 85 | + if (file) { |
| 86 | + uploadImageMutation.mutate(file); |
| 87 | + } else { |
| 88 | + updateItemMutation.mutate(); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + useEffect(() => { |
| 93 | + if (data) { |
| 94 | + const { name, memo, imageUrl, isCompleted } = data; |
| 95 | + setDetailData({ name, memo, imageUrl, isCompleted }); |
| 96 | + } |
| 97 | + }, [data, setDetailData]); |
| 98 | + |
| 99 | + if (isLoading || isFetching) return <LoadingOverlay />; |
| 100 | + |
| 101 | + return ( |
| 102 | + <div className='max-w-[75rem] w-full flex flex-col gap-6.5 bg-white px-12 sm:px-24.5 py-6'> |
| 103 | + <CheckListDetail /> |
| 104 | + <div className='flex flex-col sm:flex-row items-start justify-center gap-6'> |
| 105 | + <ImageUploader imageUrl={detailData.imageUrl} onUpload={onUploadFile} /> |
| 106 | + <Memo /> |
| 107 | + </div> |
| 108 | + <div className='flex w-full justify-center sm:justify-end gap-4 min-w-0'> |
| 109 | + <Button |
| 110 | + mode='edit' |
| 111 | + size='full' |
| 112 | + className='max-w-[168px]' |
| 113 | + disabled={disableEditButton} |
| 114 | + onClick={onClickEditDetail} |
| 115 | + > |
| 116 | + 수정 완료 |
| 117 | + </Button> |
| 118 | + <Button |
| 119 | + mode='delete' |
| 120 | + size='full' |
| 121 | + className='max-w-[168px]' |
| 122 | + onClick={() => deleteItemMutation.mutate()} |
| 123 | + > |
| 124 | + 삭제하기 |
| 125 | + </Button> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + ); |
| 129 | +}; |
| 130 | + |
| 131 | +export default ItemDetailContent; |
0 commit comments