2023-06-22 09:35:02 -05:00
|
|
|
import { Member } from "@/types/global";
|
|
|
|
import { useEffect, useState } from "react";
|
2024-07-30 13:57:09 -05:00
|
|
|
import { useCollections } from "./store/collections";
|
2024-07-30 22:19:29 -05:00
|
|
|
import { useUser } from "./store/users";
|
2023-06-20 22:40:45 -05:00
|
|
|
|
|
|
|
export default function usePermissions(collectionId: number) {
|
2024-07-30 22:19:29 -05:00
|
|
|
const { data: collections = [] } = useCollections();
|
2023-06-20 22:40:45 -05:00
|
|
|
|
2024-07-30 22:19:29 -05:00
|
|
|
const { data: user = [] } = useUser();
|
2023-06-20 22:40:45 -05:00
|
|
|
|
|
|
|
const [permissions, setPermissions] = useState<Member | true>();
|
|
|
|
useEffect(() => {
|
|
|
|
const collection = collections.find((e) => e.id === collectionId);
|
|
|
|
|
|
|
|
if (collection) {
|
|
|
|
let getPermission: Member | undefined = collection.members.find(
|
2024-07-30 22:19:29 -05:00
|
|
|
(e) => e.userId === user.id
|
2023-06-20 22:40:45 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
if (
|
|
|
|
getPermission?.canCreate === false &&
|
|
|
|
getPermission?.canUpdate === false &&
|
|
|
|
getPermission?.canDelete === false
|
|
|
|
)
|
|
|
|
getPermission = undefined;
|
|
|
|
|
2024-07-30 22:19:29 -05:00
|
|
|
setPermissions(user.id === collection.ownerId || getPermission);
|
2023-06-20 22:40:45 -05:00
|
|
|
}
|
2024-07-30 22:19:29 -05:00
|
|
|
}, [user, collections, collectionId]);
|
2023-06-20 22:40:45 -05:00
|
|
|
|
|
|
|
return permissions;
|
|
|
|
}
|