From 96a87a811d19e527280eb9380e4495e04f33b46f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=BCchele?= Date: Tue, 13 Feb 2018 09:20:44 -0800 Subject: [PATCH] Shorten playground URL Summary: Shorten URL using Google's URL shortner and display URL for copying. Reviewed By: emilsjolander Differential Revision: D6976973 fbshipit-source-id: cc791ffbf9e01a8dc2e0a40d05bce5022a3957bc --- .../components/Playground/URLShortener.css | 7 ++ .../src/components/Playground/URLShortener.js | 68 +++++++++++++++++++ website/src/components/Playground/index.js | 3 +- 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 website/src/components/Playground/URLShortener.css create mode 100644 website/src/components/Playground/URLShortener.js diff --git a/website/src/components/Playground/URLShortener.css b/website/src/components/Playground/URLShortener.css new file mode 100644 index 00000000..2b9f5e12 --- /dev/null +++ b/website/src/components/Playground/URLShortener.css @@ -0,0 +1,7 @@ +.URLShortener { + position: absolute; + bottom: 30px; + left: 30px; + z-index: 4; + box-shadow: 1px 1px 15px rgba(0, 0, 0, 0.2); +} diff --git a/website/src/components/Playground/URLShortener.js b/website/src/components/Playground/URLShortener.js new file mode 100644 index 00000000..b93469b9 --- /dev/null +++ b/website/src/components/Playground/URLShortener.js @@ -0,0 +1,68 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @flow + * @format + */ + +import React, {Component} from 'react'; +import {Tooltip, notification, Button, Input} from 'antd'; +import './URLShortener.css'; + +const API_KEY = 'AIzaSyCRvdtNY07SGUokChS8oA9EaYJafFL0zMI'; + +export default class URLShortener extends Component<{}> { + _ref: ?HTMLElement = null; + + onClick = () => { + fetch(`https://www.googleapis.com/urlshortener/v1/url?key=${API_KEY}`, { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + kind: 'urlshortener#url', + longUrl: window.location.href, + }), + }) + .then(res => res.json()) + .then(({id}) => { + notification.open({ + message: 'Created short URL', + description: ( + { + if (ref) { + ref.input.select(); + } + }} + /> + ), + placement: 'bottomLeft', + }); + }); + }; + + render() { + return ( + +