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';
|
2022-12-27 01:57:59 +01:00
|
|
|
import { Button } from 'react-basics';
|
2022-08-30 05:57:34 +02:00
|
|
|
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';
|
|
|
|
|
2023-04-21 17:00:42 +02:00
|
|
|
export 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
|
|
|
|
2022-08-30 05:57:34 +02:00
|
|
|
const ordered = useMemo(
|
|
|
|
() =>
|
|
|
|
websites
|
2022-11-01 07:42:37 +01:00
|
|
|
.map(website => ({ ...website, order: order.indexOf(website.id) }))
|
2022-08-30 05:57:34 +02:00
|
|
|
.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>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2023-04-21 17:00:42 +02:00
|
|
|
|
|
|
|
export default DashboardEdit;
|