-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(board): change order of tiles (#1584)
* feat(board): change order of tiles * style(tile): change arrow icons * style(edit): change placement delete tile and arrows to change order of tiles * style(edit): buttons and width of baseExpand * style(board): stroke on arrow buttons
- Loading branch information
Showing
5 changed files
with
187 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
next-tavla/app/(admin)/edit/[id]/components/TileList/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use client' | ||
|
||
import { TBoard, TBoardID } from 'types/settings' | ||
import { TileCard } from 'app/(admin)/edit/[id]/components/TileCard/index' | ||
import { Dispatch, SetStateAction, useEffect, useState } from 'react' | ||
import { TTile } from 'types/tile' | ||
import { saveTiles } from '../../actions' | ||
import { debounce } from 'lodash' | ||
|
||
function TileList({ | ||
board, | ||
setDemoBoard, | ||
bid, | ||
}: { | ||
board: TBoard | ||
bid?: TBoardID | ||
setDemoBoard?: Dispatch<SetStateAction<TBoard>> | ||
}) { | ||
const [array, setArray] = useState<TTile[]>(board.tiles) | ||
|
||
useEffect(() => { | ||
setArray(board.tiles) | ||
}, [board.tiles]) | ||
|
||
const moveItem = (index: number, direction: string) => { | ||
const newIndex: number = direction === 'up' ? index - 1 : index + 1 | ||
if (newIndex < 0 || newIndex >= board.tiles.length) { | ||
return | ||
} | ||
|
||
const newArray: TTile[] = [...board.tiles] | ||
|
||
const oldElement = newArray[newIndex] | ||
|
||
newArray[newIndex] = newArray[index] as TTile | ||
newArray[index] = oldElement as TTile | ||
|
||
setArray(newArray) | ||
if (bid === 'demo' && setDemoBoard) { | ||
const newBoard: TBoard = { ...board, tiles: newArray } | ||
setDemoBoard(newBoard ?? board) | ||
} else { | ||
saveTiles(board.id ?? '', newArray) | ||
} | ||
} | ||
const debouncedSave = debounce(moveItem, 150) | ||
return ( | ||
<div className="flex flex-col gap-4"> | ||
{array.map((tile, index) => ( | ||
<TileCard | ||
key={tile.uuid} | ||
bid={bid ?? board.id ?? ''} | ||
demoBoard={bid ? board : undefined} | ||
tile={tile} | ||
address={board.meta.location} | ||
moveItem={debouncedSave} | ||
index={index} | ||
totalTiles={board.tiles.length} | ||
setDemoBoard={setDemoBoard} | ||
/> | ||
))} | ||
</div> | ||
) | ||
} | ||
export { TileList } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters