Skip to content

Commit

Permalink
feat(section list actions): disable "Delete" action without delete auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed Mar 18, 2024
1 parent 429c0cb commit 824d1cb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { BaseListModel } from '../../../lib'
import { canEditModel } from '../../../lib/models/access'
import { canEditModel, canDeleteModel } from '../../../lib/models/access'
import { ListActions, ActionEdit, ActionMore } from './SectionListActions'

type DefaultListActionProps = {
Expand All @@ -16,12 +16,15 @@ export const DefaultListActions = ({
onOpenSharingClick,
onDeleteClick,
}: DefaultListActionProps) => {
const deleteAccess = canDeleteModel(model)
const editAccess = canEditModel(model)

return (
<ListActions>
<ActionEdit modelId={model.id} />
<ActionMore
modelId={model.id}
deleteAccess={deleteAccess}
editAccess={editAccess}
onShowDetailsClick={() => onShowDetailsClick(model)}
onOpenSharingClick={() => onOpenSharingClick(model.id)}
Expand Down
30 changes: 19 additions & 11 deletions src/components/sectionList/listActions/SectionListActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ export const ActionEdit = ({ modelId }: { modelId: string }) => {

type ActionMoreProps = {
modelId: string
deleteAccess: boolean
editAccess: boolean
onShowDetailsClick: () => void
onOpenSharingClick: () => void
onDeleteClick: () => void
}
export const ActionMore = ({
modelId,
deleteAccess,
editAccess,
onOpenSharingClick,
onShowDetailsClick,
Expand Down Expand Up @@ -104,17 +106,23 @@ export const ActionMore = ({
></MenuItem>
</TooltipWrapper>

<MenuItem
dense
label={i18n.t('Delete')}
icon={<IconDelete16 />}
onClick={() => {
onDeleteClick()
setOpen(false)
}}
target="_blank"
href={href}
/>
<TooltipWrapper
condition={!deleteAccess}
content={TOOLTIPS.noEditAccess}
>
<MenuItem
dense
disabled={!deleteAccess}
label={i18n.t('Delete')}
icon={<IconDelete16 />}
onClick={() => {
onDeleteClick()
setOpen(false)
}}
target="_blank"
href={href}
/>
</TooltipWrapper>
</FlyoutMenu>
</Popover>
)}
Expand Down
1 change: 1 addition & 0 deletions src/lib/constants/tooltips.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import i18n from '@dhis2/d2-i18n'

export const TOOLTIPS = {
noEditAccess: i18n.t('You do not have access to edit this item.'),
noDeleteAccess: i18n.t('You do not have access to delete this item.'),
}
4 changes: 4 additions & 0 deletions src/lib/models/access.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { Access, BaseIdentifiableObject } from '../../types/generated'

export const hasWriteAccess = (access: Partial<Access>) => !!access.write
export const hasDeleteAccess = (access: Partial<Access>) => !!access.delete

export const canEditModel = (model: Pick<BaseIdentifiableObject, 'access'>) =>
hasWriteAccess(model.access)

export const canDeleteModel = (model: Pick<BaseIdentifiableObject, 'access'>) =>
hasDeleteAccess(model.access)

export type ParsedAccessPart = {
read: boolean
write: boolean
Expand Down

0 comments on commit 824d1cb

Please sign in to comment.