Skip to content

Commit

Permalink
fix: fixed calendar selection end being before start when using a filter
Browse files Browse the repository at this point in the history
  • Loading branch information
dereekb committed May 18, 2023
1 parent 865ef18 commit 25f905f
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ import {
isSameDateScheduleRange,
DateOrDateRangeOrDateBlockIndexOrDateBlockRange,
dateTimingRelativeIndexArrayFactory,
isInfiniteDateRange
isInfiniteDateRange,
copyHoursAndMinutesFromDate
} from '@dereekb/date';
import { filterMaybe, switchMapToDefault } from '@dereekb/rxjs';
import { Maybe, TimezoneString, DecisionFunction, IterableOrValue, iterableToArray, addToSet, toggleInSet, isIndexNumberInIndexRangeFunction, MaybeMap, minAndMaxNumber, setsAreEquivalent, DayOfWeek, range, AllOrNoneSelection, unique, mergeArrays, ArrayOrValue, objectHasNoKeys } from '@dereekb/util';
import { ComponentStore } from '@ngrx/component-store';
import { addYears, startOfDay, endOfDay, startOfYear } from 'date-fns';
import { addYears, startOfDay, endOfDay, startOfYear, isAfter, isBefore } from 'date-fns';
import { Observable, distinctUntilChanged, map, shareReplay, combineLatest, switchMap, of, tap, first } from 'rxjs';
import { CalendarScheduleSelectionCellContentFactory, CalendarScheduleSelectionValue, defaultCalendarScheduleSelectionCellContentFactory } from './calendar.schedule.selection';

Expand Down Expand Up @@ -630,16 +631,21 @@ export function computeScheduleSelectionValue(state: CalendarScheduleSelectionSt
return null;
}

const { start: rangeStart, end, excluded: allExcluded, dateBlockRange } = rangeAndExclusion;
const { start: rangeStart, end: rangeEnd, excluded: allExcluded, dateBlockRange } = rangeAndExclusion;
let filterOffsetExcludedRange: DateBlockIndex[] = [];
let indexOffset = dateBlockRange.i;

let start = rangeStart;
let end = rangeEnd;

// If computeSelectionResultRelativeToFilter is true, then we need to offset the values to be relative to that start.
if (computeSelectionResultRelativeToFilter && filter?.start) {
start = filter.start;

if (filter?.end) {
end = copyHoursAndMinutesFromDate(end, filter.end);
}

const filterStartIndexOffset = indexFactory(rangeStart) - indexFactory(start);
filterOffsetExcludedRange = range(0, filterStartIndexOffset);
indexOffset = indexOffset - filterStartIndexOffset;
Expand All @@ -659,6 +665,11 @@ export function computeScheduleSelectionValue(state: CalendarScheduleSelectionSt
const w: DateScheduleEncodedWeek = dateScheduleEncodedWeek(scheduleDays);
const d: DateBlockIndex[] = []; // "included" blocks are never used/calculated.

// Always ensure the end is after or equal to the start.
if (isBefore(end, start)) {
end = start; // end is start
}

const dateScheduleRange: DateScheduleRange = {
start,
end,
Expand Down

0 comments on commit 25f905f

Please sign in to comment.