|
| 1 | +import React, { PureComponent } from 'react'; |
| 2 | +import { Button, Drawer, withStyles } from '@material-ui/core'; |
| 3 | +import Remarkable from 'react-remarkable'; |
| 4 | +import uriTemplate from 'uri-template'; |
| 5 | +import { Help } from '../Icons'; |
| 6 | +import { withAuthorization } from '../AuthorizationProvider'; |
| 7 | +import { http } from '../../utils'; |
| 8 | +import Typography from '@material-ui/core/es/Typography/Typography'; |
| 9 | + |
| 10 | +const getCurie = (rel, curies) => { |
| 11 | + const [prefix, rest] = rel.split(':', 2); |
| 12 | + |
| 13 | + return !rest || rest.indexOf(':') !== -1 |
| 14 | + ? { href: rel } |
| 15 | + : curies |
| 16 | + .filter(({ name }) => name === prefix) |
| 17 | + .map(({ href, ...link }) => ({ |
| 18 | + ...link, |
| 19 | + href: uriTemplate |
| 20 | + .parse(decodeURI(href)) |
| 21 | + .expand({ rel: rest }), |
| 22 | + }))[0] || { href: rel }; |
| 23 | +}; |
| 24 | + |
| 25 | +const Documentation = withStyles(theme => ({ |
| 26 | + drawerPaper: { |
| 27 | + width: '45%', |
| 28 | + padding: theme.spacing.unit * 2, |
| 29 | + }, |
| 30 | +}))(({ open, children, onClose, classes }) => ( |
| 31 | + <Drawer |
| 32 | + open={open} |
| 33 | + onClose={onClose} |
| 34 | + anchor={'right'} |
| 35 | + classes={{ |
| 36 | + paper: classes.drawerPaper, |
| 37 | + }} |
| 38 | + > |
| 39 | + <Typography> |
| 40 | + <Remarkable |
| 41 | + options={{ |
| 42 | + typographer: true, |
| 43 | + }} |
| 44 | + > |
| 45 | + {children} |
| 46 | + </Remarkable> |
| 47 | + </Typography> |
| 48 | + </Drawer> |
| 49 | +)); |
| 50 | + |
| 51 | +class HelpButton extends PureComponent { |
| 52 | + state = { |
| 53 | + open: false, |
| 54 | + href: null, |
| 55 | + type: null, |
| 56 | + disabled: true, |
| 57 | + documentation: null, |
| 58 | + }; |
| 59 | + |
| 60 | + static getDerivedStateFromProps = ({ rel, curies }, state) => ({ |
| 61 | + ...state, |
| 62 | + disabled: false, |
| 63 | + ...getCurie(rel, curies), |
| 64 | + }); |
| 65 | + |
| 66 | + _handleOnClick = async () => { |
| 67 | + const { authorization } = this.props; |
| 68 | + const { href, type } = this.state; |
| 69 | + const { body: documentation } = await http.get({ |
| 70 | + link: { href, type }, |
| 71 | + headers: { authorization }, |
| 72 | + }); |
| 73 | + |
| 74 | + this.setState({ |
| 75 | + ...this.state, |
| 76 | + open: true, |
| 77 | + documentation: |
| 78 | + typeof documentation === 'string' |
| 79 | + ? documentation |
| 80 | + : JSON.stringify(documentation), |
| 81 | + }); |
| 82 | + }; |
| 83 | + |
| 84 | + _handleOnClose = () => this.setState({ open: false }); |
| 85 | + |
| 86 | + render() { |
| 87 | + const { disabled } = this.props; |
| 88 | + const { documentation, open } = this.state; |
| 89 | + |
| 90 | + return ( |
| 91 | + <Button |
| 92 | + color={'secondary'} |
| 93 | + disabled={disabled} |
| 94 | + onClick={this._handleOnClick} |
| 95 | + > |
| 96 | + <Documentation onClose={this._handleOnClose} open={open}> |
| 97 | + {documentation} |
| 98 | + </Documentation> |
| 99 | + <Help /> |
| 100 | + {'Help'} |
| 101 | + </Button> |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +export default withAuthorization()(HelpButton); |
0 commit comments