diff --git a/changelogs/unreleased/87876.json b/changelogs/unreleased/87876.json new file mode 100644 index 000000000..4a0d5cde3 --- /dev/null +++ b/changelogs/unreleased/87876.json @@ -0,0 +1,5 @@ +{ + "title": "Generic action: add possibility to use async functions", + "type": "feat", + "packages": "core" +} diff --git a/packages/core/src/components/organisms/HeaderOptionsMenu/HeaderOptionsMenu.js b/packages/core/src/components/organisms/HeaderOptionsMenu/HeaderOptionsMenu.js index eef2f95ae..adbdc4461 100644 --- a/packages/core/src/components/organisms/HeaderOptionsMenu/HeaderOptionsMenu.js +++ b/packages/core/src/components/organisms/HeaderOptionsMenu/HeaderOptionsMenu.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import React, {useCallback, useMemo} from 'react'; +import React, {useCallback, useEffect, useMemo, useState} from 'react'; import {Dimensions, StyleSheet, View} from 'react-native'; import {DropdownMenu, DropdownMenuItem} from '@axelor/aos-mobile-ui'; import {HeaderOptionMenuItem} from '../../molecules'; @@ -65,38 +65,49 @@ const HeaderOptionsMenu = ({ [collapseMenuItems], ); - const allActions = useMemo(() => { - let _genericActions = []; - - if (model && modelId) { - _genericActions = Object.entries(genericActions).map(([key, func]) => - func({model, modelId, options: options?.[key]}), - ); - } - - return [ + const [visibleGenericActions, setVisibleGenericActions] = useState([]); + + useEffect(() => { + const getVisibleGenericActions = async () => { + if (model && modelId) { + const _genericActions = await Promise.all( + Object.entries(genericActions).map( + async ([key, func]) => + await func({model, modelId, options: options?.[key]}), + ), + ); + setVisibleGenericActions(_genericActions); + } else { + setVisibleGenericActions([]); + } + }; + + getVisibleGenericActions(); + }, [genericActions, model, modelId, options]); + + const allActions = useMemo( + () => + [ + attachedFilesAction, + mailMessagesAction, + printAction, + barcodeAction, + jsonFieldsAction, + ...actions, + ...visibleGenericActions, + ] + .filter(_action => !_action.hideIf) + .sort((a, b) => a.order - b.order), + [ + actions, attachedFilesAction, - mailMessagesAction, - printAction, barcodeAction, jsonFieldsAction, - ...actions, - ..._genericActions, - ] - .filter(_action => !_action.hideIf) - .sort((a, b) => a.order - b.order); - }, [ - actions, - attachedFilesAction, - barcodeAction, - genericActions, - jsonFieldsAction, - mailMessagesAction, - model, - modelId, - options, - printAction, - ]); + mailMessagesAction, + printAction, + visibleGenericActions, + ], + ); const headerActions = useMemo( () => allActions.filter(_action => _action.showInHeader).slice(0, 2), diff --git a/packages/core/src/header/types.ts b/packages/core/src/header/types.ts index 457ea9b40..2a1722cf3 100644 --- a/packages/core/src/header/types.ts +++ b/packages/core/src/header/types.ts @@ -48,7 +48,7 @@ export type RegisterFunction = ({ model: string; modelId: number; options?: any; -}) => ActionType; +}) => ActionType | Promise; export interface ActionType { key: string;