mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-27 21:00:12 +01:00
aceb904398
* Fixed issue with realtime page rendering. * fix auth, add pg extension (#1596) * Fixed change password issue. API refactoring. Closes #1592. * Fixed account lookup. * Fixed issue with accessing user dashboards. Closes #1590 * fix sort on dashboard (#1600) Co-authored-by: Brian Cao <brian@umami.is>
111 lines
3.5 KiB
JavaScript
111 lines
3.5 KiB
JavaScript
import { useState, useMemo } from 'react';
|
|
import { defineMessages, useIntl } from 'react-intl';
|
|
import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd';
|
|
import classNames from 'classnames';
|
|
import Button from 'components/common/Button';
|
|
import { firstBy } from 'thenby';
|
|
import useDashboard, { saveDashboard } from 'store/dashboard';
|
|
import styles from './DashboardEdit.module.css';
|
|
|
|
const messages = defineMessages({
|
|
save: { id: 'label.save', defaultMessage: 'Save' },
|
|
reset: { id: 'label.reset', defaultMessage: 'Reset' },
|
|
cancel: { id: 'label.cancel', defaultMessage: 'Cancel' },
|
|
});
|
|
|
|
const dragId = 'dashboard-website-ordering';
|
|
|
|
export default function DashboardEdit({ websites }) {
|
|
const settings = useDashboard();
|
|
const { websiteOrder } = settings;
|
|
const { formatMessage } = useIntl();
|
|
const [order, setOrder] = useState(websiteOrder || []);
|
|
|
|
const ordered = useMemo(
|
|
() =>
|
|
websites
|
|
.map(website => ({ ...website, order: order.indexOf(website.websiteUuid) }))
|
|
.sort(firstBy('order')),
|
|
[websites, order],
|
|
);
|
|
|
|
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);
|
|
|
|
setOrder(orderedWebsites.map(website => website?.websiteUuid || 0));
|
|
}
|
|
|
|
function handleSave() {
|
|
saveDashboard({
|
|
editing: false,
|
|
websiteOrder: order,
|
|
});
|
|
}
|
|
|
|
function handleCancel() {
|
|
saveDashboard({ editing: false, websiteOrder });
|
|
}
|
|
|
|
function handleReset() {
|
|
setOrder([]);
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<div className={styles.buttons}>
|
|
<Button onClick={handleSave} variant="action" size="small">
|
|
{formatMessage(messages.save)}
|
|
</Button>
|
|
<Button onClick={handleCancel} size="small">
|
|
{formatMessage(messages.cancel)}
|
|
</Button>
|
|
<Button onClick={handleReset} size="small">
|
|
{formatMessage(messages.reset)}
|
|
</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 }}
|
|
>
|
|
{ordered.map(({ websiteUuid, name, domain }, index) => (
|
|
<Draggable
|
|
key={websiteUuid}
|
|
draggableId={`${dragId}-${websiteUuid}`}
|
|
index={index}
|
|
>
|
|
{(provided, snapshot) => (
|
|
<div
|
|
ref={provided.innerRef}
|
|
className={classNames(styles.item, {
|
|
[styles.active]: snapshot.isDragging,
|
|
})}
|
|
{...provided.draggableProps}
|
|
{...provided.dragHandleProps}
|
|
>
|
|
<div className={styles.text}>
|
|
<h1>{name}</h1>
|
|
<h2>{domain}</h2>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</Draggable>
|
|
))}
|
|
{provided.placeholder}
|
|
</div>
|
|
)}
|
|
</Droppable>
|
|
</DragDropContext>
|
|
</div>
|
|
</>
|
|
);
|
|
}
|