2021-11-20 14:51:47 +01:00
|
|
|
import { Fragment, useState, useEffect } from 'react';
|
2021-10-22 13:31:02 +02:00
|
|
|
import {
|
|
|
|
DragDropContext,
|
|
|
|
Droppable,
|
|
|
|
Draggable,
|
|
|
|
DropResult,
|
|
|
|
} from 'react-beautiful-dnd';
|
2021-06-18 12:09:59 +02:00
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
|
|
|
|
// Redux
|
2021-11-09 14:33:51 +01:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2021-11-20 14:51:47 +01:00
|
|
|
import { State } from '../../../store/reducers';
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { actionCreators } from '../../../store';
|
2021-06-18 12:09:59 +02:00
|
|
|
|
|
|
|
// Typescript
|
2021-11-09 14:33:51 +01:00
|
|
|
import { App } from '../../../interfaces';
|
2021-05-13 18:23:12 +02:00
|
|
|
|
2021-11-20 14:51:47 +01:00
|
|
|
// Other
|
2021-12-02 14:12:23 +01:00
|
|
|
import { Message, Table } from '../../UI';
|
2021-11-20 14:51:47 +01:00
|
|
|
import { TableActions } from '../../Actions/TableActions';
|
2021-11-09 14:33:51 +01:00
|
|
|
|
|
|
|
interface Props {
|
2021-11-20 14:51:47 +01:00
|
|
|
openFormForUpdating: (app: App) => void;
|
2021-05-13 18:23:12 +02:00
|
|
|
}
|
|
|
|
|
2021-11-09 14:33:51 +01:00
|
|
|
export const AppTable = (props: Props): JSX.Element => {
|
|
|
|
const {
|
|
|
|
apps: { apps },
|
|
|
|
config: { config },
|
|
|
|
} = useSelector((state: State) => state);
|
|
|
|
|
|
|
|
const dispatch = useDispatch();
|
2021-11-20 14:51:47 +01:00
|
|
|
const { pinApp, deleteApp, reorderApps, createNotification, updateApp } =
|
2021-11-09 14:33:51 +01:00
|
|
|
bindActionCreators(actionCreators, dispatch);
|
|
|
|
|
2021-06-18 12:09:59 +02:00
|
|
|
const [localApps, setLocalApps] = useState<App[]>([]);
|
|
|
|
|
|
|
|
// Copy apps array
|
|
|
|
useEffect(() => {
|
2021-11-09 14:33:51 +01:00
|
|
|
setLocalApps([...apps]);
|
|
|
|
}, [apps]);
|
2021-06-18 12:09:59 +02:00
|
|
|
|
2021-06-15 16:02:57 +02:00
|
|
|
const dragEndHanlder = (result: DropResult): void => {
|
2021-11-20 14:51:47 +01:00
|
|
|
if (config.useOrdering !== 'orderId') {
|
2021-11-09 14:33:51 +01:00
|
|
|
createNotification({
|
2021-06-18 12:09:59 +02:00
|
|
|
title: 'Error',
|
2021-10-22 13:31:02 +02:00
|
|
|
message: 'Custom order is disabled',
|
|
|
|
});
|
2021-06-18 12:09:59 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-06-15 16:02:57 +02:00
|
|
|
|
|
|
|
if (!result.destination) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-18 12:09:59 +02:00
|
|
|
const tmpApps = [...localApps];
|
2021-06-15 16:02:57 +02:00
|
|
|
const [movedApp] = tmpApps.splice(result.source.index, 1);
|
|
|
|
tmpApps.splice(result.destination.index, 0, movedApp);
|
|
|
|
|
2021-06-18 12:09:59 +02:00
|
|
|
setLocalApps(tmpApps);
|
2021-11-09 14:33:51 +01:00
|
|
|
reorderApps(tmpApps);
|
2021-10-22 13:31:02 +02:00
|
|
|
};
|
2021-06-15 16:02:57 +02:00
|
|
|
|
2021-11-20 14:51:47 +01:00
|
|
|
// Action handlers
|
|
|
|
const deleteAppHandler = (id: number, name: string) => {
|
|
|
|
const proceed = window.confirm(`Are you sure you want to delete ${name}?`);
|
|
|
|
|
|
|
|
if (proceed) {
|
|
|
|
deleteApp(id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateAppHandler = (id: number) => {
|
|
|
|
const app = apps.find((a) => a.id === id) as App;
|
|
|
|
props.openFormForUpdating(app);
|
|
|
|
};
|
|
|
|
|
|
|
|
const pinAppHandler = (id: number) => {
|
|
|
|
const app = apps.find((a) => a.id === id) as App;
|
|
|
|
pinApp(app);
|
|
|
|
};
|
|
|
|
|
|
|
|
const changeAppVisibiltyHandler = (id: number) => {
|
|
|
|
const app = apps.find((a) => a.id === id) as App;
|
|
|
|
updateApp(id, { ...app, isPublic: !app.isPublic });
|
|
|
|
};
|
|
|
|
|
2021-05-13 18:23:12 +02:00
|
|
|
return (
|
2021-06-18 12:09:59 +02:00
|
|
|
<Fragment>
|
2021-12-02 14:12:23 +01:00
|
|
|
<Message isPrimary={false}>
|
2021-11-20 14:51:47 +01:00
|
|
|
{config.useOrdering === 'orderId' ? (
|
2021-10-22 13:31:02 +02:00
|
|
|
<p>You can drag and drop single rows to reorder application</p>
|
|
|
|
) : (
|
|
|
|
<p>
|
2021-11-20 14:51:47 +01:00
|
|
|
Custom order is disabled. You can change it in the{' '}
|
|
|
|
<Link to="/settings/interface">settings</Link>
|
2021-10-22 13:31:02 +02:00
|
|
|
</p>
|
|
|
|
)}
|
2021-12-02 14:12:23 +01:00
|
|
|
</Message>
|
2021-11-20 14:51:47 +01:00
|
|
|
|
2021-06-18 12:09:59 +02:00
|
|
|
<DragDropContext onDragEnd={dragEndHanlder}>
|
2021-10-22 13:31:02 +02:00
|
|
|
<Droppable droppableId="apps">
|
2021-06-18 12:09:59 +02:00
|
|
|
{(provided) => (
|
2021-10-22 13:31:02 +02:00
|
|
|
<Table
|
2021-11-08 23:40:30 +01:00
|
|
|
headers={['Name', 'URL', 'Icon', 'Visibility', 'Actions']}
|
2021-10-22 13:31:02 +02:00
|
|
|
innerRef={provided.innerRef}
|
|
|
|
>
|
2021-06-18 12:09:59 +02:00
|
|
|
{localApps.map((app: App, index): JSX.Element => {
|
|
|
|
return (
|
2021-10-22 13:31:02 +02:00
|
|
|
<Draggable
|
|
|
|
key={app.id}
|
|
|
|
draggableId={app.id.toString()}
|
|
|
|
index={index}
|
|
|
|
>
|
2021-06-18 12:09:59 +02:00
|
|
|
{(provided, snapshot) => {
|
|
|
|
const style = {
|
2021-10-22 13:31:02 +02:00
|
|
|
border: snapshot.isDragging
|
|
|
|
? '1px solid var(--color-accent)'
|
|
|
|
: 'none',
|
2021-06-18 12:09:59 +02:00
|
|
|
borderRadius: '4px',
|
|
|
|
...provided.draggableProps.style,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<tr
|
|
|
|
{...provided.draggableProps}
|
|
|
|
{...provided.dragHandleProps}
|
|
|
|
ref={provided.innerRef}
|
|
|
|
style={style}
|
|
|
|
>
|
2021-10-22 13:31:02 +02:00
|
|
|
<td style={{ width: '200px' }}>{app.name}</td>
|
|
|
|
<td style={{ width: '200px' }}>{app.url}</td>
|
|
|
|
<td style={{ width: '200px' }}>{app.icon}</td>
|
2021-11-08 23:40:30 +01:00
|
|
|
<td style={{ width: '200px' }}>
|
|
|
|
{app.isPublic ? 'Visible' : 'Hidden'}
|
|
|
|
</td>
|
2021-11-20 14:51:47 +01:00
|
|
|
|
2021-06-18 12:09:59 +02:00
|
|
|
{!snapshot.isDragging && (
|
2021-11-20 14:51:47 +01:00
|
|
|
<TableActions
|
|
|
|
entity={app}
|
|
|
|
deleteHandler={deleteAppHandler}
|
|
|
|
updateHandler={updateAppHandler}
|
|
|
|
pinHanlder={pinAppHandler}
|
|
|
|
changeVisibilty={changeAppVisibiltyHandler}
|
|
|
|
/>
|
2021-06-18 12:09:59 +02:00
|
|
|
)}
|
|
|
|
</tr>
|
2021-10-22 13:31:02 +02:00
|
|
|
);
|
2021-06-18 12:09:59 +02:00
|
|
|
}}
|
|
|
|
</Draggable>
|
2021-10-22 13:31:02 +02:00
|
|
|
);
|
2021-06-18 12:09:59 +02:00
|
|
|
})}
|
|
|
|
</Table>
|
|
|
|
)}
|
|
|
|
</Droppable>
|
|
|
|
</DragDropContext>
|
|
|
|
</Fragment>
|
2021-10-22 13:31:02 +02:00
|
|
|
);
|
|
|
|
};
|