mirror of
https://github.com/lukevella/rallly.git
synced 2025-06-07 05:01:49 +02:00
⬆️ v3.0.0 (#704)
This commit is contained in:
parent
735056f25f
commit
c22b3abc4d
385 changed files with 19912 additions and 5250 deletions
47
apps/web/src/contexts/permissions.ts
Normal file
47
apps/web/src/contexts/permissions.ts
Normal file
|
@ -0,0 +1,47 @@
|
|||
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";
|
||||
|
||||
export const PermissionsContext = React.createContext<{
|
||||
userId: string | null;
|
||||
}>({
|
||||
userId: null,
|
||||
});
|
||||
|
||||
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 {
|
||||
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 ||
|
||||
participant.userId === context.userId)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
};
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue