umami/components/pages/dashboard/DashboardEdit.js

101 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-08-05 06:37:18 +02:00
import { useState, useMemo } from 'react';
2022-08-04 12:56:30 +02:00
import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd';
2022-08-05 06:37:18 +02:00
import classNames from 'classnames';
import { Button } from 'react-basics';
import { firstBy } from 'thenby';
2022-08-05 06:37:18 +02:00
import useDashboard, { saveDashboard } from 'store/dashboard';
2023-03-22 22:05:55 +01:00
import useMessages from 'hooks/useMessages';
2022-08-04 12:56:30 +02:00
import styles from './DashboardEdit.module.css';
const dragId = 'dashboard-website-ordering';
2022-08-05 06:37:18 +02:00
export default function DashboardEdit({ websites }) {
2022-08-04 12:56:30 +02:00
const settings = useDashboard();
const { websiteOrder } = settings;
2023-03-22 22:05:55 +01:00
const { formatMessage, labels } = useMessages();
2022-08-05 06:37:18 +02:00
const [order, setOrder] = useState(websiteOrder || []);
2022-08-04 12:56:30 +02:00
const ordered = useMemo(
() =>
websites
2022-11-01 07:42:37 +01:00
.map(website => ({ ...website, order: order.indexOf(website.id) }))
.sort(firstBy('order')),
[websites, order],
);
2022-08-04 12:56:30 +02:00
function handleWebsiteDrag({ destination, source }) {
if (!destination || destination.index === source.index) return;
const orderedWebsites = [...ordered];
const [removed] = orderedWebsites.splice(source.index, 1);
orderedWebsites.splice(destination.index, 0, removed);
2022-11-01 07:42:37 +01:00
setOrder(orderedWebsites.map(website => website?.id || 0));
2022-08-04 12:56:30 +02:00
}
function handleSave() {
saveDashboard({
editing: false,
websiteOrder: order,
});
}
function handleCancel() {
2022-08-04 23:30:09 +02:00
saveDashboard({ editing: false, websiteOrder });
2022-08-04 12:56:30 +02:00
}
function handleReset() {
2022-08-05 06:37:18 +02:00
setOrder([]);
2022-08-04 12:56:30 +02:00
}
return (
<>
<div className={styles.buttons}>
2022-08-04 23:30:09 +02:00
<Button onClick={handleSave} variant="action" size="small">
2023-03-22 22:05:55 +01:00
{formatMessage(labels.save)}
2022-08-04 12:56:30 +02:00
</Button>
<Button onClick={handleCancel} size="small">
2023-03-22 22:05:55 +01:00
{formatMessage(labels.cancel)}
2022-08-04 12:56:30 +02:00
</Button>
<Button onClick={handleReset} size="small">
2023-03-22 22:05:55 +01:00
{formatMessage(labels.reset)}
2022-08-04 12:56:30 +02:00
</Button>
</div>
<div className={styles.dragActive}>
<DragDropContext onDragEnd={handleWebsiteDrag}>
<Droppable droppableId={dragId}>
{(provided, snapshot) => (
<div
{...provided.droppableProps}
ref={provided.innerRef}
style={{ marginBottom: snapshot.isDraggingOver ? 260 : null }}
>
2022-11-01 07:42:37 +01:00
{ordered.map(({ id, name, domain }, index) => (
<Draggable key={id} draggableId={`${dragId}-${id}`} index={index}>
2022-08-04 23:30:09 +02:00
{(provided, snapshot) => (
2022-08-04 12:56:30 +02:00
<div
ref={provided.innerRef}
2022-08-04 23:30:09 +02:00
className={classNames(styles.item, {
[styles.active]: snapshot.isDragging,
})}
2022-08-04 12:56:30 +02:00
{...provided.draggableProps}
{...provided.dragHandleProps}
>
2022-08-04 23:30:09 +02:00
<div className={styles.text}>
<h1>{name}</h1>
<h2>{domain}</h2>
</div>
2022-08-04 12:56:30 +02:00
</div>
)}
</Draggable>
))}
2022-08-05 06:37:18 +02:00
{provided.placeholder}
2022-08-04 12:56:30 +02:00
</div>
)}
</Droppable>
</DragDropContext>
</div>
</>
);
}