import React from 'react'; import useSWR from 'swr'; import LoadingSpinner from '../Common/LoadingSpinner'; import { FormattedRelativeTime, defineMessages, useIntl } from 'react-intl'; import Button from '../Common/Button'; import Table from '../Common/Table'; import Spinner from '../../assets/spinner.svg'; import axios from 'axios'; import { useToasts } from 'react-toast-notifications'; import Badge from '../Common/Badge'; const messages = defineMessages({ jobname: 'Job Name', jobtype: 'Type', nextexecution: 'Next Execution', runnow: 'Run Now', canceljob: 'Cancel Job', jobstarted: '{jobname} started.', jobcancelled: '{jobname} cancelled.', }); interface Job { id: string; name: string; type: 'process' | 'command'; nextExecutionTime: string; running: boolean; } const SettingsJobs: React.FC = () => { const intl = useIntl(); const { addToast } = useToasts(); const { data, error, revalidate } = useSWR('/api/v1/settings/jobs', { refreshInterval: 5000, }); if (!data && !error) { return ; } const runJob = async (job: Job) => { await axios.get(`/api/v1/settings/jobs/${job.id}/run`); addToast( intl.formatMessage(messages.jobstarted, { jobname: job.name, }), { appearance: 'success', autoDismiss: true, } ); revalidate(); }; const cancelJob = async (job: Job) => { await axios.get(`/api/v1/settings/jobs/${job.id}/cancel`); addToast(intl.formatMessage(messages.jobcancelled, { jobname: job.name }), { appearance: 'error', autoDismiss: true, }); revalidate(); }; return ( {intl.formatMessage(messages.jobname)}{intl.formatMessage(messages.jobtype)}{intl.formatMessage(messages.nextexecution)} {data?.map((job) => (
{job.running && } {job.name}
{job.type}
{job.running ? ( ) : ( )} ))}
); }; export default SettingsJobs;