|
1 | 1 | import FullCalendar from "@fullcalendar/react";
|
2 |
| -import interactionPlugin, { EventResizeStopArg } from "@fullcalendar/interaction"; |
| 2 | +import interactionPlugin from "@fullcalendar/interaction"; |
3 | 3 | import timeGridPlugin from "@fullcalendar/timegrid";
|
4 |
| -import { EventDropArg, EventInput } from "@fullcalendar/core"; |
| 4 | +import { EventChangeArg, EventInput } from "@fullcalendar/core"; |
5 | 5 | import { formatDate, getDateFromTimeAndWeekday, getTime, isSame, Modal, useDisclosure, useMantineTheme } from "@quassel/ui";
|
6 | 6 | import { QuestionnaireEntry } from "./QuestionnaireEntry";
|
7 | 7 | import { components } from "../../../api.gen";
|
8 | 8 | import { EntityForm, EntryFormValues } from "./EntryForm";
|
9 |
| -import { useState } from "react"; |
| 9 | +import { useEffect, useState } from "react"; |
10 | 10 | import { i18n } from "../../../stores/i18n";
|
11 | 11 | import { useStore } from "@nanostores/react";
|
12 | 12 |
|
@@ -65,17 +65,27 @@ export function EntryCalendar({
|
65 | 65 | const [entryUpdatingId, setEntryUpdadingId] = useState<number>();
|
66 | 66 | const [entryDraft, setEntryDraft] = useState<Partial<EntryFormValues>>();
|
67 | 67 |
|
68 |
| - const events: ExtendedEvent[] = |
69 |
| - entries.map(({ startedAt, endedAt, weekday, carer, entryLanguages, id }) => ({ |
70 |
| - id: id.toString(), |
71 |
| - start: getDateFromTimeAndWeekday(startedAt, weekday), |
72 |
| - end: getDateFromTimeAndWeekday(endedAt, weekday), |
73 |
| - title: carer.name, |
74 |
| - extendedProps: { entryLanguages }, |
75 |
| - backgroundColor: theme.colors[theme.primaryColor][4], |
76 |
| - })) ?? []; |
77 |
| - |
78 |
| - const handleMove = ({ event: { id, start, end } }: EventDropArg | EventResizeStopArg) => { |
| 68 | + const [events, setEvents] = useState<ExtendedEvent[]>([]); |
| 69 | + |
| 70 | + useEffect(() => { |
| 71 | + if (entries) { |
| 72 | + setEvents( |
| 73 | + entries.map(({ startedAt, endedAt, weekday, carer, entryLanguages, id }) => ({ |
| 74 | + id: id.toString(), |
| 75 | + start: getDateFromTimeAndWeekday(startedAt, weekday), |
| 76 | + end: getDateFromTimeAndWeekday(endedAt, weekday), |
| 77 | + title: carer.name, |
| 78 | + extendedProps: { entryLanguages }, |
| 79 | + backgroundColor: theme.colors[theme.primaryColor][4], |
| 80 | + })) ?? [] |
| 81 | + ); |
| 82 | + } |
| 83 | + }, [entries]); |
| 84 | + |
| 85 | + const handleEventChange = ({ event }: EventChangeArg) => { |
| 86 | + const { id, start, end } = event; |
| 87 | + setEvents(events.map((e) => (e.id === id ? { ...e, start: start!, end: end! } : e))); |
| 88 | + |
79 | 89 | onUpdateEntry(parseInt(id), { startedAt: getTime(start!), endedAt: getTime(end!) }, start!.getDay());
|
80 | 90 | };
|
81 | 91 |
|
@@ -127,8 +137,7 @@ export function EntryCalendar({
|
127 | 137 | setEntryUpdadingId(id);
|
128 | 138 | open();
|
129 | 139 | }}
|
130 |
| - eventResize={handleMove} |
131 |
| - eventDrop={handleMove} |
| 140 | + eventChange={handleEventChange} |
132 | 141 | eventContent={({ event }) => <QuestionnaireEntry event={event} />}
|
133 | 142 | />
|
134 | 143 | </>
|
|
0 commit comments