ik hou van kaas
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import useSWR from "swr"
|
||||
import * as api from "./api"
|
||||
import type { CalendarEvent, SignupStatus } from "./types"
|
||||
|
||||
export function useRooms() {
|
||||
const { data, error, isLoading, mutate } = useSWR("rooms", () => api.getRooms())
|
||||
@@ -34,3 +35,31 @@ export function useRoomPermissions(roomId: number | null) {
|
||||
)
|
||||
return { permissions: data, error, isLoading, mutate }
|
||||
}
|
||||
|
||||
// Bulk-fetch the current user's own RSVP status for every visible proposal
|
||||
// event, so the week view can color-code events the user has declined.
|
||||
// Only proposal-type events carry RSVPs, so we skip busy/private blocks.
|
||||
export function useMyResponses(events: CalendarEvent[], currentUserId: number | null) {
|
||||
const proposalIds = events
|
||||
.filter((e) => e.type === "proposal")
|
||||
.map((e) => e.id)
|
||||
.sort((a, b) => a - b)
|
||||
|
||||
const key =
|
||||
currentUserId !== null && proposalIds.length > 0
|
||||
? ["my-responses", currentUserId, proposalIds.join(",")]
|
||||
: null
|
||||
|
||||
const { data, isLoading } = useSWR(key, async () => {
|
||||
const entries = await Promise.all(
|
||||
proposalIds.map(async (id) => {
|
||||
const responses = await api.getEventResponses(id)
|
||||
const mine = responses.find((r) => r.user_id === currentUserId)?.status
|
||||
return [id, mine] as [number, SignupStatus | undefined]
|
||||
}),
|
||||
)
|
||||
return Object.fromEntries(entries) as Record<number, SignupStatus | undefined>
|
||||
})
|
||||
|
||||
return { myResponses: data ?? {}, isLoading }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user