Skip to content

Commit

Permalink
feat: add Loader toast management (axelor#366)
Browse files Browse the repository at this point in the history
* RM#73988
  • Loading branch information
hel-axelor authored and vhu-axelor committed Jun 13, 2024
1 parent bd964b9 commit 7ca20f4
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/apps/stock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {Module} from '@axelor/aos-mobile-core';
import CustomerDeliveryScreens from './screens/customerDeliveries';
import InternalMovesScreens from './screens/internalMoves';
import InventoriesScreens from './screens/inventories';
import LoaderScreens from './screens/loader';
import ProductsScreens from './screens/products';
import StockCorrectionScreens from './screens/stockCorrections';
import SupplierArrivalsScreens from './screens/supplierArrivals';
Expand All @@ -45,6 +46,11 @@ export const StockModule: Module = {
downToVersion: '7.1.0',
},
menus: {
loader_test_screen: {
title: 'Loader',
icon: 'bug-fill',
screen: 'LoaderScreen',
},
stock_menu_product: {
title: 'Stock_Product',
icon: 'cart-fill',
Expand Down Expand Up @@ -80,6 +86,7 @@ export const StockModule: Module = {
...CustomerDeliveryScreens,
...InternalMovesScreens,
...InventoriesScreens,
...LoaderScreens,
...ProductsScreens,
...StockCorrectionScreens,
...SupplierArrivalsScreens,
Expand Down Expand Up @@ -114,6 +121,7 @@ export * from './screens/auth/UserScreen';
export * from './screens/customerDeliveries';
export * from './screens/internalMoves';
export * from './screens/inventories';
export * from './screens/loader';
export * from './screens/products';
export * from './screens/stockCorrections';
export * from './screens/supplierArrivals';
Expand Down
70 changes: 70 additions & 0 deletions packages/apps/stock/src/screens/loader/LoaderScreen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Axelor Business Solutions
*
* Copyright (C) 2024 Axelor (<http://axelor.com>).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/*
* Axelor Business Solutions
*
* Copyright (C) 2024 Axelor (<http://axelor.com>).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import React from 'react';
import {Button, View} from 'react-native';
import {Screen} from '@axelor/aos-mobile-ui';
import {useLoaderListner} from '@axelor/aos-mobile-core';

// Screen for test Loader functionnalities
const LoaderScreen = () => {
const process = () =>
new Promise(resolve => {
setTimeout(() => {
resolve('Process finished');
}, 10000);
});

const handleCustomAction = () => {
console.log('Custom action executed!');
};

const {loading, listener} = useLoaderListner({
process,
onSuccess: handleCustomAction,
onError: () => console.warn('An error has occurred!'),
});

return (
<Screen>
<View>
<Button title="check process" onPress={listener} disabled={loading} />
</View>
</Screen>
);
};

export default LoaderScreen;
31 changes: 31 additions & 0 deletions packages/apps/stock/src/screens/loader/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Axelor Business Solutions
*
* Copyright (C) 2024 Axelor (<http://axelor.com>).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import {default as LoaderScreen} from './LoaderScreen';

export default {
LoaderScreen: {
title: 'Test Loader',
component: LoaderScreen,
options: {
shadedHeader: false,
},
},
};

export {LoaderScreen};
19 changes: 19 additions & 0 deletions packages/core/src/components/templates/Loader/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Axelor Business Solutions
*
* Copyright (C) 2024 Axelor (<http://axelor.com>).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

export {default as useLoaderListner} from './use-loader-listener';
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Axelor Business Solutions
*
* Copyright (C) 2024 Axelor (<http://axelor.com>).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import {useCallback, useEffect, useMemo, useState} from 'react';
import {useTranslator} from '../../../i18n';
import {showToastMessage} from '../../../utils/show-toast-message';

interface LoaderListenerProps {
process: () => Promise<any>;
onSuccess?: () => void;
onError?: () => void;
}

const useLoaderListner = ({
process,
onSuccess = () => {},
onError = () => {},
}: LoaderListenerProps) => {
const I18n = useTranslator();

const [loading, setLoading] = useState(false);
const [start, setStart] = useState(false);

const executeProcess = useCallback(async () => {
try {
setStart(false);
setLoading(true);

const response = await process();

showToastMessage({
type: 'success',
position: 'top',
topOffset: 30,
text1: I18n.t('Base_Success'),
text2: response || I18n.t('Base_Loader_ProccessSuccessMessage'),
onPress: onSuccess,
});
} catch (error) {
showToastMessage({
type: 'error',
position: 'top',
topOffset: 30,
text1: I18n.t('Base_Error'),
text2: error || I18n.t('Base_Loader_ProccessErrorMessage'),
onPress: onError,
});
} finally {
setLoading(false);
}
}, [process, onSuccess, onError, I18n]);

useEffect(() => {
if (start && !loading) {
executeProcess();
}
}, [start, loading, executeProcess]);

return useMemo(() => ({loading, listener: () => setStart(true)}), [loading]);
};

export default useLoaderListner;
1 change: 1 addition & 0 deletions packages/core/src/components/templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

export {default as AttachedFilesView} from './AttachedFilesView/AttachedFilesView';
export * from './Loader';
export {default as MailMessageView} from './MailMessageView/MailMessageView';
export {default as PeriodInput} from './PeriodInput/PeriodInput';
export {default as PopupApplicationInformation} from './PopupApplicationInformation/PopupApplicationInformation';
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/i18n/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@
"Base_Yes": "Yes",
"Base_No": "No",
"Base_Filter": "Filter",
"Base_Loader_ProccessSuccessMessage": "Process successfully completed.",
"Base_Loader_ProccessErrorMessage": "An error has occurred.",
"Base_SliceAction_FetchAttachedFiles": "fetch attached files",
"Base_SliceAction_FetchFilesDetails": "fetch file details",
"Base_SliceAction_FetchMetaModule": "fetch meta modules",
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/i18n/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@
"Base_Yes": "Oui",
"Base_No": "Non",
"Base_Filter": "Filtrer",
"Base_Loader_ProccessSuccessMessage": "Le processus s'est terminé avec succès.",
"Base_Loader_ProccessErrorMessage": "Une erreur s'est produite.",
"Base_SliceAction_FetchAttachedFiles": "récupération des fichiers joints",
"Base_SliceAction_FetchFilesDetails": "récupération des détails du fichier",
"Base_SliceAction_FetchMetaModule": "récupération des modules",
Expand Down

0 comments on commit 7ca20f4

Please sign in to comment.