Last active
April 2, 2024 13:11
-
-
Save Korveld/b9dfe75600fdf6526e28773684beaba1 to your computer and use it in GitHub Desktop.
Redux Saga callback function
This file contains hidden or 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
Reducer file (reducer.ts) | |
postFiatDepositRequest: (state, { payload }: PayloadAction<IPostFiatDepositWorker>) => { | |
state.fiatTransactionsLoading = true; | |
}, | |
Redux Saga file (saga.ts) | |
function* postFiatDepositWorker({ payload }: PayloadAction<IPostFiatDepositWorker>) { | |
const { apiParams, onFinally } = payload; | |
let hasError = false; | |
try { | |
yield some functions | |
} catch (error) { | |
if (axios.isAxiosError(error)) { | |
if (error?.response?.data?.errors[0] === 'invalid_totp_code') { | |
hasError = true; | |
// notificationContainer('Invalid totp code.', 'user_blocked', 'Errors'); | |
} else { | |
// some errors callbacks | |
} | |
} | |
} finally { | |
onFinally?.(hasError); // Here is redux saga callback function | |
} | |
} | |
React component (someComponent.tsx) | |
const params: IPostFiatDepositWorker = { | |
apiParams: { | |
some_params: userId, | |
some_params: userId, | |
some_params: userId, | |
}, | |
onFinally: (hasError: boolean) => { // Here callback function from saga | |
setSubmitting(false); | |
dispatch(clearError()); | |
if (!hasError) { | |
resetForm(); | |
handleCloseModal(); | |
} | |
}, | |
}; | |
dispatch(postFiatDepositRequest(params)); | |
Types (types.ts) | |
export interface IPostFiatDepositWorker { | |
apiParams: IApiPostFiatDepositParams; | |
onFinally?: (hasError: boolean) => void; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment