mirror of
https://github.com/lukevella/rallly.git
synced 2025-06-07 05:01:49 +02:00
⚡️ Prefetch queries with trpc (#1454)
This commit is contained in:
parent
40df1ff9da
commit
82ebcd8752
13 changed files with 196 additions and 125 deletions
59
apps/web/src/contexts/permissions.tsx
Normal file
59
apps/web/src/contexts/permissions.tsx
Normal file
|
@ -0,0 +1,59 @@
|
|||
"use client";
|
||||
import React from "react";
|
||||
|
||||
import { useParticipants } from "@/components/participants-provider";
|
||||
import { useUser } from "@/components/user-provider";
|
||||
import { usePoll } from "@/contexts/poll";
|
||||
import { useRole } from "@/contexts/role";
|
||||
|
||||
const PermissionsContext = React.createContext<{
|
||||
userId: string | null;
|
||||
}>({
|
||||
userId: null,
|
||||
});
|
||||
|
||||
export const PermissionProvider = ({
|
||||
children,
|
||||
userId,
|
||||
}: React.PropsWithChildren<{ userId: string | null }>) => {
|
||||
return (
|
||||
<PermissionsContext.Provider value={{ userId }}>
|
||||
{children}
|
||||
</PermissionsContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const usePermissions = () => {
|
||||
const poll = usePoll();
|
||||
const context = React.useContext(PermissionsContext);
|
||||
const { user } = useUser();
|
||||
const role = useRole();
|
||||
const { participants } = useParticipants();
|
||||
const isClosed = poll.closed === true || poll.event !== null;
|
||||
return {
|
||||
isUser: (userId: string) => userId === user.id || userId === context.userId,
|
||||
canAddNewParticipant: !isClosed,
|
||||
canEditParticipant: (participantId: string) => {
|
||||
if (isClosed) {
|
||||
return false;
|
||||
}
|
||||
if (role === "admin" && user.id === poll.userId) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const participant = participants.find(
|
||||
(participant) => participant.id === participantId,
|
||||
);
|
||||
|
||||
if (
|
||||
participant &&
|
||||
(participant.userId === user.id ||
|
||||
(context.userId && participant.userId === context.userId))
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
};
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue