Skip to content

Commit

Permalink
feat: Add to registry the possibility to override the default actions…
Browse files Browse the repository at this point in the history
… in items list.
  • Loading branch information
rboixaderg committed Apr 25, 2024
1 parent 78149b0 commit 08d78fb
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.32.0
------
- feat: Add to registry the possibility to override the default actions in items list.

0.31.0
------
- chore: Improve typing in registry
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.31.0",
"version": "0.32.0",
"repository": {
"type": "git",
"url": "[email protected]:guillotinaweb/guillotina_react.git"
Expand Down
8 changes: 5 additions & 3 deletions src/guillo-gmi/components/panel/actions.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { useTraversal } from '../../contexts'
import { useIntl } from 'react-intl'
import { getActionsObject } from '../../lib/helpers'
import { Fragment } from 'react'

export function PanelActions() {
const traversal = useTraversal()
const intl = useIntl()
const ACTIONS_OBJECT = getActionsObject(intl)
const ACTIONS_OBJECT = traversal.registry.getActionsList(
traversal.context['@type'],
false
)

const hasPerm = (perms: string[]) => {
return perms.some((perm) => traversal.hasPerm(perm))
Expand All @@ -32,7 +34,7 @@ export function PanelActions() {
onAction(actionKeyTyped)
}}
>
{actionObject.text}
{intl.formatMessage(actionObject.text)}
</button>
)
}
Expand Down
14 changes: 9 additions & 5 deletions src/guillo-gmi/components/selected_items_actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Dropdown from './input/dropdown'
import { Checkbox } from './input/checkbox'
import { useTraversal } from '../contexts'
import { useIntl } from 'react-intl'
import { getActionsObject } from '../lib/helpers'
import { SearchItem } from '../types/guillotina'

const ItemsActionsCtx = createContext<{
Expand Down Expand Up @@ -32,9 +31,11 @@ export function ItemsActionsProvider({
items,
children,
}: PropsItemsActionsProvider) {
const intl = useIntl()
const actions = getActionsObject(intl, true)
const traversal = useTraversal()
const actions = traversal.registry.getActionsList(
traversal.context['@type'],
true
)
const [selected, setSelected] = useState<{
all: boolean
[key: string]: boolean
Expand Down Expand Up @@ -134,12 +135,15 @@ export function ItemCheckbox({ item, dataTest }: PropsItemCheckbox) {
*/
export function ItemsActionsDropdown() {
const intl = useIntl()
const ACTIONS_OBJECT = getActionsObject(intl, true)
const traversal = useTraversal()
const ACTIONS_OBJECT = traversal.registry.getActionsList(
traversal.context['@type'],
true
)
const { selected, onAction } = useItemsActions()
const disabled = Object.values(selected).every((v) => !v)
const options = Object.keys(ACTIONS_OBJECT).map((action) => ({
text: ACTIONS_OBJECT[action].text,
text: intl.formatMessage(ACTIONS_OBJECT[action].text),
value: action,
}))

Expand Down
42 changes: 42 additions & 0 deletions src/guillo-gmi/hooks/useRegistry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {
RegistrySchema,
} from '../types/guillotina'
import { buildQs, parser } from '../lib/search'
import { MessageDescriptor } from 'react-intl'
import { getActionsObject } from '../lib/helpers'

export interface RegistrySortValue {
direction: 'asc' | 'des'
Expand Down Expand Up @@ -87,6 +89,17 @@ export interface IRegistry {
defaultSortValue: {
[key: string]: RegistrySortValue
}
actionsList: {
[key: string]: (
multiple: boolean
) => {
[key: string]: {
text: MessageDescriptor
perms: string[]
action: string
}
}
}
}

const registry: IRegistry = {
Expand Down Expand Up @@ -149,6 +162,7 @@ const registry: IRegistry = {
},
parseSearchQueryParamFunction: {},
defaultSortValue: {},
actionsList: {},
}

export interface IManageRegistry {
Expand All @@ -173,6 +187,16 @@ export interface IManageRegistry {
fallback?: RegistrySortValue
) => RegistrySortValue
getSearchEngineQueryParamsFunction: (type: string) => string
getActionsList: (
type: string,
multiple: boolean
) => {
[key: string]: {
text: MessageDescriptor
perms: string[]
action: string
}
}
}

const getPathComponent = (
Expand Down Expand Up @@ -259,6 +283,23 @@ const getParsedSearchQueryParam = (query: string, type: string) => {
return parsedFunction(query, type)
}

const getActionsList = (
type: string,
multiple: boolean
): {
[key: string]: {
text: MessageDescriptor
perms: string[]
action: string
}
} => {
const funcActionsList = registry.actionsList[type]
if (funcActionsList) {
return funcActionsList(multiple)
}
return getActionsObject(multiple)
}

export const defaultComponent = (context: GuillotinaCommonObject) => {
return context.is_folderish ? FolderCtx : ItemCtx
}
Expand Down Expand Up @@ -292,6 +333,7 @@ export function useRegistry(data: Partial<IRegistry>): IManageRegistry {
getSchemas,
getView,
getSearchEngineQueryParamsFunction,
getActionsList,
}
}

Expand Down
13 changes: 6 additions & 7 deletions src/guillo-gmi/lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IntlShape } from 'react-intl'
import { MessageDescriptor } from 'react-intl'
import { genericMessages } from '../locales/generic_messages'
import { IndexSignature } from '../types/global'

Expand Down Expand Up @@ -59,27 +59,26 @@ export function sleep(ms: number): Promise<void> {
}

export const getActionsObject = (
intl: IntlShape,
multiple = false
multiple: boolean
): {
[key: string]: {
text: string
text: MessageDescriptor
perms: string[]
action: string
}
} => ({
DELETE: {
text: intl.formatMessage(genericMessages.delete),
text: genericMessages.delete,
perms: ['guillotina.DeleteContent'],
action: multiple ? 'removeItems' : 'removeItem',
},
MOVE: {
text: intl.formatMessage(genericMessages.move_to),
text: genericMessages.move_to,
perms: ['guillotina.MoveContent'],
action: multiple ? 'moveItems' : 'moveItem',
},
COPY: {
text: intl.formatMessage(genericMessages.copy_to),
text: genericMessages.copy_to,
perms: ['guillotina.DuplicateContent'],
action: multiple ? 'copyItems' : 'copyItem',
},
Expand Down

0 comments on commit 08d78fb

Please sign in to comment.