import React, { useState } from 'react'
import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd'
const reorder = (list: any, startIndex: number, endIndex: number) => {
const result = Array.from(list)
const [removed] = result.splice(startIndex, 1)
result.splice(endIndex, 0, removed)
return result
}
function Website({ website, index }: any) {
return (
{(provided: any) => (
{website.content}
)}
)
}
const WebsiteList = React.memo(function WebsiteList({ websites }: any) {
return websites.map((website: any, index: number) => (
))
})
export function SortableWebsites({ data, onSubmitSort }: any) {
const [state, setState] = useState({ websites: data })
function onDragEnd(result: any) {
if (!result.destination) {
return
}
if (result.destination.index === result.source.index) {
return
}
const websites = reorder(
state.websites,
result.source.index,
result.destination.index
)
setState({ websites })
onSubmitSort(websites)
}
return (
{(provided: any) => (
{provided.placeholder}
)}
)
}