Skip to content

Commit

Permalink
fix: fixed DbxCalendarScheduleSelectionStore
Browse files Browse the repository at this point in the history
- fixed issues with DbxCalendarScheduleSelectionStore related to the DateCellTiming updates
  • Loading branch information
dereekb committed Oct 15, 2023
1 parent 1b9f014 commit 14014af
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { addMonths, setHours, startOfDay, addDays, addHours } from 'date-fns/esm
import { Building, Maybe, TimezoneString, isEvenNumber, range } from '@dereekb/util';
import { CalendarEvent } from 'angular-calendar';
import { CalendarScheduleSelectionDayState, DbxScheduleSelectionCalendarComponentConfig, dateScheduleRangeField } from '@dereekb/dbx-form/calendar';
import { BehaviorSubject, interval, map, of, shareReplay, startWith } from 'rxjs';
import { BehaviorSubject, delay, interval, map, of, shareReplay, startWith } from 'rxjs';
import { DOC_EXTENSION_CALENDAR_SCHEDULE_TEST_FILTER } from '../component/selection.filter.calendar.component';
import { timezoneStringField } from '@dereekb/dbx-form';
import { FormlyFieldConfig } from '@ngx-formly/core';
Expand Down Expand Up @@ -34,6 +34,11 @@ export class DocExtensionCalendarComponent implements OnInit {
event: Maybe<DbxCalendarEvent<TestCalendarEventData>>;

readonly defaultDateCellScheduleRangeFieldValue$ = of({
futureDateSchedule: {
start: addDays(startOfDay(new Date()), 1),
end: addDays(startOfDay(new Date()), 3),
w: '89'
},
dateSchedule: {
start: startOfDay(new Date()),
end: addDays(startOfDay(new Date()), 14),
Expand All @@ -56,8 +61,8 @@ export class DocExtensionCalendarComponent implements OnInit {
key: 'futureDateSchedule',
required: false,
label: 'Future Dates',
timezone: this.timezone$,
defaultScheduleDays: expandDateCellScheduleDayCodes('8'),
outputTimezone: this.timezone$,
defaultScheduleDays: expandDateCellScheduleDayCodes('89'),
minMaxDateRange: {
start: startOfDay(new Date())
},
Expand Down
12 changes: 12 additions & 0 deletions packages/date/src/lib/date/date.cell.schedule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,18 @@ describe('isSameDateCellSchedule()', () => {
const scheduleB: DateCellSchedule = { ...schedule, d: [] };
expect(isSameDateCellSchedule(schedule, scheduleB)).toBe(false);
});

it('should return true for equivalent date schedule, one with an empty d date and one with an undefined d date', () => {
const schedule: DateCellSchedule = { w: '89', ex: [0, 1], d: [] };
const scheduleB: DateCellSchedule = { ...schedule, d: undefined };
expect(isSameDateCellSchedule(schedule, scheduleB)).toBe(true);
});

it('should return true for equivalent date schedule, one with an empty ex date and one with an undefined ex date', () => {
const schedule: DateCellSchedule = { w: '89', ex: [] };
const scheduleB: DateCellSchedule = { ...schedule, ex: undefined };
expect(isSameDateCellSchedule(schedule, scheduleB)).toBe(true);
});
});

describe('fullDateCellScheduleRange()', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/date/src/lib/date/date.cell.schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export function isDateCellSchedule(input: object): input is DateCellSchedule {

export function isSameDateCellSchedule(a: Maybe<DateCellSchedule>, b: Maybe<DateCellSchedule>): boolean {
if (a && b) {
return a.w === b.w && iterablesAreSetEquivalent(a.ex, b.ex) && iterablesAreSetEquivalent(a.d, b.d);
return a.w === b.w && iterablesAreSetEquivalent(a.ex ?? [], b.ex ?? []) && iterablesAreSetEquivalent(a.d ?? [], b.d ?? []);
} else {
return a == b;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DbxCalendarScheduleSelectionStore } from './calendar.schedule.selection
import { DbxCalendarStore } from '@dereekb/dbx-web/calendar';
import { FormGroup, FormControl, AbstractControl } from '@angular/forms';
import { Maybe } from '@dereekb/util';
import { switchMap, throttleTime, distinctUntilChanged, filter, BehaviorSubject, startWith, Observable, of, map, shareReplay, combineLatest } from 'rxjs';
import { switchMap, throttleTime, distinctUntilChanged, filter, BehaviorSubject, startWith, Observable, of, map, shareReplay, combineLatest, EMPTY } from 'rxjs';
import { isSameDateDay } from '@dereekb/date';
import { MatFormFieldDefaultOptions, MAT_FORM_FIELD_DEFAULT_OPTIONS } from '@angular/material/form-field';
import { ErrorStateMatcher } from '@angular/material/core';
Expand Down Expand Up @@ -141,7 +141,7 @@ export class DbxScheduleSelectionCalendarDateRangeComponent implements OnInit, O
let obs: Observable<RangeValue>;

if (opened) {
obs = of({});
obs = EMPTY;
} else {
obs = this.range.valueChanges.pipe(startWith(this.range.value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,9 @@ export class DbxCalendarScheduleSelectionStore extends ComponentStore<CalendarSc
);

readonly currentInputRange$: Observable<Maybe<CalendarScheduleSelectionInputDateRange>> = this.state$.pipe(
map(({ inputStart, inputEnd }) => ({ inputStart, inputEnd })),
distinctUntilChanged((a, b) => isSameDate(a.inputStart, b.inputStart) && isSameDate(a.inputEnd, b.inputEnd)),
map(({ inputStart, inputEnd }) => ({ start: inputStart, end: inputEnd })),
distinctUntilChanged((a, b) => isSameDateRange(a as DateRange, b as DateRange)),
map((x) => ({ inputStart: x.start, inputEnd: x.end })),
map((x) => {
if (x.inputStart && x.inputEnd) {
return x as CalendarScheduleSelectionInputDateRange;
Expand Down Expand Up @@ -627,6 +628,7 @@ export function updateStateWithFilter(state: CalendarScheduleSelectionState, inp
filterStart = timezoneNormal.startOfDayInTargetTimezone(inputFilter.startsAt);
} else {
filterStart = inputFilter.startsAt;
filter.timezone = systemTimezone;
}
}

Expand Down Expand Up @@ -712,16 +714,22 @@ export function updateStateWithTimezoneValue(state: CalendarScheduleSelectionSta
}

export function updateStateWithDateCellScheduleRangeValue(state: CalendarScheduleSelectionState, inputChange: Maybe<FullDateCellScheduleRangeInputDateRange>): CalendarScheduleSelectionState {
const { currentSelectionValue } = state;
const { currentSelectionValue, systemTimezone } = state;
const currentDateCellScheduleRange = currentSelectionValue?.dateScheduleRange; // current range is always in system time
let change: Maybe<Pick<DateCellScheduleDateRange, 'start' | 'end' | 'ex' | 'w' | 'timezone'>>;

// When using timezones, always return from the start of the day. Inputs are converted to the system time and used as the start of the day.
// Outputs remain accurate.

if (inputChange) {
// make sure a timezone is set. Input may not have a timezone attached. Default to system time.
const inputChangeWithTimezoneSet = {
...inputChange,
timezone: inputChange.timezone ?? systemTimezone
};

// calculate the schedule range
const fullChange = fullDateCellScheduleRange({ dateCellScheduleRange: inputChange });
const fullChange = fullDateCellScheduleRange({ dateCellScheduleRange: inputChangeWithTimezoneSet });
const inputNormal = dateCellTimingTimezoneNormalInstance(fullChange);

const startInSystemTz = inputNormal.systemDateToTargetDate(fullChange.start);
Expand Down Expand Up @@ -1039,8 +1047,17 @@ export function computeScheduleSelectionValue(state: CalendarScheduleSelectionSt
};
}

/**
* The selected date range and the corresponding cell range.
*/
export interface CalendarScheduleSelectionRangeAndExclusion extends DateRange {
/**
* Corresponds to the start and end indexes in the date range.
*/
dateCellRange: DateCellRangeWithRange;
/**
* All excluded indexes.
*/
excluded: DateCellIndex[];
}

Expand Down

0 comments on commit 14014af

Please sign in to comment.