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.website_id) })) .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_id }) => website_id)); } function handleSave() { saveDashboard({ editing: false, websiteOrder: order, }); } function handleCancel() { saveDashboard({ editing: false, websiteOrder }); } function handleReset() { setOrder([]); } return ( <>
{(provided, snapshot) => (
{ordered.map(({ website_id, name, domain }, index) => ( {(provided, snapshot) => (

{name}

{domain}

)}
))} {provided.placeholder}
)}
); }