2022-05-12 21:38:21 +02:00
|
|
|
import React, { useState } from 'react';
|
2022-05-23 11:20:08 +02:00
|
|
|
import { Grid } from '@mantine/core';
|
|
|
|
|
import {
|
|
|
|
|
closestCenter,
|
|
|
|
|
DndContext,
|
|
|
|
|
DragOverlay,
|
|
|
|
|
MouseSensor,
|
|
|
|
|
useSensor,
|
|
|
|
|
useSensors,
|
|
|
|
|
} from '@dnd-kit/core';
|
|
|
|
|
import { arrayMove, SortableContext } from '@dnd-kit/sortable';
|
2022-05-02 15:09:39 +02:00
|
|
|
import { useConfig } from '../../tools/state';
|
2022-04-25 00:11:32 +02:00
|
|
|
|
2022-05-23 11:20:08 +02:00
|
|
|
import { SortableAppShelfItem, AppShelfItem } from './AppShelfItem';
|
2022-05-17 01:43:40 +02:00
|
|
|
|
2022-05-16 15:55:22 +02:00
|
|
|
const AppShelf = (props: any) => {
|
2022-05-23 11:20:08 +02:00
|
|
|
const [activeId, setActiveId] = useState(null);
|
|
|
|
|
const { config, setConfig } = useConfig();
|
|
|
|
|
const sensors = useSensors(
|
|
|
|
|
useSensor(MouseSensor, {
|
|
|
|
|
// Require the mouse to move by 10 pixels before activating
|
|
|
|
|
activationConstraint: {
|
|
|
|
|
delay: 250,
|
|
|
|
|
tolerance: 5,
|
|
|
|
|
},
|
|
|
|
|
})
|
2022-05-04 07:12:22 +02:00
|
|
|
);
|
|
|
|
|
|
2022-05-23 11:20:08 +02:00
|
|
|
function handleDragStart(event: any) {
|
|
|
|
|
const { active } = event;
|
|
|
|
|
|
|
|
|
|
setActiveId(active.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleDragEnd(event: any) {
|
|
|
|
|
const { active, over } = event;
|
|
|
|
|
|
|
|
|
|
if (active.id !== over.id) {
|
|
|
|
|
const newConfig = { ...config };
|
|
|
|
|
const activeIndex = newConfig.services.findIndex((e) => e.id === active.id);
|
|
|
|
|
const overIndex = newConfig.services.findIndex((e) => e.id === over.id);
|
|
|
|
|
newConfig.services = arrayMove(newConfig.services, activeIndex, overIndex);
|
|
|
|
|
setConfig(newConfig);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setActiveId(null);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 07:12:22 +02:00
|
|
|
return (
|
2022-05-23 11:20:08 +02:00
|
|
|
<DndContext
|
|
|
|
|
sensors={sensors}
|
|
|
|
|
collisionDetection={closestCenter}
|
|
|
|
|
onDragStart={handleDragStart}
|
|
|
|
|
onDragEnd={handleDragEnd}
|
2022-05-04 07:12:22 +02:00
|
|
|
>
|
2022-05-23 11:20:08 +02:00
|
|
|
<SortableContext items={config.services}>
|
|
|
|
|
<Grid gutter="xl" align="center">
|
|
|
|
|
{config.services.map((service) => (
|
|
|
|
|
<Grid.Col key={service.id} span={6} xl={2} xs={4} sm={3} md={3}>
|
|
|
|
|
<SortableAppShelfItem service={service} key={service.id} id={service.id} />
|
|
|
|
|
</Grid.Col>
|
|
|
|
|
))}
|
|
|
|
|
</Grid>
|
|
|
|
|
</SortableContext>
|
|
|
|
|
<DragOverlay
|
|
|
|
|
style={{
|
|
|
|
|
// Add a shadow to the drag overlay
|
|
|
|
|
boxShadow: '0 0 10px rgba(0, 0, 0, 0.5)',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{activeId ? (
|
|
|
|
|
<AppShelfItem service={config.services.find((e) => e.id === activeId)} id={activeId} />
|
|
|
|
|
) : null}
|
|
|
|
|
</DragOverlay>
|
|
|
|
|
</DndContext>
|
2022-04-25 00:11:32 +02:00
|
|
|
);
|
2022-05-23 11:20:08 +02:00
|
|
|
};
|
2022-04-25 00:11:32 +02:00
|
|
|
|
2022-04-27 03:11:35 +02:00
|
|
|
export default AppShelf;
|