forked from axelor/axelor-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Loader toast management (axelor#366)
* RM#73988
- Loading branch information
1 parent
bd964b9
commit 7ca20f4
Showing
8 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
77 changes: 77 additions & 0 deletions
77
packages/core/src/components/templates/Loader/use-loader-listener.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters