Skip to content

Commit

Permalink
Adding debounce to prevent reRender on Bricks
Browse files Browse the repository at this point in the history
  • Loading branch information
AdaltonLeite committed Apr 13, 2023
1 parent 26bba0b commit 3f6d24f
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 14 deletions.
6 changes: 5 additions & 1 deletion .storybook/bricksExamples.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ const ExampleComponent = ({ file }) => {

useEffect(() => {
import(`../examples/${file}`).then(({ default: App }) => {
setComponent(<App />);
setComponent(
<React.StrictMode>
<App />
</React.StrictMode>,
);
});
}, []);
return component;
Expand Down
26 changes: 19 additions & 7 deletions examples/bricks/CardPayment/1-Customization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,26 @@ const App = () => {
},
};

function createPayment() {
window.cardPaymentBrickController
.getFormData()
.then((cardFormData) => {
console.log('cardFormData received, creating payment...', cardFormData);
})
.catch((error) => {});
}

return (
<Card
initialization={{ amount: 100 }}
customization={customization}
onSubmit={async (param) => {
console.log(param);
}}
/>
<>
<Card
initialization={{ amount: 100 }}
customization={customization}
onSubmit={async (param) => {
console.log(param);
}}
/>
<button onClick={() => createPayment()}>Custom Button</button>
</>
);
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mercadopago/sdk-react",
"version": "0.0.6",
"version": "0.0.7",
"description": "Mercado Pago SDK React",
"main": "index.js",
"keywords": [
Expand Down
3 changes: 2 additions & 1 deletion src/@types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ declare global {
};
cardPaymentBrickController: {
unmount: () => void;
getFormData: () => Promise<void>;
};
walletBrickController: {
unmount: () => void;
};
statusScreenBrickController: {
unmount: () => void;
}
};
}
}
9 changes: 8 additions & 1 deletion src/bricks/cardPayment/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useEffect } from 'react';
import { DEBOUNCE_TIME_RENDER } from '../util/constants';
import {
onErrorDefault,
onReadyDefault,
Expand Down Expand Up @@ -41,6 +42,8 @@ const CardPayment = ({
customization,
}: TCardPayment) => {
useEffect(() => {
// CardPayment uses a debounce to prevent unnecessary reRenders.
let timer: ReturnType<typeof setTimeout>;
const CardPaymentBrickConfig = {
settings: {
initialization,
Expand All @@ -56,8 +59,12 @@ const CardPayment = ({
divId: 'cardPaymentBrick_container',
controller: 'cardPaymentBrickController',
};
initBrick(CardPaymentBrickConfig);
timer = setTimeout(() => {
initBrick(CardPaymentBrickConfig);
}, DEBOUNCE_TIME_RENDER);

return () => {
clearTimeout(timer);
window.cardPaymentBrickController?.unmount();
};
}, [initialization, customization, onBinChange, onReady, onError, onSubmit]);
Expand Down
9 changes: 8 additions & 1 deletion src/bricks/payment/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useEffect } from 'react';
import { DEBOUNCE_TIME_RENDER } from '../util/constants';
import {
onBinChangeDefault,
onErrorDefault,
Expand Down Expand Up @@ -40,6 +41,8 @@ const BrickPayment = ({
customization,
}: TPaymentType) => {
useEffect(() => {
// CardPayment uses a debounce to prevent unnecessary reRenders.
let timer: ReturnType<typeof setTimeout>;
const PaymentBrickController = {
settings: {
initialization,
Expand All @@ -55,8 +58,12 @@ const BrickPayment = ({
divId: 'paymentBrick_container',
controller: 'paymentBrickController',
};
initBrick(PaymentBrickController);
timer = setTimeout(() => {
initBrick(PaymentBrickController);
}, DEBOUNCE_TIME_RENDER);

return () => {
clearTimeout(timer);
window.paymentBrickController?.unmount();
};
}, [initialization, customization, onReady, onError, onSubmit, onBinChange]);
Expand Down
9 changes: 8 additions & 1 deletion src/bricks/statusScreen/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useEffect } from 'react';
import { onErrorDefault, onReadyDefault } from '../util/initial';
import { IStatusScreenBrickSettings } from './types';
import { initBrick } from '../util/renderBrick';
import { DEBOUNCE_TIME_RENDER } from '../util/constants';

// /**
// * Status Screen Brick allows you to show buyer the status of a purchase made with any payment method provided by Checkout Bricks.
Expand Down Expand Up @@ -35,6 +36,8 @@ const StatusScreen = ({
initialization,
}: IStatusScreenBrickSettings) => {
useEffect(() => {
// CardPayment uses a debounce to prevent unnecessary reRenders.
let timer: ReturnType<typeof setTimeout>;
const StatusScreenBrickConfig = {
settings: {
initialization,
Expand All @@ -48,8 +51,12 @@ const StatusScreen = ({
divId: 'statusScreenBrick_container',
controller: 'statusScreenBrickController',
};
initBrick(StatusScreenBrickConfig);
timer = setTimeout(() => {
initBrick(StatusScreenBrickConfig);
}, DEBOUNCE_TIME_RENDER);

return () => {
clearTimeout(timer);
window.statusScreenBrickController?.unmount();
};
}, [initialization, customization, onReady, onError]);
Expand Down
2 changes: 2 additions & 0 deletions src/bricks/util/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// CardPayment uses a debounce to prevent unnecessary reRenders.
export const DEBOUNCE_TIME_RENDER = 200; //ms
9 changes: 8 additions & 1 deletion src/bricks/wallet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useEffect } from 'react';
import { onErrorDefault, onReadyDefault, onSubmitDefault } from '../util/initial';
import { TWallet } from './types';
import { initBrick } from '../util/renderBrick';
import { DEBOUNCE_TIME_RENDER } from '../util/constants';

/**
* Wallet Brick allows you to offer payments from your Mercado Pago account at any stage of the purchase process.
Expand Down Expand Up @@ -31,6 +32,8 @@ const Wallet = ({
initialization,
}: TWallet) => {
useEffect(() => {
// CardPayment uses a debounce to prevent unnecessary reRenders.
let timer: ReturnType<typeof setTimeout>;
const WalletBrickConfig = {
settings: {
initialization,
Expand All @@ -46,8 +49,12 @@ const Wallet = ({
controller: 'walletBrickController',
};

initBrick(WalletBrickConfig);
timer = setTimeout(() => {
initBrick(WalletBrickConfig);
}, DEBOUNCE_TIME_RENDER);

return () => {
clearTimeout(timer);
window.walletBrickController?.unmount();
};
}, [customization, initialization, onReady, onError, onSubmit]);
Expand Down

0 comments on commit 3f6d24f

Please sign in to comment.