From 60ad63eb9eaca4331449a7cd0495a23f72b69c40 Mon Sep 17 00:00:00 2001 From: Ritesh Kumar Date: Sat, 2 Sep 2017 18:32:17 +0530 Subject: [PATCH] closes #25 --- components/ConversionPanel.js | 24 ++++++++++++++++++++---- package.json | 1 + yarn.lock | 2 +- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/components/ConversionPanel.js b/components/ConversionPanel.js index 7b936dc..d519f80 100644 --- a/components/ConversionPanel.js +++ b/components/ConversionPanel.js @@ -3,7 +3,8 @@ import cn from "classnames"; import Router from "next/router"; import isBrowser from "is-in-browser"; import copy from "copy-text-to-clipboard"; -import { html_beautify, css_beautify, js_beautify } from "js-beautify" +import { html_beautify, css_beautify, js_beautify } from "js-beautify"; +import unfetch from "unfetch"; const theme = "tomorrow"; @@ -62,7 +63,9 @@ type Props = { splitLeft: ?boolean, splitTitle: ?string, splitValue: ?string, - splitMode: ?string + splitMode: ?string, + showFetchButton: boolean, + fetchButtonText: ?string }; export default class ConversionPanel extends PureComponent { @@ -73,7 +76,9 @@ export default class ConversionPanel extends PureComponent { rightMode: "javascript", pathname: "/", prettifyRightPanel: true, - splitLeft: false + splitLeft: false, + showFetchButton: true, + fetchButtonText: 'Fetch JSON from URL' }; state = { @@ -96,6 +101,14 @@ export default class ConversionPanel extends PureComponent { }); } + fetchJSON = async () => { + const url = window.prompt('Enter URL to fetch JSON from a remote server.') + if (!url) return + const res = await unfetch(url) + const json = await res.json() + this.onChange(JSON.stringify(json, null, 2)) + } + onChange = (newValue, leftSplitValue) => { const nValue = newValue || this.state.value; const splitValue = @@ -174,7 +187,9 @@ export default class ConversionPanel extends PureComponent { prettifyRightPanel, splitLeft, splitTitle, - splitMode + splitMode, + showFetchButton, + fetchButtonText } = this.props; const { infoType, resultValue, value, info, splitValue } = this.state; @@ -351,6 +366,7 @@ export default class ConversionPanel extends PureComponent {

{leftTitle}

+ {leftMode === 'json' && showFetchButton && } diff --git a/package.json b/package.json index 37f571b..416edaa 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "react-fuzzy-picker": "^0.5.1", "react-github-corner": "^1.0.0", "stringify-object": "^3.2.0", + "unfetch": "^3.0.0", "xml-js": "^1.4.1" }, "devDependencies": { diff --git a/yarn.lock b/yarn.lock index c019297..db10f27 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5002,7 +5002,7 @@ ultron@~1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/ultron/-/ultron-1.1.0.tgz#b07a2e6a541a815fc6a34ccd4533baec307ca864" -unfetch@3.0.0: +unfetch@3.0.0, unfetch@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/unfetch/-/unfetch-3.0.0.tgz#8d1e0513a4ecd0e5ff2d41a6ba77771aae8b6482"