|
1 | 1 | import { DateTime } from "luxon" |
2 | 2 |
|
3 | 3 | import cCalendarEventService from "../../services/ccalendarevent" |
4 | | -const { getCurrentTimezone } = useFormatDate() |
5 | | - |
6 | 4 | import { subscriptionVisibility, type } from "../../constants/entity/ccalendarevent" |
7 | 5 | import { useFormatDate } from "../formatDate" |
8 | 6 |
|
| 7 | +const { getCurrentTimezone } = useFormatDate() |
| 8 | + |
9 | 9 | export function useCalendarEvent() { |
10 | 10 | return { |
11 | 11 | findUserLink, |
@@ -80,25 +80,76 @@ function allowUnsubscribeToEvent(event, userId) { |
80 | 80 | return !!findUserLink(event, userId) |
81 | 81 | } |
82 | 82 |
|
| 83 | +function mapCalendarEvent(event) { |
| 84 | + const timezone = getCurrentTimezone() |
| 85 | + const start = DateTime.fromISO(event.startDate, { zone: "utc" }).setZone(timezone) |
| 86 | + const end = DateTime.fromISO(event.endDate, { zone: "utc" }).setZone(timezone) |
| 87 | + |
| 88 | + return { |
| 89 | + ...event, |
| 90 | + start: start.toString(), |
| 91 | + end: end.toString(), |
| 92 | + color: event.color || "#007BFF", |
| 93 | + } |
| 94 | +} |
| 95 | + |
83 | 96 | /** |
84 | 97 | * @param {Object} params |
85 | 98 | * @returns {Promise<Object[]>} |
86 | 99 | */ |
87 | 100 | async function requestCalendarEvents(params) { |
88 | 101 | const calendarEvents = await cCalendarEventService.findAll({ params }).then((response) => response.json()) |
89 | 102 |
|
90 | | - return calendarEvents["hydra:member"].map((event) => { |
91 | | - const timezone = getCurrentTimezone() |
92 | | - const start = DateTime.fromISO(event.startDate, { zone: "utc" }).setZone(timezone) |
93 | | - const end = DateTime.fromISO(event.endDate, { zone: "utc" }).setZone(timezone) |
| 103 | + return calendarEvents["hydra:member"].map(mapCalendarEvent) |
| 104 | +} |
94 | 105 |
|
95 | | - return { |
96 | | - ...event, |
97 | | - start: start.toString(), |
98 | | - end: end.toString(), |
99 | | - color: event.color || "#007BFF", |
100 | | - } |
| 106 | +function shouldLoadLearningCalendarEvents(commonParams) { |
| 107 | + if (!commonParams) { |
| 108 | + return true |
| 109 | + } |
| 110 | + |
| 111 | + if (commonParams.cid || commonParams.sid || commonParams.gid || commonParams.type === "global") { |
| 112 | + return false |
| 113 | + } |
| 114 | + |
| 115 | + return true |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * @param {Object} startDate |
| 120 | + * @param {Object} endDate |
| 121 | + * @param {Object} commonParams |
| 122 | + * @returns {Promise<Object[]>} |
| 123 | + */ |
| 124 | +async function requestLearningCalendarEvents(startDate, endDate, commonParams) { |
| 125 | + if (!shouldLoadLearningCalendarEvents(commonParams)) { |
| 126 | + return [] |
| 127 | + } |
| 128 | + |
| 129 | + const params = new URLSearchParams({ |
| 130 | + startDate: startDate.toISOString(), |
| 131 | + endDate: endDate.toISOString(), |
101 | 132 | }) |
| 133 | + |
| 134 | + try { |
| 135 | + const response = await fetch(`/plugin/LearningCalendar/my_events.php?${params.toString()}`, { |
| 136 | + credentials: "same-origin", |
| 137 | + headers: { |
| 138 | + Accept: "application/json", |
| 139 | + }, |
| 140 | + }) |
| 141 | + |
| 142 | + if (!response.ok) { |
| 143 | + return [] |
| 144 | + } |
| 145 | + |
| 146 | + const payload = await response.json() |
| 147 | + const events = Array.isArray(payload.events) ? payload.events : [] |
| 148 | + |
| 149 | + return events.map(mapCalendarEvent) |
| 150 | + } catch (error) { |
| 151 | + return [] |
| 152 | + } |
102 | 153 | } |
103 | 154 |
|
104 | 155 | /** |
@@ -126,17 +177,21 @@ async function getCalendarEvents(startDate, endDate, commonParams) { |
126 | 177 | "startDate[after]": startDate.toISOString(), |
127 | 178 | }) |
128 | 179 |
|
129 | | - const [endingEvents, currentEvents, startingEvents] = await Promise.all([ |
| 180 | + const learningCalendarEventsPromise = requestLearningCalendarEvents(startDate, endDate, commonParams) |
| 181 | + |
| 182 | + const [endingEvents, currentEvents, startingEvents, learningCalendarEvents] = await Promise.all([ |
130 | 183 | endingEventsPromise, |
131 | 184 | currentEventsPromise, |
132 | 185 | startingEventsPromise, |
| 186 | + learningCalendarEventsPromise, |
133 | 187 | ]) |
134 | 188 |
|
135 | 189 | const uniqueEventsMap = new Map() |
136 | 190 |
|
137 | 191 | endingEvents |
138 | 192 | .concat(startingEvents) |
139 | 193 | .concat(currentEvents) |
| 194 | + .concat(learningCalendarEvents) |
140 | 195 | .forEach((event) => uniqueEventsMap.set(event.id, event)) |
141 | 196 |
|
142 | 197 | return Array.from(uniqueEventsMap.values()) |
|
0 commit comments