MyCrypto/common/utils/printElement.ts
Eddie Wang 980366694c RPC Error Handling (#384)
* create ensureOkResponse and check against RPC responses

* Merge with develop branch

* added single unit test

* main nodes added

* getBalance method

* remove console.log

* minor conflict fix - readd polyfill to integration test

* added two more method tests

* seperate rpcnode from extended classes

* fixes etherscan

* added all tests

* revert files with only formatting changes

* remove console.logs - still need to update snapshot before tests will pass

* updated snapshot due to RpcNode fixes for Infura and Etherscan nodes

* added RpcNodeTest config so we don't rely on constants in code

* undo formatting changes

* Multiple fixes to error handling tokens.

* Fixed TSC errors

* Minor styling edit - change async func to promise

* changed shape of tokenBalances

* change balance type back to stricter TokenValue type

* remove package.json change and include test for error state.

* minor change removing unneeded line of code

* added longer timeout for api

* update snapshot
2017-11-29 23:35:17 -06:00

56 lines
1.4 KiB
TypeScript

import React from 'react';
import { renderToString } from 'react-dom/server';
interface PrintOptions {
styles?: string;
printTimeout?: number;
popupFeatures?: object;
}
export default function(
element: React.ReactElement<any>,
opts: PrintOptions = {}
) {
const options = {
styles: '',
printTimeout: 500,
popupFeatures: {},
...opts
};
// Convert popupFeatures into a key=value,key=value string. See
// https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Window_features
// for more information.
const featuresStr = Object.keys(options.popupFeatures)
.map(key => `${key}=${options.popupFeatures[key]}`)
.join(',');
const popup = window.open('about:blank', 'printWindow', featuresStr);
if (popup) {
popup.document.open();
popup.document.write(`
<html>
<head>
<style>${options.styles}</style>
<script>
setTimeout(function() {
window.print();
}, ${options.printTimeout});
</script>
</head>
<body>
${renderToString(element)}
<script>
// FIXME consider if we REALLY want it
var width = document.body.children[0].offsetWidth;
var height = document.body.children[0].offsetHeight;
window.moveTo(0, 0);
// FIXME Chrome could be larger (i guess?)
window.resizeTo(width + 60, height + 60);
</script>
</body>
</html>
`);
}
}