2020-10-15 03:38:59 +00:00
|
|
|
import React, { useCallback, useState } from 'react';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
2022-02-27 17:35:27 +00:00
|
|
|
import { triggerScheduleExecution } from '@/api/server/schedules';
|
2020-10-15 03:38:59 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
2022-02-27 17:35:27 +00:00
|
|
|
import { Schedule } from '@definitions/user';
|
2020-10-15 03:38:59 +00:00
|
|
|
|
|
|
|
const RunScheduleButton = ({ schedule }: { schedule: Schedule }) => {
|
|
|
|
const [ loading, setLoading ] = useState(false);
|
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
|
|
|
|
|
|
|
const id = ServerContext.useStoreState(state => state.server.data!.id);
|
|
|
|
const appendSchedule = ServerContext.useStoreActions(actions => actions.schedules.appendSchedule);
|
|
|
|
|
|
|
|
const onTriggerExecute = useCallback(() => {
|
|
|
|
clearFlashes('schedule');
|
|
|
|
setLoading(true);
|
|
|
|
triggerScheduleExecution(id, schedule.id)
|
|
|
|
.then(() => {
|
|
|
|
setLoading(false);
|
|
|
|
appendSchedule({ ...schedule, isProcessing: true });
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
2021-11-17 20:52:06 +00:00
|
|
|
clearAndAddHttpError({ key: 'schedules', error });
|
2020-10-15 03:38:59 +00:00
|
|
|
})
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<SpinnerOverlay visible={loading} size={'large'}/>
|
|
|
|
<Button
|
|
|
|
isSecondary
|
|
|
|
color={'grey'}
|
|
|
|
css={tw`flex-1 sm:flex-none border-transparent`}
|
|
|
|
disabled={schedule.isProcessing}
|
|
|
|
onClick={onTriggerExecute}
|
|
|
|
>
|
|
|
|
Run Now
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default RunScheduleButton;
|