Skip to content

Commit

Permalink
feat: add LoaderToastNotifier
Browse files Browse the repository at this point in the history
  • Loading branch information
hel-axelor committed Feb 2, 2024
1 parent 6492620 commit a8cbc51
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 81 deletions.
6 changes: 4 additions & 2 deletions packages/apps/stock/src/screens/loader/LoaderScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import React, {useState} from 'react';
import {View} from 'react-native';
import {Button, Screen} from '@axelor/aos-mobile-ui';
import {LoaderPopup} from '@axelor/aos-mobile-core';
import {LoaderPopup, useLoader} from '@axelor/aos-mobile-core';

// Screen for test Loader functionnalities
const LoaderScreen = () => {
Expand All @@ -60,13 +60,15 @@ const LoaderScreen = () => {
console.log('Error action executed!');
};

const {loading} = useLoader();

return (
<Screen>
<View>
<Button
title="Run process"
onPress={() => setRunProccess(true)}
disabled={runProccess}
disabled={loading}
/>
<LoaderPopup
process={process}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/app/ContextedApplication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
Camera,
CameraScanner,
HeaderBandList,
LoaderToastNotifier,
Scanner,
Toast,
} from '../components';
Expand Down Expand Up @@ -87,6 +88,7 @@ const ContextedApplication = ({
<NavigationContainer>
<HeaderBandList />
<LoadingIndicator />
<LoaderToastNotifier />
<BlockInteractionMessage />
<RootNavigator
modules={modules}
Expand Down
203 changes: 185 additions & 18 deletions packages/core/src/components/templates/Loader/LoaderContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,104 @@ import React, {
useReducer,
} from 'react';

export type LoaderStatus = 'ok' | 'error' | undefined;

type callBack = () => void;

interface LoaderContextState {
loading: boolean;
notifyMe: boolean;
showPopup: boolean;
getCurrentNotifyMe: () => boolean;
status: LoaderStatus;
message: string;
finished: boolean;
disabled: boolean;
onSuccessCallBack: () => void;
onErrorCallBack: () => void;
setLoading: (option: boolean) => void;
setNotifyMe: (option: boolean) => void;
setShowPopup: (option: boolean) => void;
setStatus: (option: LoaderStatus) => void;
setMessage: (message: string) => void;
setFinished: (option: boolean) => void;
setDisabled: (option: boolean) => void;
setOnSuccessCallBack: (f: callBack) => void;
setOnErrorCallBack: (f: callBack) => void;
}

interface LoaderAction {
type: string;
payload: boolean;
payload: boolean | string | callBack | LoaderStatus;
}

const defaultLoaderContext = {
loading: false,
notifyMe: false,
showPopup: false,
getCurrentNotifyMe: () => {
throw new Error(
'LoaderProvider should be mounted to get current notify me',
);
status: null,
message: null,
finished: false,
disabled: false,
onSuccessCallBack: () => {},
onErrorCallBack: () => {},
setLoading: () => {
throw new Error('LoaderProvider should be mounted to set loading');
},
setNotifyMe: () => {
throw new Error('LoaderProvider should be mounted to set notify me');
},
setShowPopup: () => {
throw new Error('LoaderProvider should be mounted to show popup');
throw new Error('LoaderProvider should be mounted to set show popup');
},
setStatus: () => {
throw new Error('LoaderProvider should be mounted to set status');
},
setMessage: () => {
throw new Error('LoaderProvider should be mounted to set message');
},
setFinished: () => {
throw new Error('LoaderProvider should be mounted to set finished');
},
setDisabled: () => {
throw new Error('LoaderProvider should be mounted to set disabled');
},
setOnSuccessCallBack: () => {
throw new Error(
'LoaderProvider should be mounted to set on success callback',
);
},
setOnErrorCallBack: () => {
throw new Error(
'LoaderProvider should be mounted to set on success callback',
);
},
};

const LoaderContext = createContext<LoaderContextState>(defaultLoaderContext);

const actionTypes = {
getCurrentNotifyMe: 'getCurrentNotifyMe',
setLoading: 'setLoading',
setNotifyMe: 'setNotifyMe',
setShowPopup: 'setShowPopup',
setStatus: 'setStatus',
setMessage: 'setMessage',
setFinished: 'setFinished',
setDisabled: 'setDisabled',
setOnSuccessCallBack: 'setOnSuccessCallBack',
setOnErrorCallBack: 'setOnErrorCallBack',
};

const LoaderReducer = (
state: LoaderContextState,
action: LoaderAction,
): LoaderContextState => {
switch (action.type) {
case actionTypes.setLoading: {
return {
...state,
loading: action.payload as boolean,
};
}
case actionTypes.setNotifyMe: {
return {
...state,
Expand All @@ -78,10 +134,50 @@ const LoaderReducer = (
showPopup: action.payload as boolean,
};
}
case actionTypes.setStatus: {
return {
...state,
status: action.payload as LoaderStatus,
};
}
case actionTypes.setMessage: {
return {
...state,
message: action.payload as string,
};
}
case actionTypes.setFinished: {
return {
...state,
finished: action.payload as boolean,
};
}
case actionTypes.setDisabled: {
return {
...state,
disabled: action.payload as boolean,
};
}
case actionTypes.setOnSuccessCallBack: {
return {
...state,
onSuccessCallBack: action.payload as callBack,
};
}
case actionTypes.setOnErrorCallBack: {
return {
...state,
onErrorCallBack: action.payload as callBack,
};
}
}
};

const actions = {
setLoading: option => ({
type: actionTypes.setLoading,
payload: option,
}),
setNotifyMe: option => ({
type: actionTypes.setNotifyMe,
payload: option,
Expand All @@ -90,34 +186,105 @@ const actions = {
type: actionTypes.setShowPopup,
payload: option,
}),
setStatus: status => ({
type: actionTypes.setStatus,
payload: status,
}),
setMessage: message => ({
type: actionTypes.setMessage,
payload: message,
}),
setFinished: option => ({
type: actionTypes.setFinished,
payload: option,
}),
setDisabled: option => ({
type: actionTypes.setDisabled,
payload: option,
}),
setOnSuccessCallBack: f => ({
type: actionTypes.setOnSuccessCallBack,
payload: f,
}),
setOnErrorCallBack: f => ({
type: actionTypes.setOnErrorCallBack,
payload: f,
}),
};

export const LoaderProvider = ({children}) => {
const [state, dispatch] = useReducer(LoaderReducer, defaultLoaderContext);

const getCurrentNotifyMe = useCallback(() => {
console.log('getCurrentNotifyMe', state.notifyMe);
return state.notifyMe;
}, [state]);
const setLoading = useCallback(
option => dispatch(actions.setLoading(option)),
[],
);

const setNotifyMe = useCallback(option => {
console.log('setNotifyMe', option);
dispatch(actions.setNotifyMe(option));
}, []);
const setNotifyMe = useCallback(
option => dispatch(actions.setNotifyMe(option)),
[],
);

const setShowPopup = useCallback(
option => dispatch(actions.setShowPopup(option)),
[],
);

const setStatus = useCallback(
status => dispatch(actions.setStatus(status)),
[],
);

const setMessage = useCallback(
message => dispatch(actions.setMessage(message)),
[],
);

const setFinished = useCallback(
option => dispatch(actions.setFinished(option)),
[],
);

const setDisabled = useCallback(
option => dispatch(actions.setDisabled(option)),
[],
);

const setOnSuccessCallBack = useCallback(
f => dispatch(actions.setOnSuccessCallBack(f)),
[],
);

const setOnErrorCallBack = useCallback(
f => dispatch(actions.setOnErrorCallBack(f)),
[],
);

const LoaderContextState = useMemo<LoaderContextState>(
() => ({
...state,
getCurrentNotifyMe,
setLoading,
setNotifyMe,
setShowPopup,
setStatus,
setMessage,
setFinished,
setDisabled,
setOnSuccessCallBack,
setOnErrorCallBack,
}),
[state, getCurrentNotifyMe, setNotifyMe, setShowPopup],
[
state,
setLoading,
setNotifyMe,
setShowPopup,
setStatus,
setMessage,
setFinished,
setDisabled,
setOnSuccessCallBack,
setOnErrorCallBack,
],
);

return (
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/components/templates/Loader/LoaderPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ const LoaderPopup = ({

const timeoutRef = useRef(null);

const {notifyMe, showPopup, setNotifyMe, setShowPopup} = useLoader();
const {loading, notifyMe, showPopup, setNotifyMe, setShowPopup} = useLoader();

const {loading, listener} = useLoaderListner({
const {listener} = useLoaderListner({
process,
onFinish: () => setShowPopup(false),
onSuccess,
onError,
disabled,
Expand Down
Loading

0 comments on commit a8cbc51

Please sign in to comment.