Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make rotation start and rotation end take timezone into consideration #4481

Merged
merged 28 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b4c6fba
schedules time shift fix
teodosii Jun 6, 2024
e42bc86
lint
teodosii Jun 6, 2024
3575572
fix issue when in dst
teodosii Jun 10, 2024
2d2b081
removed unused
teodosii Jun 10, 2024
c150553
Merge branch 'dev' into rares/fix-schedule-shifting-time-in-rotation
teodosii Jun 10, 2024
821b3f5
force datepicker use current offset
teodosii Jun 10, 2024
a2c9726
added comment
teodosii Jun 10, 2024
9d2ff75
moved to util
teodosii Jun 10, 2024
bca5c44
schedule fix (wip)
teodosii Jun 11, 2024
12ae8be
force keep date in selected offset
teodosii Jun 11, 2024
ca32396
fix time shifting
teodosii Jun 12, 2024
add8633
time conversion
teodosii Jun 12, 2024
c6642fc
lint
teodosii Jun 12, 2024
6f464e6
Merge branch 'dev' into rares/fix-schedule-shifting-time-in-rotation
teodosii Jun 12, 2024
80831ec
unused
teodosii Jun 12, 2024
aba0f88
improvements
teodosii Jun 12, 2024
05599b8
more tweaks to date offsetting
teodosii Jun 13, 2024
199aa5c
cleanup
teodosii Jun 14, 2024
d3f4647
Merge branch 'dev' into rares/fix-schedule-shifting-time-in-rotation
teodosii Jun 14, 2024
1131300
cleanup
teodosii Jun 14, 2024
ef0b934
lint
teodosii Jun 14, 2024
b55c5cc
cleanup
teodosii Jun 14, 2024
2c484a9
revert
teodosii Jun 14, 2024
66524e7
simplify logic for rotation change on new rotations
teodosii Jun 14, 2024
6f151c8
ci
teodosii Jun 14, 2024
ad05205
fixed calendar period shift when changing offset
teodosii Jun 14, 2024
66b4af6
Merge branch 'dev' into rares/fix-schedule-shifting-time-in-rotation
teodosii Jun 17, 2024
893f1b9
tweak
teodosii Jun 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 26 additions & 29 deletions grafana-plugin/src/containers/RotationForm/RotationForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useCallback, useEffect, useMemo, useState } from 'react';

import {
Alert,
Button,
Field,
HorizontalGroup,
Expand Down Expand Up @@ -330,28 +331,26 @@ export const RotationForm = observer((props: RotationFormProps) => {
}
};

const handleRotationStartChange = useCallback(
(value) => {
setRotationStart(value);
setShiftStart(value);
if (showActiveOnSelectedPartOfDay) {
setShiftEnd(
dayJSAddWithDSTFixed({
baseDate: value,
addParams: [activePeriod, 'seconds'],
})
);
} else {
setShiftEnd(
dayJSAddWithDSTFixed({
baseDate: value,
addParams: [repeatEveryValue, repeatEveryPeriodToUnitName[repeatEveryPeriod]],
})
);
}
},
[showActiveOnSelectedPartOfDay, activePeriod, repeatEveryPeriod, repeatEveryValue]
);
const handleRotationStartChange = (value: dayjs.Dayjs) => {
setRotationStart(value);
setShiftStart(value);

if (showActiveOnSelectedPartOfDay) {
setShiftEnd(
dayJSAddWithDSTFixed({
baseDate: value,
addParams: [activePeriod, 'seconds'],
})
);
} else {
setShiftEnd(
dayJSAddWithDSTFixed({
baseDate: value,
addParams: [repeatEveryValue, repeatEveryPeriodToUnitName[repeatEveryPeriod]],
})
);
}
};

const handleActivePeriodChange = useCallback(
(value) => {
Expand Down Expand Up @@ -435,9 +434,10 @@ export const RotationForm = observer((props: RotationFormProps) => {
useEffect(() => {
if (shift) {
setRotationName(getShiftName(shift));
const shiftStart = getDateTime(shift.shift_start);
const shiftStart = getDateTime(shift.shift_start).utcOffset(store.timezoneStore.selectedTimezoneOffset);
teodosii marked this conversation as resolved.
Show resolved Hide resolved
// use shiftStart as rotationStart for existing shifts
// (original rotationStart defaulted to the shift creation timestamp)

setRotationStart(shiftStart);
setRotationEnd(shift.until ? getDateTime(shift.until) : getDateTime(shift.shift_start).add(1, 'month'));
setShiftStart(shiftStart);
Expand Down Expand Up @@ -561,14 +561,11 @@ export const RotationForm = observer((props: RotationFormProps) => {
</Block>
)}
{!hasUpdatedShift && ended && (
<Block bordered className={cx('updated-shift-info')}>
<div className={cx('updated-shift-info')}>
<VerticalGroup>
<HorizontalGroup>
<Icon name="info-circle" size="md"></Icon>
<Text>This rotation is over</Text>
</HorizontalGroup>
<Alert severity="info" title={(<Text>This rotation is over</Text>) as unknown as string} />
</VerticalGroup>
</Block>
</div>
)}
<div className={cx('two-fields')}>
<Field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import dayjs from 'dayjs';
import { observer } from 'mobx-react';

import { Text } from 'components/Text/Text';
import { getDateForDatePicker } from 'containers/RotationForm/RotationForm.helpers';
import { useStore } from 'state/useStore';

import styles from 'containers/RotationForm/RotationForm.module.css';
Expand All @@ -27,30 +26,32 @@ interface DateTimePickerProps {
export const DateTimePicker = observer(
({ value: propValue, onChange, disabled, onFocus, onBlur, error }: DateTimePickerProps) => {
const styles = useStyles2(getStyles);
const {
timezoneStore: { getDateInSelectedTimezone },
} = useStore();
const { timezoneStore } = useStore();
const { getDateInSelectedTimezone } = timezoneStore;

const valueInSelectedTimezone = getDateInSelectedTimezone(propValue);
const valueAsDate = valueInSelectedTimezone.toDate();

const handleDateChange = (newDate: Date) => {
const localMoment = getDateInSelectedTimezone(dayjs(newDate));
const newValue = localMoment
.set('year', newDate.getFullYear())
.set('month', newDate.getMonth())
.set('date', newDate.getDate())
.set('hour', valueAsDate.getHours())
.set('minute', valueAsDate.getMinutes())
.set('second', valueAsDate.getSeconds());

onChange(newValue);
const dateInDayJS = dayjs(newDate);

// newDate will always point to a new day in the calendar at 00:00 local timezone
// We need to clone the date and apply only the new changes to it (year/month/date);
// Because we're only altering the date and not the time of it

const newDateTime = propValue
.clone()
.set('year', dateInDayJS.year())
.set('month', dateInDayJS.month())
.set('date', dateInDayJS.date());

onChange(newDateTime);
};
const handleTimeChange = (newMoment: DateTime) => {
const selectedHour = newMoment.hour();
const selectedMinute = newMoment.minute();
const newValue = valueInSelectedTimezone.set('hour', selectedHour).set('minute', selectedMinute);

onChange(newValue);
const handleTimeChange = (timeMoment: DateTime) => {
// Same as above, clone the date and only alter hour and minute from timeMoment
const newDateTime = propValue.clone().set('hour', timeMoment.hour()).set('minute', timeMoment.minute());

onChange(newDateTime);
};

const getTimeValueInSelectedTimezone = () => {
Expand All @@ -73,7 +74,7 @@ export const DateTimePicker = observer(
<DatePickerWithInput
open
disabled={disabled}
value={getDateForDatePicker(valueInSelectedTimezone)}
teodosii marked this conversation as resolved.
Show resolved Hide resolved
value={valueInSelectedTimezone.toDate()}
onChange={handleDateChange}
/>
</div>
Expand Down
Loading