CFE-6 bug fix toast and make better flow for remove and add toast in page
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import {createContext, useReducer} from "react";
|
||||
import {toast} from "react-toastify";
|
||||
|
||||
export const ToastContext = createContext()
|
||||
|
||||
@@ -9,6 +10,14 @@ const reducer = (state, action) => {
|
||||
...state,
|
||||
[action.toast_type]: [...state[action.toast_type], action.toast_id]
|
||||
};
|
||||
case "DISMISS":
|
||||
action.toast_type.map((item) => {
|
||||
state[item].map((id) => {
|
||||
toast.dismiss(id);
|
||||
})
|
||||
state[item] = []
|
||||
});
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -20,8 +29,6 @@ export const ToastProvider = ({children}) => {
|
||||
success: []
|
||||
});
|
||||
|
||||
console.log(state)
|
||||
|
||||
return (
|
||||
<ToastContext.Provider value={{state, dispatch}}>
|
||||
{children}
|
||||
|
||||
@@ -24,7 +24,7 @@ const useRequest = (initOptions) => {
|
||||
const network = useNetwork()
|
||||
const t = useTranslations()
|
||||
const {token, clearToken} = useUser()
|
||||
const {pushToastList} = useToast();
|
||||
const {pushToastList, dismissToastList} = useToast();
|
||||
let _options = {...defaultOptions, ...initOptions}
|
||||
|
||||
function requestServer(url = '', method = 'get', options) {
|
||||
@@ -40,22 +40,25 @@ const useRequest = (initOptions) => {
|
||||
reject()
|
||||
return
|
||||
}
|
||||
if (_options.notification && _options.failed.notification.show && _options.pending) Notifications("pending", t)
|
||||
if (_options.notification && _options.failed.notification.show && _options.pending) {
|
||||
dismissToastList(["pending", "warning", "error", "success"]);
|
||||
Notifications(pushToastList, "pending", t);
|
||||
}
|
||||
|
||||
axios({
|
||||
url: url, method: method, data: _options.data, ..._options.requestOptions
|
||||
})
|
||||
.then(response => {
|
||||
successRequest(response, t, _options)
|
||||
successRequest(pushToastList, dismissToastList, response, t, _options)
|
||||
resolve(response)
|
||||
})
|
||||
.catch(error => {
|
||||
if (error.response) {
|
||||
errorResponse(error.response, clearToken, t, _options.notification && _options.failed.notification.show)
|
||||
errorResponse(pushToastList, dismissToastList, error.response, clearToken, t, _options.notification && _options.failed.notification.show)
|
||||
} else if (error.request) {
|
||||
errorRequest(t, _options.notification && _options.failed.notification.show)
|
||||
errorRequest(dismissToastList, t, _options.notification && _options.failed.notification.show)
|
||||
} else {
|
||||
errorSetting(t, _options.notification && _options.failed.notification.show)
|
||||
errorSetting(dismissToastList, t, _options.notification && _options.failed.notification.show)
|
||||
}
|
||||
reject(error)
|
||||
})
|
||||
|
||||
@@ -2,15 +2,14 @@ import {useContext} from "react";
|
||||
import {ToastContext} from "@/lib/app/contexts/toast";
|
||||
|
||||
const useToast = () => {
|
||||
const {state, dispatch} = useContext(ToastContext);
|
||||
const {dispatch} = useContext(ToastContext);
|
||||
|
||||
const pushToastList = (toast_type, toast_id) => {
|
||||
console.log()
|
||||
dispatch({type: "PUSH", toast_type, toast_id});
|
||||
};
|
||||
|
||||
const dismissToastList = () => {
|
||||
dispatch({type: "DISMISS"});
|
||||
const dismissToastList = (toast_type) => {
|
||||
dispatch({type: "DISMISS", toast_type});
|
||||
};
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user