Make account cards draggable and persist order in local storage
This commit is contained in:
@@ -11,6 +11,7 @@ import { AccessToken } from "@/types";
|
||||
import { CharacterContext, SessionContext } from "../context/Context";
|
||||
import ResponsiveAppBar from "./AppBar/AppBar";
|
||||
import { Summary } from "./Summary/Summary";
|
||||
import { DragDropContext, Droppable, Draggable, DropResult } from "@hello-pangea/dnd";
|
||||
|
||||
interface Grouped {
|
||||
[key: string]: AccessToken[];
|
||||
@@ -38,7 +39,41 @@ declare module "@mui/material/styles" {
|
||||
}
|
||||
|
||||
export const MainGrid = () => {
|
||||
const { characters } = useContext(CharacterContext);
|
||||
const { characters, updateCharacter } = useContext(CharacterContext);
|
||||
const [accountOrder, setAccountOrder] = useState<string[]>([]);
|
||||
|
||||
// Initialize account order when characters change
|
||||
useEffect(() => {
|
||||
const currentAccounts = Object.keys(
|
||||
characters.reduce<Grouped>((group, character) => {
|
||||
const { account } = character;
|
||||
group[account ?? ""] = group[account ?? ""] ?? [];
|
||||
group[account ?? ""].push(character);
|
||||
return group;
|
||||
}, {})
|
||||
);
|
||||
|
||||
const savedOrder = localStorage.getItem('accountOrder');
|
||||
if (savedOrder) {
|
||||
try {
|
||||
const parsedOrder = JSON.parse(savedOrder);
|
||||
const validOrder = parsedOrder.filter((account: string) => currentAccounts.includes(account));
|
||||
const newAccounts = currentAccounts.filter(account => !validOrder.includes(account));
|
||||
setAccountOrder([...validOrder, ...newAccounts]);
|
||||
} catch (e) {
|
||||
setAccountOrder(currentAccounts);
|
||||
}
|
||||
} else {
|
||||
setAccountOrder(currentAccounts);
|
||||
}
|
||||
}, [characters]);
|
||||
|
||||
useEffect(() => {
|
||||
if (accountOrder.length > 0) {
|
||||
localStorage.setItem('accountOrder', JSON.stringify(accountOrder));
|
||||
}
|
||||
}, [accountOrder]);
|
||||
|
||||
const groupByAccount = characters.reduce<Grouped>((group, character) => {
|
||||
const { account } = character;
|
||||
group[account ?? ""] = group[account ?? ""] ?? [];
|
||||
@@ -79,24 +114,66 @@ export const MainGrid = () => {
|
||||
);
|
||||
}, [compactMode]);
|
||||
|
||||
const handleDragEnd = (result: DropResult) => {
|
||||
if (!result.destination) return;
|
||||
|
||||
const items = Array.from(accountOrder);
|
||||
const [reorderedItem] = items.splice(result.source.index, 1);
|
||||
items.splice(result.destination.index, 0, reorderedItem);
|
||||
|
||||
setAccountOrder(items);
|
||||
};
|
||||
|
||||
const DragDropContextComponent = DragDropContext as any;
|
||||
const DroppableComponent = Droppable as any;
|
||||
const DraggableComponent = Draggable as any;
|
||||
|
||||
return (
|
||||
<ThemeProvider theme={darkTheme}>
|
||||
<CssBaseline />
|
||||
<Box sx={{ flexGrow: 1 }}>
|
||||
<ResponsiveAppBar />
|
||||
{compactMode ? <></> : <Summary characters={characters} />}
|
||||
<Grid container spacing={1}>
|
||||
{Object.values(groupByAccount).map((g, id) => (
|
||||
<Grid
|
||||
item
|
||||
xs={12}
|
||||
sm={compactMode ? 6 : 12}
|
||||
key={`account-${id}-${g[0].account}`}
|
||||
>
|
||||
<AccountCard characters={g} />
|
||||
</Grid>
|
||||
))}
|
||||
</Grid>
|
||||
<DragDropContextComponent onDragEnd={handleDragEnd}>
|
||||
<DroppableComponent droppableId="accounts">
|
||||
{(provided: any) => (
|
||||
<Grid
|
||||
container
|
||||
spacing={1}
|
||||
sx={{ padding: 1 }}
|
||||
{...provided.droppableProps}
|
||||
ref={provided.innerRef}
|
||||
>
|
||||
{accountOrder.map((account, index) => (
|
||||
<DraggableComponent
|
||||
key={account}
|
||||
draggableId={account}
|
||||
index={index}
|
||||
>
|
||||
{(provided: any) => (
|
||||
<Grid
|
||||
item
|
||||
xs={12}
|
||||
sm={compactMode ? 6 : 12}
|
||||
ref={provided.innerRef}
|
||||
{...provided.draggableProps}
|
||||
{...provided.dragHandleProps}
|
||||
sx={{
|
||||
'& > *': {
|
||||
width: '100%',
|
||||
},
|
||||
}}
|
||||
>
|
||||
<AccountCard characters={groupByAccount[account]} />
|
||||
</Grid>
|
||||
)}
|
||||
</DraggableComponent>
|
||||
))}
|
||||
{provided.placeholder}
|
||||
</Grid>
|
||||
)}
|
||||
</DroppableComponent>
|
||||
</DragDropContextComponent>
|
||||
</Box>
|
||||
</ThemeProvider>
|
||||
);
|
||||
|
Reference in New Issue
Block a user