2021-05-01 18:24:18 +00:00
import React , { useContext , useEffect } from 'react' ;
2020-04-10 17:46:00 +00:00
import { Schedule , Task } from '@/api/server/schedules/getServerSchedules' ;
2021-05-01 18:24:18 +00:00
import { Field as FormikField , Form , Formik , FormikHelpers , useField } from 'formik' ;
2020-03-19 05:28:32 +00:00
import { ServerContext } from '@/state/server' ;
import createOrUpdateScheduleTask from '@/api/server/schedules/createOrUpdateScheduleTask' ;
import { httpErrorToHuman } from '@/api/http' ;
import Field from '@/components/elements/Field' ;
import FlashMessageRender from '@/components/FlashMessageRender' ;
2021-05-01 18:24:18 +00:00
import { boolean , number , object , string } from 'yup' ;
2020-04-10 17:46:00 +00:00
import useFlash from '@/plugins/useFlash' ;
2020-04-19 19:15:10 +00:00
import FormikFieldWrapper from '@/components/elements/FormikFieldWrapper' ;
2020-07-05 00:00:19 +00:00
import tw from 'twin.macro' ;
import Label from '@/components/elements/Label' ;
import { Textarea } from '@/components/elements/Input' ;
2022-06-20 19:28:27 +00:00
import { Button } from '@/components/elements/button/index' ;
2020-07-05 00:00:19 +00:00
import Select from '@/components/elements/Select' ;
2021-05-01 18:24:18 +00:00
import ModalContext from '@/context/ModalContext' ;
import asModal from '@/hoc/asModal' ;
import FormikSwitch from '@/components/elements/FormikSwitch' ;
2020-03-19 05:28:32 +00:00
interface Props {
2020-04-10 17:46:00 +00:00
schedule : Schedule ;
2020-03-19 05:28:32 +00:00
// If a task is provided we can assume we're editing it. If not provided,
// we are creating a new one.
task? : Task ;
}
interface Values {
action : string ;
payload : string ;
timeOffset : string ;
2021-05-01 18:24:18 +00:00
continueOnFailure : boolean ;
2020-03-19 05:28:32 +00:00
}
2021-05-01 18:24:18 +00:00
const schema = object ( ) . shape ( {
2022-06-26 19:13:52 +00:00
action : string ( ) . required ( ) . oneOf ( [ 'command' , 'power' , 'backup' ] ) ,
2021-05-01 18:24:18 +00:00
payload : string ( ) . when ( 'action' , {
2022-06-26 19:13:52 +00:00
is : ( v ) = > v !== 'backup' ,
2021-05-01 18:24:18 +00:00
then : string ( ) . required ( 'A task payload must be provided.' ) ,
otherwise : string ( ) ,
} ) ,
continueOnFailure : boolean ( ) ,
2022-06-26 19:13:52 +00:00
timeOffset : number ( )
. typeError ( 'The time offset must be a valid number between 0 and 900.' )
2021-05-01 18:24:18 +00:00
. required ( 'A time offset value must be provided.' )
. min ( 0 , 'The time offset must be at least 0 seconds.' )
. max ( 900 , 'The time offset must be less than 900 seconds.' ) ,
} ) ;
const ActionListener = ( ) = > {
2022-06-26 19:13:52 +00:00
const [ { value } , { initialValue : initialAction } ] = useField < string > ( 'action' ) ;
const [ , { initialValue : initialPayload } , { setValue , setTouched } ] = useField < string > ( 'payload' ) ;
2020-03-22 21:11:26 +00:00
useEffect ( ( ) = > {
2021-05-01 18:24:18 +00:00
if ( value !== initialAction ) {
setValue ( value === 'power' ? 'start' : '' ) ;
setTouched ( false ) ;
2020-08-02 02:52:13 +00:00
} else {
2021-05-01 18:24:18 +00:00
setValue ( initialPayload || '' ) ;
setTouched ( false ) ;
2020-08-02 02:52:13 +00:00
}
2022-06-26 19:13:52 +00:00
} , [ value ] ) ;
2020-03-22 21:11:26 +00:00
2021-05-01 18:24:18 +00:00
return null ;
2020-03-22 21:11:26 +00:00
} ;
2021-05-01 18:24:18 +00:00
const TaskDetailsModal = ( { schedule , task } : Props ) = > {
const { dismiss } = useContext ( ModalContext ) ;
2020-04-10 17:46:00 +00:00
const { clearFlashes , addError } = useFlash ( ) ;
2021-05-01 18:24:18 +00:00
2022-06-26 19:13:52 +00:00
const uuid = ServerContext . useStoreState ( ( state ) = > state . server . data ! . uuid ) ;
const appendSchedule = ServerContext . useStoreActions ( ( actions ) = > actions . schedules . appendSchedule ) ;
const backupLimit = ServerContext . useStoreState ( ( state ) = > state . server . data ! . featureLimits . backups ) ;
2020-03-19 05:28:32 +00:00
useEffect ( ( ) = > {
2021-05-01 18:24:18 +00:00
return ( ) = > {
clearFlashes ( 'schedule:task' ) ;
} ;
2020-03-19 05:28:32 +00:00
} , [ ] ) ;
const submit = ( values : Values , { setSubmitting } : FormikHelpers < Values > ) = > {
clearFlashes ( 'schedule:task' ) ;
2021-05-16 16:47:36 +00:00
if ( backupLimit === 0 && values . action === 'backup' ) {
setSubmitting ( false ) ;
2022-06-26 19:13:52 +00:00
addError ( {
message : "A backup task cannot be created when the server's backup limit is set to 0." ,
key : 'schedule:task' ,
} ) ;
2021-05-16 16:47:36 +00:00
} else {
createOrUpdateScheduleTask ( uuid , schedule . id , task ? . id , values )
2022-06-26 19:13:52 +00:00
. then ( ( task ) = > {
let tasks = schedule . tasks . map ( ( t ) = > ( t . id === task . id ? task : t ) ) ;
if ( ! schedule . tasks . find ( ( t ) = > t . id === task . id ) ) {
tasks = [ . . . tasks , task ] ;
2021-05-16 16:47:36 +00:00
}
2020-04-10 17:46:00 +00:00
2021-05-16 16:47:36 +00:00
appendSchedule ( { . . . schedule , tasks } ) ;
dismiss ( ) ;
} )
2022-06-26 19:13:52 +00:00
. catch ( ( error ) = > {
2021-05-16 16:47:36 +00:00
console . error ( error ) ;
setSubmitting ( false ) ;
addError ( { message : httpErrorToHuman ( error ) , key : 'schedule:task' } ) ;
} ) ;
}
2020-03-19 05:28:32 +00:00
} ;
return (
< Formik
onSubmit = { submit }
2021-05-01 18:24:18 +00:00
validationSchema = { schema }
2020-03-19 05:28:32 +00:00
initialValues = { {
action : task?.action || 'command' ,
payload : task?.payload || '' ,
timeOffset : task?.timeOffset.toString ( ) || '0' ,
2021-05-01 18:24:18 +00:00
continueOnFailure : task?.continueOnFailure || false ,
2020-03-19 05:28:32 +00:00
} }
>
2021-05-01 18:24:18 +00:00
{ ( { isSubmitting , values } ) = > (
< Form css = { tw ` m-0 ` } >
2022-06-26 19:13:52 +00:00
< FlashMessageRender byKey = { 'schedule:task' } css = { tw ` mb-4 ` } / >
2021-05-01 18:24:18 +00:00
< h2 css = { tw ` text-2xl mb-6 ` } > { task ? 'Edit Task' : 'Create Task' } < / h2 >
< div css = { tw ` flex ` } >
< div css = { tw ` mr-2 w-1/3 ` } >
< Label > Action < / Label >
2022-06-26 19:13:52 +00:00
< ActionListener / >
2021-05-01 18:24:18 +00:00
< FormikFieldWrapper name = { 'action' } >
< FormikField as = { Select } name = { 'action' } >
< option value = { 'command' } > Send command < / option >
< option value = { 'power' } > Send power action < / option >
< option value = { 'backup' } > Create backup < / option >
< / FormikField >
< / FormikFieldWrapper >
< / div >
< div css = { tw ` flex-1 ml-6 ` } >
< Field
name = { 'timeOffset' }
label = { 'Time offset (in seconds)' }
2022-06-26 19:13:52 +00:00
description = {
'The amount of time to wait after the previous task executes before running this one. If this is the first task on a schedule this will not be applied.'
}
2021-05-01 18:24:18 +00:00
/ >
< / div >
< / div >
< div css = { tw ` mt-6 ` } >
2022-06-26 19:13:52 +00:00
{ values . action === 'command' ? (
2021-05-01 18:24:18 +00:00
< div >
< Label > Payload < / Label >
< FormikFieldWrapper name = { 'payload' } >
2022-06-26 19:13:52 +00:00
< FormikField as = { Textarea } name = { 'payload' } rows = { 6 } / >
2021-05-01 18:24:18 +00:00
< / FormikFieldWrapper >
< / div >
2022-06-26 19:13:52 +00:00
) : values . action === 'power' ? (
< div >
< Label > Payload < / Label >
< FormikFieldWrapper name = { 'payload' } >
< FormikField as = { Select } name = { 'payload' } >
< option value = { 'start' } > Start the server < / option >
< option value = { 'restart' } > Restart the server < / option >
< option value = { 'stop' } > Stop the server < / option >
< option value = { 'kill' } > Terminate the server < / option >
< / FormikField >
< / FormikFieldWrapper >
< / div >
) : (
< div >
< Label > Ignored Files < / Label >
< FormikFieldWrapper
name = { 'payload' }
description = {
'Optional. Include the files and folders to be excluded in this backup. By default, the contents of your .pteroignore file will be used. If you have reached your backup limit, the oldest backup will be rotated.'
}
>
< FormikField as = { Textarea } name = { 'payload' } rows = { 6 } / >
< / FormikFieldWrapper >
< / div >
) }
2021-05-01 18:24:18 +00:00
< / div >
< div css = { tw ` mt-6 bg-neutral-700 border border-neutral-800 shadow-inner p-4 rounded ` } >
< FormikSwitch
name = { 'continueOnFailure' }
description = { 'Future tasks will be run when this task fails.' }
label = { 'Continue on Failure' }
/ >
< / div >
< div css = { tw ` flex justify-end mt-6 ` } >
< Button type = { 'submit' } disabled = { isSubmitting } >
{ task ? 'Save Changes' : 'Create Task' }
< / Button >
< / div >
< / Form >
2020-03-19 05:28:32 +00:00
) }
< / Formik >
) ;
} ;
2021-05-01 18:24:18 +00:00
export default asModal < Props > ( ) ( TaskDetailsModal ) ;