2020-10-15 03:38:59 +00:00
|
|
|
import React, { useCallback, useState } from 'react';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
2022-06-20 19:28:27 +00:00
|
|
|
import { Button } from '@/components/elements/button/index';
|
2020-10-15 03:38:59 +00:00
|
|
|
import triggerScheduleExecution from '@/api/server/schedules/triggerScheduleExecution';
|
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
|
|
|
import { Schedule } from '@/api/server/schedules/getServerSchedules';
|
|
|
|
|
|
|
|
const RunScheduleButton = ({ schedule }: { schedule: Schedule }) => {
|
2022-06-26 19:13:52 +00:00
|
|
|
const [loading, setLoading] = useState(false);
|
2020-10-15 03:38:59 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
const id = ServerContext.useStoreState((state) => state.server.data!.id);
|
|
|
|
const appendSchedule = ServerContext.useStoreActions((actions) => actions.schedules.appendSchedule);
|
2020-10-15 03:38:59 +00:00
|
|
|
|
|
|
|
const onTriggerExecute = useCallback(() => {
|
|
|
|
clearFlashes('schedule');
|
|
|
|
setLoading(true);
|
|
|
|
triggerScheduleExecution(id, schedule.id)
|
|
|
|
.then(() => {
|
|
|
|
setLoading(false);
|
|
|
|
appendSchedule({ ...schedule, isProcessing: true });
|
|
|
|
})
|
2022-06-26 19:13:52 +00:00
|
|
|
.catch((error) => {
|
2020-10-15 03:38:59 +00:00
|
|
|
console.error(error);
|
|
|
|
clearAndAddHttpError({ error, key: 'schedules' });
|
|
|
|
})
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-06-26 19:13:52 +00:00
|
|
|
<SpinnerOverlay visible={loading} size={'large'} />
|
2020-10-15 03:38:59 +00:00
|
|
|
<Button
|
2022-06-20 19:28:27 +00:00
|
|
|
variant={Button.Variants.Secondary}
|
|
|
|
className={'flex-1 sm:flex-none'}
|
2020-10-15 03:38:59 +00:00
|
|
|
disabled={schedule.isProcessing}
|
|
|
|
onClick={onTriggerExecute}
|
|
|
|
>
|
|
|
|
Run Now
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default RunScheduleButton;
|