import classNames = require("classnames"); import { observer } from "mobx-react"; import * as React from "react"; import * as ReactDOM from "react-dom"; import { SortableContainer, SortableElement, SortableHandle, SortEnd, arrayMove } from "react-sortable-hoc"; import { Form, Icon, List } from "semantic-ui-react"; import { DurationView, SectionChooser } from "@app/components/index"; import { Duration } from "@common/Duration"; import { ProgramItem, Section } from "@common/sprinklersRpc"; import "@app/styles/ProgramSequenceView"; const Handle = SortableHandle(() => ); @observer class ProgramSequenceItem extends React.Component<{ sequenceItem: ProgramItem, sections: Section[], onChange?: (newItem: ProgramItem) => void, }> { renderContent() { const { sequenceItem, sections } = this.props; const editing = this.props.onChange != null; const section = sections[sequenceItem.section]; const duration = Duration.fromSeconds(sequenceItem.duration); if (editing) { return ( ); } else { return ( {section.toString()} for {duration.toString()} ); } } render() { const editing = this.props.onChange != null; return (
  • {editing ? : } {this.renderContent()}
  • ); } private onSectionChange = (newSection: Section) => { if (!this.props.onChange) { return; } this.props.onChange(new ProgramItem({ ...this.props.sequenceItem, section: newSection.id, })); } private onDurationChange = (newDuration: Duration) => { if (!this.props.onChange) { return; } this.props.onChange(new ProgramItem({ ...this.props.sequenceItem, duration: newDuration.toSeconds(), })); } } const ProgramSequenceItemD = SortableElement(ProgramSequenceItem); type ItemChangeHandler = (newItem: ProgramItem, index: number) => void; const ProgramSequenceList = SortableContainer(observer(({ className, list, sections, onChange }: { className: string, list: ProgramItem[], sections: Section[], onChange?: ItemChangeHandler, }) => { const listItems = list.map((item, index) => { const onChangeHandler = onChange ? (newItem: ProgramItem) => onChange(newItem, index) : undefined; const key = `item-${index}`; return ( ); }); return ; }), { withRef: true }); @observer class ProgramSequenceView extends React.Component<{ sequence: ProgramItem[], sections: Section[], editing?: boolean, }> { render() { const { sequence, sections } = this.props; const editing = this.props.editing || false; const className = classNames("programSequence", { editing }); const onChange = editing ? this.changeItem : undefined; return ( ); } private changeItem: ItemChangeHandler = (newItem, index) => { this.props.sequence[index] = newItem; } private onSortEnd = ({oldIndex, newIndex}: SortEnd) => { const { sequence: array } = this.props; if (newIndex >= array.length) { return; } array.splice(newIndex, 0, array.splice(oldIndex, 1)[0]); } } const ProgramSequenceViewD = SortableContainer(ProgramSequenceView); export default ProgramSequenceViewD;