All files / app/src/entities/task/lib use-task-action-permissions.ts

100% Statements 20/20
33.33% Branches 1/3
100% Functions 1/1
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 321x             1x 2x 2x   2x 2x   2x   2x 2x   2x 2x 2x 2x   2x 2x 2x 2x 2x 2x 2x  
import { useAtomValue } from 'jotai'
 
import { currentUserRolesAtom } from '@/features/rolevik'
 
import { Task, TaskStatusEnum } from '@/entities/task'
import { viewerAtom } from '@/entities/viewer'
 
export const useTaskActionPermissions = (task: Task) => {
  const viewer = useAtomValue(viewerAtom)
  const currentRoles = useAtomValue(currentUserRolesAtom)
 
  const isViewerManager = currentRoles?.some(role => role?.role?.includes('role_manager'))
  const isCreatedByCurrentUser = viewer?.id === task.createdBy.id
 
  const isNotCompleted = task.status !== TaskStatusEnum.COMPLETED
 
  const isEditBtnManager = isNotCompleted
  const isEditBtnMol = isNotCompleted && isCreatedByCurrentUser
 
  const isStartBtn = !isViewerManager && task.status === TaskStatusEnum.NEW
  const isCompleteBtn = !isViewerManager && task.status === TaskStatusEnum.IN_WORK
  const isEditBtn = isViewerManager ? isEditBtnManager : isEditBtnMol
  const isDeleteBtn = isViewerManager || isCreatedByCurrentUser
 
  return {
    isStartBtn,
    isCompleteBtn,
    isEditBtn,
    isDeleteBtn,
  }
}