-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat/#152]: 우물 아이템 이동 기능 구현 #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b70b31d
936382d
4347303
c070c12
7033dd7
f7fb096
939ce67
f258812
1166a58
1be4f62
316b84f
eb11d52
d0c3a7a
37c3904
0f17e06
076e1db
725a8f2
f594ca3
7d16c29
0aa5062
91d8f0d
466c07a
85adff7
32d10a2
f67af28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import React, { useEffect, useMemo, useState } from 'react'; | ||
| import { useObserver } from '@/hooks/gesture/useObserver'; | ||
| import { | ||
| ChangeWellItemOrderReq, | ||
| GetWellItemRes, | ||
| GetWellRes, | ||
| SearchWellItemRes, | ||
| } from '@frolog/frolog-api'; | ||
| import { usePathname } from 'next/navigation'; | ||
| import { getPath } from '@/utils/getPath'; | ||
| import { useCustomRouter } from '@/hooks/useCustomRouter'; | ||
| import { useWellItems } from '../../hooks/useWellItems'; | ||
| import WellTitle from './WellTitle'; | ||
| import { updateWellItemOrder } from '../../api/well.api'; | ||
| import WellItemList from './WellItem/WellItemList'; | ||
| import WellOrderEditHeader from './WellHeader/WellOrderEditHeader'; | ||
| import WellHeader from './WellHeader/WellHeader'; | ||
|
|
||
| interface Props { | ||
| /** 우물 정보 데이터 객체 */ | ||
| wellData: GetWellRes; | ||
| /** 로그인한 유저인지 여부 */ | ||
| isRootUser: boolean; | ||
| /** 첫 우물인지 여부 */ | ||
| isDefaultWell?: boolean; | ||
| /** 우물 순서 변경 모드 여부 */ | ||
| isMovable: boolean; | ||
| userId: string; | ||
| initialWellItemList: SearchWellItemRes; | ||
| } | ||
| function WellDetail({ | ||
| userId, | ||
| wellData, | ||
| isRootUser, | ||
| isDefaultWell, | ||
| isMovable, | ||
| initialWellItemList, | ||
| }: Props) { | ||
| const router = useCustomRouter('well'); | ||
| const pathname = usePathname(); | ||
| const { id, name, item_cnt } = wellData; | ||
|
|
||
| const { | ||
| wellItems, | ||
| fetchNextPage, | ||
| hasNextPage, | ||
| isFetchingNextPage, | ||
| isEmpty, | ||
| isFetched, | ||
| } = useWellItems(wellData.id, initialWellItemList); | ||
|
|
||
| const { setTarget } = useObserver({ hasNextPage, fetchNextPage }); | ||
| const originalItems = useMemo(() => wellItems, [wellItems]); | ||
| const [items, setItems] = useState<GetWellItemRes[]>([]); | ||
| const [orderChanges, setOrderChanges] = useState<ChangeWellItemOrderReq[]>( | ||
| [] | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| if (originalItems.length) { | ||
| setItems(originalItems); | ||
| } | ||
| }, [originalItems]); | ||
|
|
||
| const handleMoveItem = (result: any) => { | ||
| const movedItem = items.find((item) => item.id === result.draggableId); | ||
| if (!movedItem) return; | ||
|
|
||
| const newOrder = items.length - result.destination.index - 1; | ||
|
|
||
| const prevChanges = [...orderChanges]; | ||
| prevChanges.push({ | ||
| well_id: wellData.id, | ||
| id: result.draggableId, | ||
| order: newOrder, | ||
| }); | ||
|
|
||
| console.log(prevChanges); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| setOrderChanges(prevChanges); | ||
|
|
||
| setItems((prevItems) => { | ||
| const updated = [...prevItems]; | ||
| const [moved] = updated.splice(result.source.index, 1); | ||
| updated.splice(result.destination.index, 0, moved); | ||
| return updated; | ||
| }); | ||
| }; | ||
|
|
||
| const handleOrderChange = async () => { | ||
| try { | ||
| await updateWellItemOrder({ | ||
| well_id: wellData.id, | ||
| changes: orderChanges, | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이건 왜 mutation안쓰시고 바로 호출하셨어요 ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 깜빡했달까... |
||
| setOrderChanges([]); | ||
| router.replace(getPath.wellDetail(userId, wellData.id)); | ||
| } catch (error) { | ||
| console.error('순서 저장 실패:', error); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| {isMovable ? ( | ||
| <WellOrderEditHeader | ||
| onClickCancel={() => router.replace(pathname)} | ||
| onClickDone={handleOrderChange} | ||
| /> | ||
| ) : ( | ||
| <WellHeader | ||
| userId={userId} | ||
| wellId={wellData.id} | ||
| isRootUser={isRootUser} | ||
| /> | ||
| )} | ||
| <WellTitle | ||
| title={name} | ||
| wellId={id} | ||
| itemCount={item_cnt} | ||
| isRootUser={isRootUser} | ||
| isPointing={isDefaultWell && wellItems.length < 2} | ||
| isMovable={isMovable} | ||
| /> | ||
|
|
||
| <WellItemList | ||
| wellData={wellData} | ||
| items={items} | ||
| userId={userId} | ||
| wellItems={wellItems} | ||
| isRootUser={isRootUser} | ||
| isDefaultWell={isDefaultWell} | ||
| isMovable={isMovable} | ||
| isFetchingNextPage={isFetchingNextPage} | ||
| isFetched={isFetched} | ||
| isEmpty={isEmpty} | ||
| setTarget={setTarget} | ||
| handleMoveItem={handleMoveItem} | ||
| /> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| export default WellDetail; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 패키지 이제 deprecated됐을텐데 괜찮아요 ?
atlassian/react-beautiful-dnd#2672
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
당장 react 19버전으로 업그레이드 할 예정은 없으니 기타 의존성 문제 생길 때 마이그레이션 하면 된다고 생각합니다~