From f3453449742532a634c6f36c773ba50aabc06b1f Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Fri, 17 Aug 2018 13:57:41 -0600 Subject: [PATCH] Refer to section by id in SectionChooser --- client/components/RunSectionForm.tsx | 19 ++++++++++--------- client/components/SectionChooser.tsx | 17 ++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/client/components/RunSectionForm.tsx b/client/components/RunSectionForm.tsx index c14425c..93391bd 100644 --- a/client/components/RunSectionForm.tsx +++ b/client/components/RunSectionForm.tsx @@ -15,18 +15,18 @@ export default class RunSectionForm extends React.Component<{ uiStore: UiStore, }, { duration: Duration, - section: Section | undefined, + sectionId: number | undefined, }> { constructor(props: any, context?: any) { super(props, context); this.state = { duration: new Duration(0, 0), - section: undefined, + sectionId: undefined, }; } render() { - const { section, duration } = this.state; + const { sectionId, duration } = this.state; return (
Run Section
@@ -34,7 +34,7 @@ export default class RunSectionForm extends React.Component<{ { - this.setState({ section: newSection }); + private onSectionChange = (newSectionId: number) => { + this.setState({ sectionId: newSectionId }); } private onDurationChange = (newDuration: Duration) => { @@ -65,10 +65,11 @@ export default class RunSectionForm extends React.Component<{ private run = (e: React.SyntheticEvent) => { e.preventDefault(); - const { section, duration } = this.state; - if (!section) { + const { sectionId, duration } = this.state; + if (sectionId == null) { return; } + const section = this.props.device.sections[sectionId]; section.run(duration.toSeconds()) .then(this.onRunSuccess) .catch(this.onRunError); @@ -91,6 +92,6 @@ export default class RunSectionForm extends React.Component<{ } private get isValid(): boolean { - return this.state.section != null && this.state.duration.toSeconds() > 0; + return this.state.sectionId != null && this.state.duration.toSeconds() > 0; } } diff --git a/client/components/SectionChooser.tsx b/client/components/SectionChooser.tsx index 1ac7984..18463d2 100644 --- a/client/components/SectionChooser.tsx +++ b/client/components/SectionChooser.tsx @@ -12,17 +12,16 @@ export default class SectionChooser extends React.Component<{ label?: string, inline?: boolean, sections: Section[], - value?: Section, - onChange?: (section: Section) => void, + sectionId?: number, + onChange?: (sectionId: number) => void, }> { render() { - const { label, inline, sections, value, onChange } = this.props; - let section = (value == null) ? "" : sections.indexOf(value); - section = (section === -1) ? "" : section; - const onSectionChange = (onChange == null) ? undefined : this.onSectionChange; + const { label, inline, sections, sectionId, onChange } = this.props; if (onChange == null) { - return {label || ""} '{value ? value.toString() : ""}'; + const sectionStr = sectionId != null ? sections[sectionId].toString() : ""; + return {label || ""} '{sectionStr}'; } + const section = (sectionId == null) ? "" : sectionId; return ( ); } private onSectionChange = (e: React.SyntheticEvent, v: DropdownProps) => { - this.props.onChange!(this.props.sections[v.value as number]); + this.props.onChange!(this.props.sections[v.value as number].id); } @computed