2020-03-19 04:08:32 +00:00
|
|
|
import React, { useState } from 'react';
|
2020-03-19 05:28:32 +00:00
|
|
|
import { Task } from '@/api/server/schedules/getServerSchedules';
|
2020-03-18 06:33:53 +00:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faTrashAlt } from '@fortawesome/free-solid-svg-icons/faTrashAlt';
|
|
|
|
import { faCode } from '@fortawesome/free-solid-svg-icons/faCode';
|
|
|
|
import { faToggleOn } from '@fortawesome/free-solid-svg-icons/faToggleOn';
|
2020-03-19 04:08:32 +00:00
|
|
|
import ConfirmTaskDeletionModal from '@/components/server/schedules/ConfirmTaskDeletionModal';
|
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import { Actions, useStoreActions } from 'easy-peasy';
|
|
|
|
import { ApplicationStore } from '@/state';
|
|
|
|
import deleteScheduleTask from '@/api/server/schedules/deleteScheduleTask';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
2020-03-19 05:28:32 +00:00
|
|
|
import TaskDetailsModal from '@/components/server/schedules/TaskDetailsModal';
|
|
|
|
import { faPencilAlt } from '@fortawesome/free-solid-svg-icons/faPencilAlt';
|
2020-03-30 05:20:27 +00:00
|
|
|
import Can from '@/components/elements/Can';
|
2020-03-18 06:33:53 +00:00
|
|
|
|
|
|
|
interface Props {
|
2020-03-19 04:08:32 +00:00
|
|
|
schedule: number;
|
2020-03-18 06:33:53 +00:00
|
|
|
task: Task;
|
2020-03-19 05:28:32 +00:00
|
|
|
onTaskUpdated: (task: Task) => void;
|
2020-03-19 04:08:32 +00:00
|
|
|
onTaskRemoved: () => void;
|
2020-03-18 06:33:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 05:28:32 +00:00
|
|
|
export default ({ schedule, task, onTaskUpdated, onTaskRemoved }: Props) => {
|
|
|
|
const [ visible, setVisible ] = useState(false);
|
|
|
|
const [ isLoading, setIsLoading ] = useState(false);
|
|
|
|
const [ isEditing, setIsEditing ] = useState(false);
|
2020-03-19 04:08:32 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
|
|
|
const { clearFlashes, addError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
|
|
|
|
|
|
|
const onConfirmDeletion = () => {
|
|
|
|
setIsLoading(true);
|
|
|
|
clearFlashes('schedules');
|
|
|
|
deleteScheduleTask(uuid, schedule, task.id)
|
|
|
|
.then(() => onTaskRemoved())
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
setIsLoading(false);
|
|
|
|
addError({ message: httpErrorToHuman(error), key: 'schedules' });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-03-18 06:33:53 +00:00
|
|
|
return (
|
2020-03-19 05:28:32 +00:00
|
|
|
<div className={'flex items-center bg-neutral-700 border border-neutral-600 mb-2 px-6 py-4 rounded'}>
|
2020-03-19 04:08:32 +00:00
|
|
|
<SpinnerOverlay visible={isLoading} fixed={true} size={'large'}/>
|
2020-03-19 05:28:32 +00:00
|
|
|
{isEditing && <TaskDetailsModal
|
|
|
|
scheduleId={schedule}
|
|
|
|
task={task}
|
|
|
|
onDismissed={task => {
|
|
|
|
task && onTaskUpdated(task);
|
|
|
|
setIsEditing(false);
|
|
|
|
}}
|
|
|
|
/>}
|
2020-03-19 04:08:32 +00:00
|
|
|
<ConfirmTaskDeletionModal
|
|
|
|
visible={visible}
|
|
|
|
onDismissed={() => setVisible(false)}
|
|
|
|
onConfirmed={() => onConfirmDeletion()}
|
|
|
|
/>
|
2020-03-18 06:33:53 +00:00
|
|
|
<FontAwesomeIcon icon={task.action === 'command' ? faCode : faToggleOn} className={'text-lg text-white'}/>
|
|
|
|
<div className={'flex-1'}>
|
|
|
|
<p className={'ml-6 text-neutral-300 mb-2 uppercase text-xs'}>
|
|
|
|
{task.action === 'command' ? 'Send command' : 'Send power action'}
|
|
|
|
</p>
|
|
|
|
<code className={'ml-6 font-mono bg-neutral-800 rounded py-1 px-2 text-sm'}>
|
|
|
|
{task.payload}
|
|
|
|
</code>
|
|
|
|
</div>
|
|
|
|
{task.sequenceId > 1 &&
|
|
|
|
<div className={'mr-6'}>
|
|
|
|
<p className={'text-center mb-1'}>
|
|
|
|
{task.timeOffset}s
|
|
|
|
</p>
|
|
|
|
<p className={'text-neutral-300 uppercase text-2xs'}>
|
|
|
|
Delay Run By
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
}
|
2020-03-30 05:20:27 +00:00
|
|
|
<Can action={'schedule.update'}>
|
|
|
|
<button
|
|
|
|
type={'button'}
|
|
|
|
aria-label={'Edit scheduled task'}
|
|
|
|
className={'block text-sm p-2 text-neutral-500 hover:text-neutral-100 transition-colors duration-150 mr-4'}
|
|
|
|
onClick={() => setIsEditing(true)}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon icon={faPencilAlt}/>
|
|
|
|
</button>
|
|
|
|
</Can>
|
|
|
|
<Can action={'schedule.update'}>
|
|
|
|
<button
|
|
|
|
type={'button'}
|
|
|
|
aria-label={'Delete scheduled task'}
|
|
|
|
className={'block text-sm p-2 text-neutral-500 hover:text-red-600 transition-colors duration-150'}
|
|
|
|
onClick={() => setVisible(true)}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon icon={faTrashAlt}/>
|
|
|
|
</button>
|
|
|
|
</Can>
|
2020-03-18 06:33:53 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|