From c744d73aef53ce5afab498424f12bbcc8e00c8aa Mon Sep 17 00:00:00 2001 From: Rares Mardare Date: Wed, 5 Oct 2022 13:37:51 +0300 Subject: [PATCH] schedule changes on the helper methods --- .../src/containers/Rotations/ScheduleFinal.tsx | 4 ++-- .../containers/Rotations/ScheduleOverrides.tsx | 11 ++++++++--- .../UsersTimezones/UsersTimezones.tsx | 14 ++++++-------- .../src/models/schedule/schedule.helpers.ts | 18 +++++++----------- grafana-plugin/src/models/schedule/schedule.ts | 4 ++-- .../src/models/schedule/schedule.types.ts | 6 ++++++ grafana-plugin/src/pages/schedule/Schedule.tsx | 1 + 7 files changed, 32 insertions(+), 26 deletions(-) diff --git a/grafana-plugin/src/containers/Rotations/ScheduleFinal.tsx b/grafana-plugin/src/containers/Rotations/ScheduleFinal.tsx index f5ed2945..1fcb97ea 100644 --- a/grafana-plugin/src/containers/Rotations/ScheduleFinal.tsx +++ b/grafana-plugin/src/containers/Rotations/ScheduleFinal.tsx @@ -47,11 +47,11 @@ class ScheduleFinal extends Component 1; diff --git a/grafana-plugin/src/containers/Rotations/ScheduleOverrides.tsx b/grafana-plugin/src/containers/Rotations/ScheduleOverrides.tsx index 1fdc5121..8d051fb0 100644 --- a/grafana-plugin/src/containers/Rotations/ScheduleOverrides.tsx +++ b/grafana-plugin/src/containers/Rotations/ScheduleOverrides.tsx @@ -10,8 +10,13 @@ import TimelineMarks from 'components/TimelineMarks/TimelineMarks'; import Rotation from 'containers/Rotation/Rotation'; import { RotationCreateData } from 'containers/RotationForm/RotationForm.types'; import ScheduleOverrideForm from 'containers/RotationForm/ScheduleOverrideForm'; -import { getFromString, getOverrideColor, getShiftsFromStore } from 'models/schedule/schedule.helpers'; -import { Event, Schedule, Shift } from 'models/schedule/schedule.types'; +import { + getFromString, + getOverrideColor, + getOverridesFromStore, + getShiftsFromStore, +} from 'models/schedule/schedule.helpers'; +import { Event, Schedule, Shift, ShiftEvents } from 'models/schedule/schedule.types'; import { Timezone } from 'models/timezone/timezone.types'; import { WithStoreProps } from 'state/types'; import { withMobXProviderContext } from 'state/withStore'; @@ -49,7 +54,7 @@ class ScheduleOverrides extends Component; tz: Timezone; onCallNow: Array>; + scheduleId: Schedule['id']; onTzChange: (tz: Timezone) => void; } @@ -46,8 +45,7 @@ const jLimit = 24 / hoursToSplit; const UsersTimezones: FC = (props) => { const store = useStore(); - const { userIds, tz, onTzChange, onCallNow } = props; - const { scheduleId } = store.scheduleStore; + const { userIds, tz, onTzChange, onCallNow, scheduleId } = props; useEffect(() => { userIds.forEach((userId) => { @@ -313,13 +311,13 @@ const AvatarGroup = (props: AvatarGroupProps) => { }; function getColorSchemeMappingForUsers(store: RootStore, scheduleId: string): { [userId: string]: Set } { + const usersColorSchemeHash: { [userId: string]: Set } = {}; + const startMoment = getStartOfWeek(store.currentTimezone); - const shifts = getShiftsFromStore(store, scheduleId, startMoment, false); + const shifts = getShiftsFromStore(store, scheduleId, startMoment); const layers = getLayersFromStore(store, scheduleId, startMoment); - const overrides = getOverridesFromStore(store, scheduleId, startMoment, false); - - const usersColorSchemeHash: { [userId: string]: Set } = {}; + const overrides = getOverridesFromStore(store, scheduleId, startMoment); if (!shifts?.length || !layers?.length) { return usersColorSchemeHash; diff --git a/grafana-plugin/src/models/schedule/schedule.helpers.ts b/grafana-plugin/src/models/schedule/schedule.helpers.ts index c025e249..861d1735 100644 --- a/grafana-plugin/src/models/schedule/schedule.helpers.ts +++ b/grafana-plugin/src/models/schedule/schedule.helpers.ts @@ -1,7 +1,7 @@ import dayjs from 'dayjs'; import { RootStore } from 'state'; -import { Event, Layer, Schedule, ScheduleType, Shift } from './schedule.types'; +import { Event, Layer, Schedule, ScheduleType, Shift, ShiftEvents } from './schedule.types'; export const getFromString = (moment: dayjs.Dayjs) => { return moment.format('YYYY-MM-DD'); @@ -62,12 +62,10 @@ export const getShiftsFromStore = ( store: RootStore, scheduleId: Schedule['id'], startMoment: dayjs.Dayjs, - isOverridePreview: boolean, -): Array<{ shiftId: string; events: Event[], isPreview?: boolean; }> => { - const source = isOverridePreview ? store.scheduleStore.overridePreview : store.scheduleStore.finalPreview; - return source +): ShiftEvents[] => { + return store.scheduleStore.finalPreview ? store.scheduleStore.finalPreview - : (store.scheduleStore.events[scheduleId]?.[isOverridePreview ? 'override' : 'final']?.[getFromString(startMoment)] as any); + : (store.scheduleStore.events[scheduleId]?.['final']?.[getFromString(startMoment)] as any); }; export const getLayersFromStore = (store: RootStore, scheduleId: Schedule['id'], startMoment: dayjs.Dayjs): Layer[] => { @@ -80,7 +78,6 @@ export const getOverridesFromStore = ( store: RootStore, scheduleId: Schedule['id'], startMoment: dayjs.Dayjs, - isOverridePreview: boolean ): | Layer[] | { @@ -89,10 +86,9 @@ export const getOverridesFromStore = ( isPreview?: boolean; }[] => { - const source = isOverridePreview ? store.scheduleStore.overridePreview : store.scheduleStore.rotationPreview; - return source - ? store.scheduleStore.rotationPreview - : (store.scheduleStore.events[scheduleId]?.[isOverridePreview ? 'override' : 'rotation']?.[getFromString(startMoment)] as Layer[]); + return store.scheduleStore.overridePreview + ? store.scheduleStore.overridePreview + : (store.scheduleStore.events[scheduleId]?.['override']?.[getFromString(startMoment)] as Layer[]); }; export const splitToLayers = ( diff --git a/grafana-plugin/src/models/schedule/schedule.ts b/grafana-plugin/src/models/schedule/schedule.ts index cfd9a4ca..1df49c23 100644 --- a/grafana-plugin/src/models/schedule/schedule.ts +++ b/grafana-plugin/src/models/schedule/schedule.ts @@ -21,7 +21,7 @@ import { splitToLayers, splitToShiftsAndFillGaps, } from './schedule.helpers'; -import { Events, Rotation, RotationType, Schedule, ScheduleEvent, Shift, Event, Layer } from './schedule.types'; +import { Events, Rotation, RotationType, Schedule, ScheduleEvent, Shift, Event, Layer, ShiftEvents } from './schedule.types'; const DEFAULT_FORMAT = 'YYYY-MM-DDTHH:mm:ss'; @@ -54,7 +54,7 @@ export class ScheduleStore extends BaseStore { events: { [scheduleId: string]: { [type: string]: { - [startMoment: string]: Array<{ shiftId: string; events: Event[]; isPreview?: boolean }> | Layer[]; + [startMoment: string]: ShiftEvents[] | Layer[]; }; }; } = {}; diff --git a/grafana-plugin/src/models/schedule/schedule.types.ts b/grafana-plugin/src/models/schedule/schedule.types.ts index ff36079e..ffbec8dc 100644 --- a/grafana-plugin/src/models/schedule/schedule.types.ts +++ b/grafana-plugin/src/models/schedule/schedule.types.ts @@ -95,3 +95,9 @@ export interface Layer { priority: Shift['priority_level']; shifts: Array<{ shiftId: Shift['id']; isPreview?: boolean; events: Event[] }>; } + +export interface ShiftEvents { + shiftId: string; + events: Event[]; + isPreview?: boolean; +} diff --git a/grafana-plugin/src/pages/schedule/Schedule.tsx b/grafana-plugin/src/pages/schedule/Schedule.tsx index adf816bf..f2e6c2a3 100644 --- a/grafana-plugin/src/pages/schedule/Schedule.tsx +++ b/grafana-plugin/src/pages/schedule/Schedule.tsx @@ -141,6 +141,7 @@ class SchedulePage extends React.Component } tz={currentTimezone} onTzChange={this.handleTimezoneChange} + scheduleId={scheduleId} />