Files
yoga/website/src/components/Playground/URLShortener.js
Peter van der Zee e1c19cecad Upgrade Prettier to v1.12.1 on fbsource
Summary:
Updates Prettier to v1.12.1, using `xplat/js/scripts/prettier-bump.js` based on the playbook by https://our.intern.facebook.com/intern/wiki/Prettier/upgrade-guide/

All changes related to upgrading or running Prettier

allow-many-files
allow-large-files
bypass-lint
drop-conflicts
ignore-conflict-markers
ignore-signed-source
ignore-nocommit

Reviewed By: benjaffe, ryanmce, yungsters

Differential Revision: D7652946

fbshipit-source-id: 396d45c675e93f2298cd2920d927630c81867447
2018-04-19 03:05:38 -07:00

97 lines
2.1 KiB
JavaScript

/**
* Copyright (c) 2014-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
import React, {Component} from 'react';
import {Tooltip, notification, Button, Input} from 'antd';
import './URLShortener.css';
const API_KEY = 'AIzaSyCRvdtNY07SGUokChS8oA9EaYJafFL0zMI';
type State = {
shortURL: ?string,
loading: boolean,
};
export default class URLShortener extends Component<{}, State> {
_ref: ?HTMLElement = null;
state = {
loading: false,
shortURL: null,
};
componentDidMount() {
window.addEventListener('hashchange', this.onHashChange);
}
componentWillUnmount() {
window.removeEventListener('hashchange', this.onHashChange);
}
onHashChange = () => {
this.setState({shortURL: null});
};
onClick = () => {
this.setState(
{
loading: true,
},
() => {
if (window.ga) {
window.ga('send', {
hitType: 'event',
eventCategory: 'URLShortener',
eventAction: 'created',
});
}
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}) => this.setState({shortURL: id, loading: false}))
.catch(() => this.setState({shortURL: null, loading: false}));
},
);
};
render() {
return this.state.shortURL ? (
<Input
value={this.state.shortURL}
autoFocus
ref={ref => {
if (ref) {
ref.input.select();
}
}}
/>
) : (
<Button
className="URLShortener"
onClick={this.onClick}
icon="share-alt"
disabled={this.state.loading}
type="primary">
Share URL
</Button>
);
}
}