Browse Source

Fixed ScheduleDate

update-deps
Alex Mikhalev 7 years ago
parent
commit
c100c502cd
  1. 12
      app/components/ScheduleView/ScheduleDate.tsx
  2. 3
      app/pages/ProgramPage.tsx
  3. 15
      common/sprinklersRpc/Program.ts
  4. 30
      common/sprinklersRpc/schedule.ts

12
app/components/ScheduleView/ScheduleDate.tsx

@ -21,8 +21,9 @@ interface ScheduleDateState {
export default class ScheduleDate extends React.Component<ScheduleDateProps, ScheduleDateState> { export default class ScheduleDate extends React.Component<ScheduleDateProps, ScheduleDateState> {
static getDerivedStateFromProps(props: ScheduleDateProps, state: ScheduleDateState): Partial<ScheduleDateState> { static getDerivedStateFromProps(props: ScheduleDateProps, state: ScheduleDateState): Partial<ScheduleDateState> {
if (!DateOfYear.equals(props.date, state.lastDate)) { if (!DateOfYear.equals(props.date, state.lastDate)) {
const thisYear = moment().year();
const rawValue = props.date == null ? "" : 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 { lastDate: props.date, rawValue };
} }
return {}; return {};
@ -44,10 +45,11 @@ export default class ScheduleDate extends React.Component<ScheduleDateProps, Sch
} }
dayNode = <Input type="date" icon={clearIcon} value={this.state.rawValue} onChange={this.onChange} />; dayNode = <Input type="date" icon={clearIcon} value={this.state.rawValue} onChange={this.onChange} />;
} else { } else {
const m = moment(date || "");
let dayString: string; let dayString: string;
if (date) { if (m.isValid()) {
const format = (date.year === 0) ? "M/D" : "l"; const format = (m.year() === 0) ? "M/D" : "l";
dayString = moment(date).format(format); dayString = m.format(format);
} else { } else {
dayString = "N/A"; dayString = "N/A";
} }
@ -68,7 +70,7 @@ export default class ScheduleDate extends React.Component<ScheduleDateProps, Sch
const { onChange } = this.props; const { onChange } = this.props;
if (!onChange) return; if (!onChange) return;
const m = moment(data.value, HTML_DATE_INPUT_FORMAT); const m = moment(data.value, HTML_DATE_INPUT_FORMAT);
onChange(DateOfYear.fromMoment(m)); onChange(DateOfYear.fromMoment(m).with({ year: 0 }));
} }
private onClear = () => { private onClear = () => {

3
app/pages/ProgramPage.tsx

@ -113,8 +113,7 @@ class ProgramPage extends React.Component<ProgramPageProps> {
if (this.programView == null && this.program) { if (this.programView == null && this.program) {
// this.programView = createViewModel(this.program); // this.programView = createViewModel(this.program);
// this.programView = observable(toJS(this.program)); // this.programView = observable(toJS(this.program));
this.programView = new Program(this.program.device, this.program.id); this.programView = this.program.clone();
merge(this.programView, this.program);
} }
} else { } else {
if (this.programView != null) { if (this.programView != null) {

15
common/sprinklersRpc/Program.ts

@ -28,9 +28,12 @@ export class Program {
@observable.shallow sequence: ProgramItem[] = []; @observable.shallow sequence: ProgramItem[] = [];
@observable running: boolean = false; @observable running: boolean = false;
constructor(device: SprinklersDevice, id: number) { constructor(device: SprinklersDevice, id: number, data?: Partial<Program>) {
this.device = device; this.device = device;
this.id = id; this.id = id;
if (data) {
Object.assign(this, data);
}
} }
run() { run() {
@ -46,8 +49,16 @@ export class Program {
return this.device.updateProgram({ programId: this.id, data }); 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 { toString(): string {
return `Program{name="${this.name}", enabled=${this.enabled}, schedule=${this.schedule}, ` + return `Program{name="${this.name}", enabled=${this.enabled}, schedule=${this.schedule}, ` +
`sequence=${this.sequence}, running=${this.running}}`; `sequence=${this.sequence}, running=${this.running}}`;
} }
} }

30
common/sprinklersRpc/schedule.ts

@ -54,6 +54,8 @@ export enum Month {
} }
export class DateOfYear { 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 { static equals(a: DateOfYear | null | undefined, b: DateOfYear | null | undefined): boolean {
return (a === b) || ((a instanceof DateOfYear && b instanceof DateOfYear) && return (a === b) || ((a instanceof DateOfYear && b instanceof DateOfYear) &&
a.day === b.day && a.day === b.day &&
@ -62,17 +64,19 @@ export class DateOfYear {
} }
static fromMoment(m: Moment): 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 day!: number;
readonly month: Month; readonly month!: Month;
readonly year: number; readonly year!: number;
constructor(data?: Partial<DateOfYear>) {
Object.assign(this, DateOfYear.DEFAULT, data);
}
constructor(day: number = 0, month: Month = Month.January, year: number = 0) { with(data: Partial<DateOfYear>): DateOfYear {
this.day = day; return new DateOfYear(Object.assign({}, this, data));
this.month = month;
this.year = year;
} }
toString() { toString() {
@ -85,4 +89,14 @@ export class Schedule {
@observable weekdays: Weekday[] = []; @observable weekdays: Weekday[] = [];
@observable from: DateOfYear | null = null; @observable from: DateOfYear | null = null;
@observable to: DateOfYear | null = null; @observable to: DateOfYear | null = null;
constructor(data?: Partial<Schedule>) {
if (typeof data === "object") {
Object.assign(this, data);
}
}
clone(): Schedule {
return new Schedule(this);
}
} }

Loading…
Cancel
Save