From 46484cf028affb2d45d36c15d217a4bbc030d4d2 Mon Sep 17 00:00:00 2001 From: Petr Jasek Date: Tue, 16 Jul 2024 16:17:07 +0200 Subject: [PATCH] save --- client/selectors/events.ts | 2 ++ client/selectors/tests/events_test.ts | 2 +- client/utils/index.ts | 50 ++++++++++++++++----------- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/client/selectors/events.ts b/client/selectors/events.ts index 0b848a8d5..d53ea74d9 100644 --- a/client/selectors/events.ts +++ b/client/selectors/events.ts @@ -49,6 +49,8 @@ export const orderedEvents = createSelector( const dateRange = getSearchDateRange(search, appConfig.start_of_week); + console.info("RANGE", dateRange.startDate.toString(), dateRange.endDate.toString()); + return eventUtils.getEventsByDate(events, dateRange.startDate, dateRange.endDate); } ); diff --git a/client/selectors/tests/events_test.ts b/client/selectors/tests/events_test.ts index cfdd99af2..bc74c4ca6 100644 --- a/client/selectors/tests/events_test.ts +++ b/client/selectors/tests/events_test.ts @@ -38,7 +38,7 @@ describe('selectors', () => { session: {identity: {_id: 'user1'}}, }); - const setAdvancedSearchDates = (state, startDate, endate) => { + const setAdvancedSearchDates = (state, startDate, endate?) => { state.main = { filter: 'EVENTS', search: { diff --git a/client/utils/index.ts b/client/utils/index.ts index 6782b7bf3..70af18b4d 100644 --- a/client/utils/index.ts +++ b/client/utils/index.ts @@ -11,6 +11,8 @@ import { IPlanningCoverageItem, IIngestProvider, IFeaturedPlanningItem, + ISearchParams, + ICommonSearchParams, } from '../interfaces'; import {IUser} from 'superdesk-api'; import {superdeskApi} from '../superdeskApi'; @@ -685,43 +687,51 @@ export const isDateInRange = (inputDate, startDate, endDate) => { return true; }; -export const getSearchDateRange = (currentSearch, startOfWeek) => { - const dates = get(currentSearch, 'advancedSearch.dates', {}); + +interface IDateRange { + startDate: moment.Moment; + endDate: moment.Moment; +} + +export const getSearchDateRange = (currentSearch: ICommonSearchParams, startOfWeek?: number): IDateRange => { + const dates = currentSearch.advancedSearch.dates ?? {}; const dateRange = {startDate: null, endDate: null}; - if (!get(dates, 'start') && !get(dates, 'end') && !get(dates, 'range')) { - dateRange.startDate = moment(moment().format('YYYY-MM-DD'), 'YYYY-MM-DD', true); - dateRange.endDate = moment().add(999, 'years'); - } else if (get(dates, 'range')) { - let range = get(dates, 'range'); + console.info("DATES", JSON.stringify(dates)); - if (range === MAIN.DATE_RANGE.TODAY) { + if (dates.range != null) { + if (dates.range === MAIN.DATE_RANGE.TODAY) { dateRange.startDate = moment(moment().format('YYYY-MM-DD'), 'YYYY-MM-DD', true); dateRange.endDate = dateRange.startDate.clone().add('86399', 'seconds'); - } else if (range === MAIN.DATE_RANGE.TOMORROW) { + } else if (dates.range === MAIN.DATE_RANGE.TOMORROW) { const tomorrow = moment().add(1, 'day'); dateRange.startDate = moment(tomorrow.format('YYYY-MM-DD'), 'YYYY-MM-DD', true); dateRange.endDate = tomorrow.clone().add('86399', 'seconds'); - } else if (range === MAIN.DATE_RANGE.LAST_24) { + } else if (dates.range === MAIN.DATE_RANGE.LAST_24) { dateRange.endDate = moment(); dateRange.startDate = dateRange.endDate.clone().subtract('86400', 'seconds'); - } else if (range === MAIN.DATE_RANGE.THIS_WEEK) { + } else if (dates.range === MAIN.DATE_RANGE.THIS_WEEK) { dateRange.endDate = timeUtils.getStartOfNextWeek(null, startOfWeek); dateRange.startDate = dateRange.endDate.clone().subtract(7, 'days'); - } else if (range === MAIN.DATE_RANGE.NEXT_WEEK) { + } else if (dates.range === MAIN.DATE_RANGE.NEXT_WEEK) { dateRange.endDate = timeUtils.getStartOfNextWeek(null, startOfWeek).add(7, 'days'); dateRange.startDate = dateRange.endDate.clone().subtract(7, 'days'); } - } else { - if (get(dates, 'start')) { - dateRange.startDate = moment(get(dates, 'start')); - } - - if (get(dates, 'end')) { - dateRange.endDate = moment(get(dates, 'end')); - } + } else if (dates.start != null && dates.end != null) { + dateRange.startDate = moment(dates.start); + dateRange.endDate = moment(dates.end); + } else if (dates.start != null) { + dateRange.startDate = moment(dates.start); + dateRange.endDate = dateRange.startDate.clone().add(1, 'month'); + } else if (dates.end != null) { + dateRange.endDate = moment(dates.end); + dateRange.startDate = dateRange.endDate.clone().subtract(1, 'month'); + } else { // Default to today + dateRange.startDate = moment().startOf('day'); + dateRange.endDate = moment().add(1, 'month'); } + return dateRange; };