import { Switch } from 'ui' import { ALL_PRIVILEGE_TYPES, COLUMN_PRIVILEGE_TYPES, TABLE_PRIVILEGE_TYPES, } from './Privileges.constants' import { usePrivilegesState } from './Privileges.utils' import Table from '@/components/to-be-cleaned/Table' import type { ColumnPrivilege } from '@/data/privileges/column-privileges-query' export interface PrivilegesTableProps extends Pick< ReturnType, 'tableCheckedStates' | 'columnCheckedStates' | 'toggleTablePrivilege' | 'toggleColumnPrivilege' > { columnPrivileges: ColumnPrivilege[] disabled: boolean isApplyingChanges?: boolean } const PrivilegesTable = ({ columnPrivileges, tableCheckedStates, columnCheckedStates, toggleTablePrivilege, toggleColumnPrivilege, disabled, isApplyingChanges = false, }: PrivilegesTableProps) => { const handleClickColumnName = (columnId: string) => { const hasAllPrivileges = COLUMN_PRIVILEGE_TYPES.every( (privilege) => columnCheckedStates[columnId][privilege] ) const privilegesToToggle = COLUMN_PRIVILEGE_TYPES.filter((privilege) => hasAllPrivileges ? columnCheckedStates[columnId][privilege] : !columnCheckedStates[columnId][privilege] ) privilegesToToggle.forEach((privilege) => { toggleColumnPrivilege(columnId, privilege) }) } return ( Column, ...ALL_PRIVILEGE_TYPES.map((privilege) => { const checked = tableCheckedStates[privilege] return (
{privilege.charAt(0) + privilege.slice(1).toLowerCase()} { toggleTablePrivilege(privilege) }} disabled={disabled || isApplyingChanges} />
) }), ]} body={columnPrivileges.map((column) => ( {COLUMN_PRIVILEGE_TYPES.map((privilege) => { const checked = columnCheckedStates[column.column_id][privilege] return ( {COLUMN_PRIVILEGE_TYPES.includes(privilege as any) && (
{ toggleColumnPrivilege(column.column_id, privilege) }} disabled={disabled || isApplyingChanges} />
)}
) })} {TABLE_PRIVILEGE_TYPES.map((privilege) => { return ( N/A ) })}
))} /> ) } export default PrivilegesTable