Skip to content

Commit 0078ba8

Browse files
committed
Revert "Add hotkeys for unslecting and deleting timespans (#148)"
This reverts commit 6842b5c.
1 parent 4e90096 commit 0078ba8

File tree

3 files changed

+39
-122
lines changed

3 files changed

+39
-122
lines changed

ui/src/timespan/TimeSpan.tsx

+36-5
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ import Menu from '@material-ui/core/Menu';
1616
import MenuItem from '@material-ui/core/MenuItem';
1717
import {RemoveTimeSpan, RemoveTimeSpanVariables} from '../gql/__generated__/RemoveTimeSpan';
1818
import {useStateAndDelegateWithDelayOnChange} from '../utils/hooks';
19+
import {TimeSpans} from '../gql/__generated__/TimeSpans';
1920
import {isSameDate} from '../utils/time';
21+
import {Trackers} from '../gql/__generated__/Trackers';
2022
import {addTimeSpanToCache, removeFromTrackersCache} from '../gql/utils';
2123
import {StartTimer, StartTimerVariables} from '../gql/__generated__/StartTimer';
2224
import {RelativeTime, RelativeToNow} from '../common/RelativeTime';
2325
import ShowNotesIcon from '@material-ui/icons/KeyboardArrowDown';
2426
import HideNotesIcon from '@material-ui/icons/KeyboardArrowUp';
25-
import {removeTimeSpanOptions} from './mutations';
2627

2728
interface Range {
2829
from: moment.Moment;
@@ -81,10 +82,40 @@ export const TimeSpan: React.FC<TimeSpanProps> = React.memo(
8182
clearTimeout(note.current.handle);
8283
return updateTimeSpan({variables: {...variables, note: note.current.value}});
8384
};
84-
const [removeTimeSpan] = useMutation<RemoveTimeSpan, RemoveTimeSpanVariables>(
85-
gqlTimeSpan.RemoveTimeSpan,
86-
removeTimeSpanOptions
87-
);
85+
const [removeTimeSpan] = useMutation<RemoveTimeSpan, RemoveTimeSpanVariables>(gqlTimeSpan.RemoveTimeSpan, {
86+
update: (cache, {data}) => {
87+
let oldData: TimeSpans | null = null;
88+
try {
89+
oldData = cache.readQuery<TimeSpans>({query: gqlTimeSpan.TimeSpans});
90+
} catch (e) {}
91+
92+
const oldTrackers = cache.readQuery<Trackers>({query: gqlTimeSpan.Trackers});
93+
if (!data || !data.removeTimeSpan) {
94+
return;
95+
}
96+
const removedId = data.removeTimeSpan.id;
97+
if (oldTrackers) {
98+
cache.writeQuery<Trackers>({
99+
query: gqlTimeSpan.Trackers,
100+
data: {
101+
timers: (oldTrackers.timers || []).filter((tracker) => tracker.id !== removedId),
102+
},
103+
});
104+
}
105+
if (oldData) {
106+
cache.writeQuery<TimeSpans>({
107+
query: gqlTimeSpan.TimeSpans,
108+
data: {
109+
timeSpans: {
110+
__typename: 'PagedTimeSpans',
111+
timeSpans: oldData.timeSpans.timeSpans.filter((ts) => ts.id !== removedId),
112+
cursor: oldData.timeSpans.cursor,
113+
},
114+
},
115+
});
116+
}
117+
},
118+
});
88119

89120
const updateNote = (newValue: string) => {
90121
window.clearTimeout(note.current.handle);

ui/src/timespan/calendar/CalendarPage.tsx

+3-77
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ import {timeRunningCalendar} from '../timeutils';
3838
import {stripTypename} from '../../utils/strip';
3939
import {TimeSpansInRange, TimeSpansInRangeVariables} from '../../gql/__generated__/TimeSpansInRange';
4040
import {ExtendedEventSourceInput} from '@fullcalendar/core/structs/event-source';
41-
import {RemoveTimeSpan, RemoveTimeSpanVariables} from '../../gql/__generated__/RemoveTimeSpan';
42-
import {removeTimeSpanOptions} from '../mutations';
4341

4442
const toMoment = (date: Date): moment.Moment => {
4543
return moment(date).tz('utc');
@@ -74,10 +72,6 @@ export const CalendarPage: React.FC = () => {
7472
refetchQueries: [{query: gqlTimeSpan.Trackers}],
7573
});
7674
const [updateTimeSpanMutation] = useMutation<UpdateTimeSpan, UpdateTimeSpanVariables>(gqlTimeSpan.UpdateTimeSpan);
77-
const [removeTimeSpan] = useMutation<RemoveTimeSpan, RemoveTimeSpanVariables>(
78-
gqlTimeSpan.RemoveTimeSpan,
79-
removeTimeSpanOptions
80-
);
8175
const [currentDate, setCurrentDate] = React.useState(moment());
8276
const [stopTimer] = useMutation<StopTimer, StopTimerVariables>(gqlTimeSpan.StopTimer, {
8377
update: (cache, {data}) => {
@@ -99,34 +93,18 @@ export const CalendarPage: React.FC = () => {
9993
window.__TRAGGO_CALENDAR = {};
10094
return () => (window.__TRAGGO_CALENDAR = undefined);
10195
});
102-
103-
const fullCalendarRef = React.useRef<FullCalendar | null>(null);
104-
const unselectCalenderItem = () => {
105-
const instance = fullCalendarRef.current;
106-
if (instance) {
107-
const calendar = instance.getApi();
108-
if (calendar) {
109-
calendar.unselect();
110-
}
111-
}
112-
};
113-
11496
const [ignore, setIgnore] = React.useState<boolean>(false);
11597
const [selected, setSelected] = React.useState<{selected: HTMLElement | null; data: TimeSpans_timeSpans_timeSpans | null}>({
11698
selected: null,
11799
data: null,
118100
});
119-
120-
const [lastCreatedTimeSpanId, setLastCreatedTimeSpanId] = React.useState<number | null>(null);
121-
122101
const [addTimeSpan] = useMutation<AddTimeSpan, AddTimeSpanVariables>(gqlTimeSpan.AddTimeSpan, {
123102
update: (cache, {data}) => {
124103
if (!data || !data.createTimeSpan) {
125104
return;
126105
}
127106
addTimeSpanInRangeToCache(cache, data.createTimeSpan, timeSpansResult.variables);
128107
addTimeSpanToCache(cache, data.createTimeSpan);
129-
unselectCalenderItem();
130108
},
131109
});
132110

@@ -198,19 +176,15 @@ export const CalendarPage: React.FC = () => {
198176
},
199177
});
200178
};
201-
const onSelect: OptionsInput['select'] = async (data) => {
202-
const result = await addTimeSpan({
179+
const onSelect: OptionsInput['select'] = (data) => {
180+
addTimeSpan({
203181
variables: {
204182
start: moment(data.start).format(),
205183
end: moment(data.end).format(),
206184
tags: [],
207185
note: '',
208186
},
209187
});
210-
211-
if (result.data && result.data.createTimeSpan) {
212-
setLastCreatedTimeSpanId(result.data.createTimeSpan.id);
213-
}
214188
};
215189
const onClick: OptionsInput['eventClick'] = (data) => {
216190
data.jsEvent.preventDefault();
@@ -223,7 +197,6 @@ export const CalendarPage: React.FC = () => {
223197

224198
// tslint:disable-next-line:no-any
225199
setSelected({data: data.event.extendedProps.ts, selected: data.jsEvent.target as any});
226-
setLastCreatedTimeSpanId(null);
227200
};
228201
if (trackersResult.data && !(trackersResult.data.timers || []).length) {
229202
const startTimerEvent: ExtendedEventSourceInput = {
@@ -238,57 +211,10 @@ export const CalendarPage: React.FC = () => {
238211
values.push(startTimerEvent);
239212
}
240213

241-
React.useEffect(() => {
242-
let deletingSelected = false;
243-
244-
const onKeyDown = (e: KeyboardEvent) => {
245-
if (e.key === 'Escape') {
246-
e.preventDefault();
247-
setSelected({selected: null, data: null});
248-
unselectCalenderItem();
249-
}
250-
251-
if (
252-
!deletingSelected &&
253-
lastCreatedTimeSpanId &&
254-
(e.key === 'Delete' || e.key === 'Backspace' || e.key === 'Escape')
255-
) {
256-
e.preventDefault();
257-
removeFromTimeSpanInRangeCache(apollo.cache, lastCreatedTimeSpanId, timeSpansResult.variables);
258-
removeTimeSpan({variables: {id: lastCreatedTimeSpanId}}).finally(() => {
259-
deletingSelected = false;
260-
});
261-
setSelected({selected: null, data: null});
262-
setLastCreatedTimeSpanId(null);
263-
unselectCalenderItem();
264-
return;
265-
}
266-
267-
if (!deletingSelected && selected.data && (e.key === 'Delete' || e.key === 'Backspace')) {
268-
e.preventDefault();
269-
setSelected({selected: null, data: selected.data});
270-
removeFromTimeSpanInRangeCache(apollo.cache, selected.data.id, timeSpansResult.variables);
271-
removeTimeSpan({variables: {id: selected.data.id}}).finally(() => {
272-
deletingSelected = false;
273-
});
274-
setSelected({selected: null, data: null});
275-
unselectCalenderItem();
276-
return;
277-
}
278-
};
279-
280-
document.addEventListener('keydown', onKeyDown);
281-
282-
return () => {
283-
document.removeEventListener('keydown', onKeyDown);
284-
};
285-
}, [selected, lastCreatedTimeSpanId, apollo.cache, removeTimeSpan, timeSpansResult.variables]);
286-
287214
return (
288215
<Paper style={{padding: 10, bottom: 10, top: 80, position: 'absolute'}} color="red">
289216
<FullCalendarStyling>
290217
<FullCalendar
291-
ref={fullCalendarRef}
292218
defaultView="timeGridWeek"
293219
rerenderDelay={30}
294220
datesRender={(x) => {
@@ -304,7 +230,7 @@ export const CalendarPage: React.FC = () => {
304230
events={values}
305231
allDaySlot={false}
306232
selectable={true}
307-
selectMirror={false}
233+
selectMirror={true}
308234
handleWindowResize={true}
309235
height={'parent'}
310236
selectMinDistance={20}

ui/src/timespan/mutations.ts

-40
This file was deleted.

0 commit comments

Comments
 (0)