diff --git a/app/components/ScheduleView/ScheduleDate.tsx b/app/components/ScheduleView/ScheduleDate.tsx index 38e3f71..25b06ca 100644 --- a/app/components/ScheduleView/ScheduleDate.tsx +++ b/app/components/ScheduleView/ScheduleDate.tsx @@ -21,8 +21,9 @@ interface ScheduleDateState { export default class ScheduleDate extends React.Component { static getDerivedStateFromProps(props: ScheduleDateProps, state: ScheduleDateState): Partial { if (!DateOfYear.equals(props.date, state.lastDate)) { + const thisYear = moment().year(); const rawValue = props.date == null ? "" : - moment(props.date).format(HTML_DATE_INPUT_FORMAT); + moment(props.date).year(thisYear).format(HTML_DATE_INPUT_FORMAT); return { lastDate: props.date, rawValue }; } return {}; @@ -44,10 +45,11 @@ export default class ScheduleDate extends React.Component; } else { + const m = moment(date || ""); let dayString: string; - if (date) { - const format = (date.year === 0) ? "M/D" : "l"; - dayString = moment(date).format(format); + if (m.isValid()) { + const format = (m.year() === 0) ? "M/D" : "l"; + dayString = m.format(format); } else { dayString = "N/A"; } @@ -68,7 +70,7 @@ export default class ScheduleDate extends React.Component { diff --git a/app/pages/ProgramPage.tsx b/app/pages/ProgramPage.tsx index 223aff3..4b4b493 100644 --- a/app/pages/ProgramPage.tsx +++ b/app/pages/ProgramPage.tsx @@ -113,8 +113,7 @@ class ProgramPage extends React.Component { if (this.programView == null && this.program) { // this.programView = createViewModel(this.program); // this.programView = observable(toJS(this.program)); - this.programView = new Program(this.program.device, this.program.id); - merge(this.programView, this.program); + this.programView = this.program.clone(); } } else { if (this.programView != null) { diff --git a/common/sprinklersRpc/Program.ts b/common/sprinklersRpc/Program.ts index 85c1273..1b32b69 100644 --- a/common/sprinklersRpc/Program.ts +++ b/common/sprinklersRpc/Program.ts @@ -28,9 +28,12 @@ export class Program { @observable.shallow sequence: ProgramItem[] = []; @observable running: boolean = false; - constructor(device: SprinklersDevice, id: number) { + constructor(device: SprinklersDevice, id: number, data?: Partial) { this.device = device; this.id = id; + if (data) { + Object.assign(this, data); + } } run() { @@ -46,8 +49,16 @@ export class Program { return this.device.updateProgram({ programId: this.id, data }); } + clone(): Program { + return new Program(this.device, this.id, { + name: this.name, enabled: this.enabled, running: this.running, + schedule: this.schedule.clone(), + sequence: this.sequence.slice(), + }); + } + toString(): string { return `Program{name="${this.name}", enabled=${this.enabled}, schedule=${this.schedule}, ` + - `sequence=${this.sequence}, running=${this.running}}`; + `sequence=${this.sequence}, running=${this.running}}`; } } diff --git a/common/sprinklersRpc/schedule.ts b/common/sprinklersRpc/schedule.ts index 09d8ba2..1bdc1a6 100644 --- a/common/sprinklersRpc/schedule.ts +++ b/common/sprinklersRpc/schedule.ts @@ -54,6 +54,8 @@ export enum Month { } export class DateOfYear { + static readonly DEFAULT = new DateOfYear({ day: 1, month: Month.January, year: 0 }); + static equals(a: DateOfYear | null | undefined, b: DateOfYear | null | undefined): boolean { return (a === b) || ((a instanceof DateOfYear && b instanceof DateOfYear) && a.day === b.day && @@ -62,17 +64,19 @@ export class DateOfYear { } static fromMoment(m: Moment): DateOfYear { - return new DateOfYear(m.date(), m.month(), m.year()); + return new DateOfYear({ day: m.date(), month: m.month(), year: m.year() }); } - readonly day: number; - readonly month: Month; - readonly year: number; + readonly day!: number; + readonly month!: Month; + readonly year!: number; + + constructor(data?: Partial) { + Object.assign(this, DateOfYear.DEFAULT, data); + } - constructor(day: number = 0, month: Month = Month.January, year: number = 0) { - this.day = day; - this.month = month; - this.year = year; + with(data: Partial): DateOfYear { + return new DateOfYear(Object.assign({}, this, data)); } toString() { @@ -85,4 +89,14 @@ export class Schedule { @observable weekdays: Weekday[] = []; @observable from: DateOfYear | null = null; @observable to: DateOfYear | null = null; + + constructor(data?: Partial) { + if (typeof data === "object") { + Object.assign(this, data); + } + } + + clone(): Schedule { + return new Schedule(this); + } }