Skip to content

Commit

Permalink
feat: add possibility to use async functions on generic action (#822)
Browse files Browse the repository at this point in the history
* RM#87876
  • Loading branch information
vhu-axelor authored Dec 4, 2024
1 parent bb29cd2 commit 0dfadd4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 deletions.
5 changes: 5 additions & 0 deletions changelogs/unreleased/87876.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"title": "Generic action: add possibility to use async functions",
"type": "feat",
"packages": "core"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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';
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/header/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export type RegisterFunction = ({
model: string;
modelId: number;
options?: any;
}) => ActionType;
}) => ActionType | Promise<ActionType>;

export interface ActionType {
key: string;
Expand Down

0 comments on commit 0dfadd4

Please sign in to comment.