Skip to content

Commit

Permalink
fix(jobs): small improvments in the creation flow
Browse files Browse the repository at this point in the history
  • Loading branch information
bdebon committed Dec 22, 2022
1 parent a07b9a5 commit 861c047
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 22 deletions.
Expand Up @@ -11,7 +11,7 @@ import PageSettingsConfigureJob from '../../ui/page-settings-configure-job/page-

export function PageSettingsConfigureJobFeature() {
const { applicationId = '', environmentId = '' } = useParams()
const methods = useForm<JobConfigureData>()
const methods = useForm<JobConfigureData>({ mode: 'onChange' })

const application: JobApplicationEntity | undefined = useSelector<RootState, ApplicationEntity | undefined>(
(state) => selectApplicationById(state, applicationId),
Expand Down Expand Up @@ -44,15 +44,21 @@ export function PageSettingsConfigureJobFeature() {
methods.setValue('image_entry_point', application.schedule?.cronjob?.entrypoint || undefined)
} else {
methods.setValue('on_start.enabled', !!application.schedule?.on_start)
methods.setValue('on_start.arguments_string', JSON.stringify(application.schedule?.on_start?.arguments))
if (application.schedule?.on_start?.arguments && application.schedule?.on_start?.arguments.length > 0) {
methods.setValue('on_start.arguments_string', JSON.stringify(application.schedule.on_start.arguments))
}
methods.setValue('on_start.entrypoint', application.schedule?.on_start?.entrypoint)

methods.setValue('on_stop.enabled', !!application.schedule?.on_stop)
methods.setValue('on_stop.arguments_string', JSON.stringify(application.schedule?.on_stop?.arguments))
if (application.schedule?.on_stop?.arguments && application.schedule?.on_stop?.arguments.length > 0) {
methods.setValue('on_stop.arguments_string', JSON.stringify(application.schedule?.on_stop?.arguments))
}
methods.setValue('on_stop.entrypoint', application.schedule?.on_stop?.entrypoint)

methods.setValue('on_delete.enabled', !!application.schedule?.on_delete)
methods.setValue('on_delete.arguments_string', JSON.stringify(application.schedule?.on_delete?.arguments))
if (application.schedule?.on_delete?.arguments && application.schedule?.on_delete?.arguments.length > 0) {
methods.setValue('on_delete.arguments_string', JSON.stringify(application.schedule?.on_delete?.arguments))
}
methods.setValue('on_delete.entrypoint', application.schedule?.on_delete?.entrypoint)
}
}
Expand Down Expand Up @@ -80,6 +86,8 @@ export function PageSettingsConfigureJobFeature() {
toastError(e, 'Invalid CMD array')
return
}
} else {
schedule.cronjob.arguments = undefined
}
schedule.cronjob.entrypoint = data.image_entry_point
}
Expand All @@ -91,45 +99,48 @@ export function PageSettingsConfigureJobFeature() {
if (data.on_start?.enabled) {
schedule.on_start = {
entrypoint: data.on_start.entrypoint,
arguments: undefined,
}

try {
if (data.on_start?.arguments_string) {
if (data.on_start?.arguments_string && data.on_start?.arguments_string.length > 0) {
try {
schedule.on_start.arguments = eval(data.on_start.arguments_string)
} catch (e: any) {
toastError(e, 'Invalid CMD array')
return
}
} catch (e: any) {
toastError(e, 'Invalid CMD array')
return
}
}

if (data.on_stop?.enabled) {
schedule.on_stop = {
entrypoint: data.on_stop.entrypoint,
arguments: undefined,
}

try {
if (data.on_stop?.arguments_string) {
if (data.on_stop?.arguments_string && data.on_stop?.arguments_string.length > 0) {
try {
schedule.on_stop.arguments = eval(data.on_stop.arguments_string)
} catch (e: any) {
toastError(e, 'Invalid CMD array')
return
}
} catch (e: any) {
toastError(e, 'Invalid CMD array')
return
}
}

if (data.on_delete?.enabled) {
schedule.on_delete = {
entrypoint: data.on_delete.entrypoint,
arguments: undefined,
}

try {
if (data.on_delete?.arguments_string) {
if (data.on_delete?.arguments_string && data.on_delete?.arguments_string.length > 0) {
try {
schedule.on_delete.arguments = eval(data.on_delete.arguments_string)
} catch (e: any) {
toastError(e, 'Invalid CMD array')
return
}
} catch (e: any) {
toastError(e, 'Invalid CMD array')
return
}
}

Expand Down
Expand Up @@ -53,7 +53,7 @@ function prepareJobRequest(
cronjob: {
entrypoint: configureData.image_entry_point,
scheduled_at: configureData.schedule || '',
arguments: configureData.cmd || [''],
arguments: configureData.cmd,
},
}
} else {
Expand Down
Expand Up @@ -21,7 +21,7 @@ export function StepVariableFeature() {
const { organizationId = '', projectId = '', environmentId = '' } = useParams()
const navigate = useNavigate()
const pathCreate = `${SERVICES_URL(organizationId, projectId, environmentId)}${jobURL}`
const [availableScopes] = useState<APIVariableScopeEnum[]>(computeAvailableScope())
const [availableScopes] = useState<APIVariableScopeEnum[]>(computeAvailableScope(undefined, false, jobType))

useEffect(() => {
!generalData?.name &&
Expand Down Expand Up @@ -56,7 +56,12 @@ export function StepVariableFeature() {
const [variables, setVariables] = useState(methods.getValues().variables)

const onAddPort = () => {
const newVariableRow: VariableData = { variable: undefined, isSecret: false, value: undefined, scope: undefined }
const newVariableRow: VariableData = {
variable: '',
isSecret: false,
value: '',
scope: APIVariableScopeEnum.JOB,
}
if (variables.length) {
setVariables([...variables, newVariableRow])
methods.setValue(`variables.${variables.length}`, newVariableRow)
Expand Down
Expand Up @@ -24,6 +24,8 @@ export function JobConfigureSettings(props: JobConfigureSettingsProps) {
const isValidCron = cronstrue.toString(watchSchedule, { throwExceptionOnParseError: false })
if (isValidCron.indexOf('An error') === -1) {
setCronDescription(isValidCron)
} else {
setCronDescription('')
}
}
}, [watchSchedule])
Expand All @@ -40,6 +42,12 @@ export function JobConfigureSettings(props: JobConfigureSettingsProps) {
control={control}
rules={{
required: 'Value required',
validate: (value) => {
return (
cronstrue.toString(value || '', { throwExceptionOnParseError: false }).indexOf('An error') === -1 ||
'Invalid cron expression'
)
},
}}
render={({ field, fieldState: { error } }) => (
<InputText
Expand Down
1 change: 1 addition & 0 deletions libs/shared/utils/src/lib/tools/refacto-payload.ts
Expand Up @@ -121,6 +121,7 @@ export function refactoJobPayload(job: Partial<JobApplicationEntity>): JobReques
}
}

console.log('jobRequest', job.schedule)
jobRequest.schedule = job.schedule

return jobRequest
Expand Down

0 comments on commit 861c047

Please sign in to comment.