-
Notifications
You must be signed in to change notification settings - Fork 900
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): Reactify overrideTimeout (#7126)
- Loading branch information
Jammy Louie
committed
Jun 20, 2019
1 parent
0a3bd68
commit 4782808
Showing
7 changed files
with
163 additions
and
228 deletions.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
app/scripts/modules/core/src/pipeline/config/stages/overrideTimeout/OverrideTimeout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import * as React from 'react'; | ||
|
||
import { IStage } from 'core/domain'; | ||
import { CheckboxInput, NumberInput } from 'core/presentation'; | ||
import { HelpContentsRegistry, HelpField } from 'core/help'; | ||
|
||
const { useEffect, useState } = React; | ||
|
||
export interface IOverrideTimeoutConfigProps { | ||
stageConfig: IStageConfig; | ||
stageTimeoutMs: number; | ||
updateStageField: (changes: Partial<IStage>) => void; | ||
} | ||
|
||
interface IStageConfig { | ||
defaultTimeoutMs: number; | ||
} | ||
|
||
export const OverrideTimeout = (props: IOverrideTimeoutConfigProps) => { | ||
const [hours, setHours] = useState(0); | ||
const [minutes, setMinutes] = useState(0); | ||
const [overrideTimeout, setOverrideTimeout] = useState(false); | ||
const [configurable, setConfigurable] = useState(false); | ||
const [defaults, setDefaults] = useState({ hours: 0, minutes: 0 }); | ||
const helpContent = HelpContentsRegistry.getHelpField('pipeline.config.timeout'); | ||
|
||
useEffect(() => { | ||
setOverrideValues(overrideTimeout); | ||
}, [props.stageConfig]); | ||
|
||
const setOverrideValues = (newOverrideTimeout: boolean) => { | ||
const stageDefaults = props.stageConfig ? props.stageConfig.defaultTimeoutMs : null; | ||
const originalOverrideTimeout = newOverrideTimeout === true; | ||
const shouldRemoveOverride = originalOverrideTimeout === false; | ||
|
||
setConfigurable(!!stageDefaults); | ||
setDefaults(toHoursAndMinutes(stageDefaults)); | ||
|
||
if (shouldRemoveOverride) { | ||
props.updateStageField({ stageTimeoutMs: undefined }); | ||
} else if (originalOverrideTimeout || props.stageTimeoutMs !== undefined) { | ||
// Either vm.overrideTimeout was originally true, or forcing to true because stageTimeoutMs is defined | ||
setOverrideTimeout(true); | ||
props.updateStageField({ | ||
stageTimeoutMs: props.stageTimeoutMs || stageDefaults, | ||
}); | ||
setHours(toHoursAndMinutes(props.stageTimeoutMs).hours); | ||
setMinutes(toHoursAndMinutes(props.stageTimeoutMs).minutes); | ||
} | ||
}; | ||
|
||
const synchronizeTimeout = (h: number, m: number) => { | ||
let timeout = 0; | ||
timeout += 60 * 60 * 1000 * h; | ||
timeout += 60 * 1000 * m; | ||
props.updateStageField({ stageTimeoutMs: timeout }); | ||
}; | ||
|
||
function toHoursAndMinutes(ms: number) { | ||
if (!ms) { | ||
return { hours: 0, minutes: 0 }; | ||
} else { | ||
const seconds = ms / 1000; | ||
return { | ||
hours: Math.floor(seconds / 3600), | ||
minutes: Math.floor(seconds / 60) % 60, | ||
}; | ||
} | ||
} | ||
|
||
if (configurable) { | ||
return ( | ||
<> | ||
<div className="form-group"> | ||
<div className="col-md-9 col-md-offset-1"> | ||
<div className="checkbox"> | ||
<CheckboxInput | ||
text={ | ||
<> | ||
<strong> Override default timeout</strong> ( | ||
{defaults.hours > 0 && ( | ||
<span> | ||
{defaults.hours} {defaults.hours > 1 ? 'hours' : 'hour'}{' '} | ||
</span> | ||
)} | ||
{defaults.minutes > 0 && ( | ||
<span> | ||
{defaults.minutes} {defaults.minutes > 1 ? 'minutes' : 'minute'} | ||
</span> | ||
)} | ||
)<HelpField content={helpContent} /> | ||
</> | ||
} | ||
value={overrideTimeout} | ||
onChange={() => { | ||
const newOverrideTimeout = !overrideTimeout; | ||
setOverrideTimeout(newOverrideTimeout); | ||
setOverrideValues(newOverrideTimeout); | ||
}} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
{overrideTimeout && ( | ||
<div> | ||
<div className="form-group form-inline"> | ||
<div className="col-md-9 col-md-offset-1 checkbox-padding"> | ||
Fail this stage if it takes longer than | ||
<NumberInput | ||
inputClassName={'form-control input-sm inline-number with-space-before'} | ||
min={0} | ||
onChange={(e: React.ChangeEvent<any>) => { | ||
setHours(e.target.value); | ||
synchronizeTimeout(e.target.value, minutes); | ||
}} | ||
value={hours} | ||
/> | ||
hours | ||
<NumberInput | ||
inputClassName={'form-control input-sm inline-number with-space-before'} | ||
min={0} | ||
onChange={(e: React.ChangeEvent<any>) => { | ||
setMinutes(e.target.value); | ||
synchronizeTimeout(hours, e.target.value); | ||
}} | ||
value={minutes} | ||
/> | ||
minutes to complete | ||
</div> | ||
</div> | ||
</div> | ||
)} | ||
</> | ||
); | ||
} else { | ||
return <></>; | ||
} | ||
}; |
46 changes: 0 additions & 46 deletions
46
...ts/modules/core/src/pipeline/config/stages/overrideTimeout/overrideTimeout.directive.html
This file was deleted.
Oops, something went wrong.
79 changes: 0 additions & 79 deletions
79
...ipts/modules/core/src/pipeline/config/stages/overrideTimeout/overrideTimeout.directive.js
This file was deleted.
Oops, something went wrong.
101 changes: 0 additions & 101 deletions
101
...modules/core/src/pipeline/config/stages/overrideTimeout/overrideTimeout.directive.spec.js
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
...scripts/modules/core/src/pipeline/config/stages/overrideTimeout/overrideTimeout.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { module } from 'angular'; | ||
import { react2angular } from 'react2angular'; | ||
|
||
import { OverrideTimeout } from './OverrideTimeout'; | ||
|
||
export const OVERRIDE_TIMEOUT_COMPONENT = 'spinnaker.core.pipeline.stage.overrideTimeout'; | ||
module(OVERRIDE_TIMEOUT_COMPONENT, []).component( | ||
'overrideTimeout', | ||
react2angular(OverrideTimeout, ['stageConfig', 'stageTimeoutMs', 'updateStageField']), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.