Fixed ScheduleDate
This commit is contained in:
parent
a3c6fa2b8e
commit
c100c502cd
@ -21,8 +21,9 @@ interface ScheduleDateState {
|
||||
export default class ScheduleDate extends React.Component<ScheduleDateProps, ScheduleDateState> {
|
||||
static getDerivedStateFromProps(props: ScheduleDateProps, state: ScheduleDateState): Partial<ScheduleDateState> {
|
||||
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<ScheduleDateProps, Sch
|
||||
}
|
||||
dayNode = <Input type="date" icon={clearIcon} value={this.state.rawValue} onChange={this.onChange} />;
|
||||
} 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<ScheduleDateProps, Sch
|
||||
const { onChange } = this.props;
|
||||
if (!onChange) return;
|
||||
const m = moment(data.value, HTML_DATE_INPUT_FORMAT);
|
||||
onChange(DateOfYear.fromMoment(m));
|
||||
onChange(DateOfYear.fromMoment(m).with({ year: 0 }));
|
||||
}
|
||||
|
||||
private onClear = () => {
|
||||
|
@ -113,8 +113,7 @@ class ProgramPage extends React.Component<ProgramPageProps> {
|
||||
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) {
|
||||
|
@ -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<Program>) {
|
||||
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}}`;
|
||||
}
|
||||
}
|
||||
|
@ -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(day: number = 0, month: Month = Month.January, year: number = 0) {
|
||||
this.day = day;
|
||||
this.month = month;
|
||||
this.year = year;
|
||||
constructor(data?: Partial<DateOfYear>) {
|
||||
Object.assign(this, DateOfYear.DEFAULT, data);
|
||||
}
|
||||
|
||||
with(data: Partial<DateOfYear>): 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<Schedule>) {
|
||||
if (typeof data === "object") {
|
||||
Object.assign(this, data);
|
||||
}
|
||||
}
|
||||
|
||||
clone(): Schedule {
|
||||
return new Schedule(this);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user