Merge with develop
This commit is contained in:
23
src/hooks/useEntities.tsx
Normal file
23
src/hooks/useEntities.tsx
Normal file
@@ -0,0 +1,23 @@
|
||||
import { EntityWithRoles } from "@/interfaces/entity";
|
||||
import { Discount } from "@/interfaces/paypal";
|
||||
import { Code, Group, User } from "@/interfaces/user";
|
||||
import axios from "axios";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export default function useEntities() {
|
||||
const [entities, setEntities] = useState<EntityWithRoles[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [isError, setIsError] = useState(false);
|
||||
|
||||
const getData = () => {
|
||||
setIsLoading(true);
|
||||
axios
|
||||
.get<EntityWithRoles[]>("/api/entities?showRoles=true")
|
||||
.then((response) => setEntities(response.data))
|
||||
.finally(() => setIsLoading(false));
|
||||
};
|
||||
|
||||
useEffect(getData, []);
|
||||
|
||||
return { entities, isLoading, isError, reload: getData };
|
||||
}
|
||||
23
src/hooks/useEntitiesGroups.tsx
Normal file
23
src/hooks/useEntitiesGroups.tsx
Normal file
@@ -0,0 +1,23 @@
|
||||
import { EntityWithRoles, WithEntity, WithLabeledEntities } from "@/interfaces/entity";
|
||||
import { Discount } from "@/interfaces/paypal";
|
||||
import { Code, Group, Type, User } from "@/interfaces/user";
|
||||
import axios from "axios";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export default function useEntitiesGroups() {
|
||||
const [groups, setGroups] = useState<WithEntity<Group>[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [isError, setIsError] = useState(false);
|
||||
|
||||
const getData = () => {
|
||||
setIsLoading(true);
|
||||
axios
|
||||
.get<WithEntity<Group>[]>(`/api/entities/groups`)
|
||||
.then((response) => setGroups(response.data))
|
||||
.finally(() => setIsLoading(false));
|
||||
};
|
||||
|
||||
useEffect(getData, []);
|
||||
|
||||
return { groups, isLoading, isError, reload: getData };
|
||||
}
|
||||
23
src/hooks/useEntitiesUsers.tsx
Normal file
23
src/hooks/useEntitiesUsers.tsx
Normal file
@@ -0,0 +1,23 @@
|
||||
import { EntityWithRoles, WithLabeledEntities } from "@/interfaces/entity";
|
||||
import { Discount } from "@/interfaces/paypal";
|
||||
import { Code, Group, Type, User } from "@/interfaces/user";
|
||||
import axios from "axios";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export default function useEntitiesUsers(type?: Type) {
|
||||
const [users, setUsers] = useState<WithLabeledEntities<User>[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [isError, setIsError] = useState(false);
|
||||
|
||||
const getData = () => {
|
||||
setIsLoading(true);
|
||||
axios
|
||||
.get<WithLabeledEntities<User>[]>(`/api/entities/users${type ? "?type=" + type : ""}`)
|
||||
.then((response) => setUsers(response.data))
|
||||
.finally(() => setIsLoading(false));
|
||||
};
|
||||
|
||||
useEffect(getData, [type]);
|
||||
|
||||
return { users, isLoading, isError, reload: getData };
|
||||
}
|
||||
24
src/hooks/useEntityPermissions.tsx
Normal file
24
src/hooks/useEntityPermissions.tsx
Normal file
@@ -0,0 +1,24 @@
|
||||
import { EntityWithRoles } from "@/interfaces/entity";
|
||||
import { User } from "@/interfaces/user";
|
||||
import { RolePermission } from "@/resources/entityPermissions";
|
||||
import { mapBy } from "@/utils";
|
||||
import { doesEntityAllow, findAllowedEntities, findAllowedEntitiesSomePermissions } from "@/utils/permissions";
|
||||
import { isAdmin } from "@/utils/users";
|
||||
import { useMemo, useState } from "react";
|
||||
|
||||
export const useAllowedEntities = (user: User, entities: EntityWithRoles[], permission: RolePermission) => {
|
||||
const allowedEntityIds = useMemo(() => findAllowedEntities(user, entities, permission), [user, entities, permission])
|
||||
return allowedEntityIds
|
||||
}
|
||||
|
||||
export const useAllowedEntitiesSomePermissions = (user: User, entities: EntityWithRoles[], permissions: RolePermission[]) => {
|
||||
const allowedEntityIds = useMemo(() => findAllowedEntitiesSomePermissions(user, entities, permissions), [user, entities, permissions])
|
||||
return allowedEntityIds
|
||||
}
|
||||
|
||||
export const useEntityPermission = (user: User, entity?: EntityWithRoles, permission?: RolePermission) => {
|
||||
if (isAdmin(user)) return true
|
||||
if (!entity || !permission) return false
|
||||
|
||||
return doesEntityAllow(user, entity, permission)
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import {search} from "@/utils/search";
|
||||
export function useListSearch<T>(fields: string[][], rows: T[]) {
|
||||
const [text, setText] = useState("");
|
||||
|
||||
const renderSearch = () => <Input label="Search" type="text" name="search" onChange={setText} placeholder="Enter search text" value={text} />;
|
||||
const renderSearch = () => <Input type="text" name="search" onChange={setText} placeholder="Enter search text" value={text} />;
|
||||
|
||||
const updatedRows = useMemo(() => {
|
||||
if (text.length > 0) return search(text, fields, rows);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import Button from "@/components/Low/Button";
|
||||
import {useMemo, useState} from "react";
|
||||
import {BiChevronLeft} from "react-icons/bi";
|
||||
import {BsChevronDoubleLeft, BsChevronDoubleRight, BsChevronLeft, BsChevronRight} from "react-icons/bs";
|
||||
|
||||
export default function usePagination<T>(list: T[], size = 25) {
|
||||
const [page, setPage] = useState(0);
|
||||
@@ -24,5 +26,35 @@ export default function usePagination<T>(list: T[], size = 25) {
|
||||
</div>
|
||||
);
|
||||
|
||||
return {page, items, setPage, render};
|
||||
const renderMinimal = () => (
|
||||
<div className="flex gap-4 items-center">
|
||||
<div className="flex gap-2 items-center">
|
||||
<button disabled={page === 0} onClick={() => setPage(0)} className="disabled:opacity-60 disabled:cursor-not-allowed">
|
||||
<BsChevronDoubleLeft />
|
||||
</button>
|
||||
<button disabled={page === 0} onClick={() => setPage((prev) => prev - 1)} className="disabled:opacity-60 disabled:cursor-not-allowed">
|
||||
<BsChevronLeft />
|
||||
</button>
|
||||
</div>
|
||||
<span className="opacity-80 w-32 text-center">
|
||||
{page * size + 1} - {(page + 1) * size > list.length ? list.length : (page + 1) * size} / {list.length}
|
||||
</span>
|
||||
<div className="flex gap-2 items-center">
|
||||
<button
|
||||
disabled={(page + 1) * size >= list.length}
|
||||
onClick={() => setPage((prev) => prev + 1)}
|
||||
className="disabled:opacity-60 disabled:cursor-not-allowed">
|
||||
<BsChevronRight />
|
||||
</button>
|
||||
<button
|
||||
disabled={(page + 1) * size >= list.length}
|
||||
onClick={() => setPage(Math.floor(list.length / size))}
|
||||
className="disabled:opacity-60 disabled:cursor-not-allowed">
|
||||
<BsChevronDoubleRight />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
return {page, items, setPage, render, renderMinimal};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user