Skip to content

Commit

Permalink
Create useAuthorization hook
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoPicolo committed Sep 11, 2022
1 parent fc5dd61 commit 9d8dd99
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 14 deletions.
3 changes: 0 additions & 3 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@ NEXT_PUBLIC_DETALHADOR_CHAMADOS_URL=http://localhost:5000/
NEXT_PUBLIC_GESTOR_DE_USUARIOS_URL=http://localhost:5000/
NEXT_PUBLIC_GERENCIADOR_DE_LOCALIDADES_URL=http://localhost:5000/
NEXTAUTH_URL=http://localhost:3000/api/auth
SECRET=schedula
NEXTAUTH_SECRET=schedula

2 changes: 0 additions & 2 deletions .env.production
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ NEXT_PUBLIC_GESTOR_DE_USUARIOS_URL=https://gestor.schedula.duckdns.org/
NEXT_PUBLIC_DETALHADOR_CHAMADOS_URL=https://detalhador.schedula.duckdns.org/
NEXT_PUBLIC_GERENCIADOR_DE_LOCALIDADES_URL=https://gerenciador.schedula.duckdns.org/
NEXTAUTH_URL=http://localhost:3000/api/auth
SECRET=schedula
NEXTAUTH_SECRET=schedula
18 changes: 15 additions & 3 deletions src/components/Items/CityItem/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useCallback } from "react"
import { ReactElement, useCallback } from "react"

import { DeleteButton } from "@components/ActionButtons/DeleteButton"
import { EditButton } from "@components/ActionButtons/EditButton"
import { Item } from "@components/ListItem"
import { useAuthorization } from "@hooks/useAuthorization"
import { deleteCity } from "@services/Cidades"
import { request } from "@services/request"

Expand All @@ -13,6 +14,9 @@ interface CityItemProps {
}

export const CityItem = ({ city, onEdit, onDelete }: CityItemProps) => {
const isEditAuthorized = useAuthorization(["manager"])
const isDeleteAuthorized = useAuthorization()

const handleDelete = useCallback(
async ({ id }: City) => {
const response = await request<null>(deleteCity(id))
Expand All @@ -25,8 +29,16 @@ export const CityItem = ({ city, onEdit, onDelete }: CityItemProps) => {
return (
<Item<City> title={city?.name} description="">
<Item.Actions item={city}>
<EditButton onClick={onEdit} label={city.name} />
<DeleteButton onClick={handleDelete} label={city.name} />
{
(isEditAuthorized && (
<EditButton onClick={onEdit} label={city.name} />
)) as ReactElement
}
{
(isDeleteAuthorized && (
<DeleteButton onClick={handleDelete} label={city.name} />
)) as ReactElement
}
</Item.Actions>
</Item>
)
Expand Down
16 changes: 14 additions & 2 deletions src/components/Items/UserItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Badge, HStack } from "@chakra-ui/react"
import { DeleteButton } from "@components/ActionButtons/DeleteButton"
import { EditButton } from "@components/ActionButtons/EditButton"
import { Item } from "@components/ListItem"
import { useAuthorization } from "@hooks/useAuthorization"
import { request } from "@services/request"
import { deleteUser } from "@services/Usuarios"

Expand All @@ -14,6 +15,9 @@ interface UserItemProps {
}

export const UserItem = ({ user, onEdit, onDelete }: UserItemProps) => {
const isEditAuthorized = useAuthorization(["manager"])
const isDeleteAuthorized = useAuthorization()

const handleDelete = useCallback(
async ({ username }: User) => {
const response = await request<null>(deleteUser(username))
Expand All @@ -36,8 +40,16 @@ export const UserItem = ({ user, onEdit, onDelete }: UserItemProps) => {
}
>
<Item.Actions item={user}>
<EditButton onClick={onEdit} label={user.name} />
<DeleteButton onClick={handleDelete} label={user.name} />
{
(isEditAuthorized && (
<EditButton onClick={onEdit} label={user.name} />
)) as ReactElement
}
{
(isDeleteAuthorized && (
<DeleteButton onClick={handleDelete} label={user.name} />
)) as ReactElement
}
</Item.Actions>
</Item>
)
Expand Down
12 changes: 8 additions & 4 deletions src/components/ListItem/ListItemActions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ export interface ActionsProps<Data> {
children:
| ReactElement<ActionButton<Data>>
| ReactElement<ActionButton<Data>>[]
| null
}

export const Actions = <Data,>({ children, item }: ActionsProps<Data>) => {
return (
<HStack spacing={4} role="menubar">
{Children.map(children, (child) =>
cloneElement(child, {
onClick: child?.props?.onClick?.bind?.(null, item)
})
{Children.map(
children,
(child) =>
child &&
cloneElement(child, {
onClick: child?.props?.onClick?.bind?.(null, item)
})
)}
</HStack>
)
Expand Down
14 changes: 14 additions & 0 deletions src/hooks/useAuthorization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useMemo } from "react"
import { useSession } from "next-auth/react"

export function useAuthorization(access?: Access[]): boolean {
const { data: session } = useSession()

const isAuthorized = useMemo(() => {
if (session?.user?.access === "admin") return true

return access?.includes(session?.user?.access) || false
}, [access, session])

return isAuthorized
}

0 comments on commit 9d8dd99

Please sign in to comment.