Skip to content

Instantly share code, notes, and snippets.

@Korveld
Last active April 2, 2024 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Korveld/b9dfe75600fdf6526e28773684beaba1 to your computer and use it in GitHub Desktop.
Save Korveld/b9dfe75600fdf6526e28773684beaba1 to your computer and use it in GitHub Desktop.
Redux Saga callback function
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