Merge pull request #1722 from 0xProject/fix/instant-polling

Unmount Instant when closing
This commit is contained in:
Francesco Agosti 2019-03-22 17:03:16 -07:00 committed by GitHub
commit fde9fc9dd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -65,21 +65,34 @@ const validateInstantRenderConfig = (config: ZeroExInstantConfig, selector: stri
assert.isString('selector', selector); assert.isString('selector', selector);
}; };
let injectedDiv: HTMLDivElement | undefined;
let parentElement: Element | undefined;
export const unrender = () => {
if (!injectedDiv) {
return;
}
ReactDOM.unmountComponentAtNode(injectedDiv);
if (parentElement && parentElement.contains(injectedDiv)) {
parentElement.removeChild(injectedDiv);
}
};
// Render instant and return a callback that allows you to remove it from the DOM. // Render instant and return a callback that allows you to remove it from the DOM.
const renderInstant = (config: ZeroExInstantConfig, selector: string) => { const renderInstant = (config: ZeroExInstantConfig, selector: string) => {
const appendToIfExists = document.querySelector(selector); const appendToIfExists = document.querySelector(selector);
assert.assert(!_.isNull(appendToIfExists), `Could not find div with selector: ${selector}`); assert.assert(!_.isNull(appendToIfExists), `Could not find div with selector: ${selector}`);
const appendTo = appendToIfExists as Element; parentElement = appendToIfExists as Element;
const injectedDiv = document.createElement('div'); injectedDiv = document.createElement('div');
injectedDiv.setAttribute('id', INJECTED_DIV_ID); injectedDiv.setAttribute('id', INJECTED_DIV_ID);
injectedDiv.setAttribute('class', INJECTED_DIV_CLASS); injectedDiv.setAttribute('class', INJECTED_DIV_CLASS);
appendTo.appendChild(injectedDiv); parentElement.appendChild(injectedDiv);
const closeInstant = () => { const closeInstant = () => {
analytics.trackInstantClosed(); analytics.trackInstantClosed();
if (!_.isUndefined(config.onClose)) { if (!_.isUndefined(config.onClose)) {
config.onClose(); config.onClose();
} }
appendTo.removeChild(injectedDiv); unrender();
}; };
const instantOverlayProps = { const instantOverlayProps = {
...config, ...config,