From eb2f4b980f1f5fe7210747eec619acfd2cc89c96 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Fri, 5 Jul 2024 04:59:32 +0200 Subject: [PATCH] Implement drag & drop w/o persistence --- pages/settings/wallets/index.js | 51 ++++++++++++++++++++++++++++++--- styles/wallet.module.css | 9 ++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/pages/settings/wallets/index.js b/pages/settings/wallets/index.js index 32725dfc..50265172 100644 --- a/pages/settings/wallets/index.js +++ b/pages/settings/wallets/index.js @@ -5,10 +5,44 @@ import { WalletCard } from '@/components/wallet-card' import { WALLETS as WALLETS_QUERY } from '@/fragments/wallet' import Link from 'next/link' import { WALLET_DEFS } from '@/components/wallet' +import { useState } from 'react' export const getServerSideProps = getGetServerSideProps({ query: WALLETS_QUERY, authRequired: true }) export default function Wallet ({ ssrData }) { + const [wallets, setWallets] = useState(WALLET_DEFS) + const [sourceIndex, setSourceIndex] = useState() + const [targetIndex, setTargetIndex] = useState() + + const onDragStart = (i) => (e) => { + // e.dataTransfer.dropEffect = 'move' + // We can only use the DataTransfer API inside the drop event + // see https://html.spec.whatwg.org/multipage/dnd.html#security-risks-in-the-drag-and-drop-model + // e.dataTransfer.setData('text/plain', name) + // That's why we use React state instead + setSourceIndex(i) + } + + const onDragEnter = (i) => (e) => { + setTargetIndex(i) + } + + const onDragEnd = (e) => { + setSourceIndex(null) + setTargetIndex(null) + if (sourceIndex === targetIndex) return + setWallets(wallets => { + const copy = [...wallets] + + const [source] = copy.splice(sourceIndex, 1) + const newTargetIndex = sourceIndex < targetIndex ? targetIndex - 1 : targetIndex + const append = sourceIndex < targetIndex + + copy.splice(newTargetIndex + (append ? 1 : 0), 0, source) + return copy + }) + } + return (
@@ -19,10 +53,19 @@ export default function Wallet ({ ssrData }) { wallet logs
-
- {WALLET_DEFS.map((def, i) => - - )} +
+ {wallets + .map((def, i) => +
+ +
+ )}
diff --git a/styles/wallet.module.css b/styles/wallet.module.css index eb1605ea..e29e5ae3 100644 --- a/styles/wallet.module.css +++ b/styles/wallet.module.css @@ -7,9 +7,18 @@ margin-top: 3rem; } +.drag { + opacity: 33%; +} + +.drop { + box-shadow: 0 0 10px var(--bs-info); +} + .card { width: 160px; height: 180px; + cursor: move; } .badge {