summaryrefslogtreecommitdiffstats
path: root/front/odiparpack/app/components/SourceReader/SourceReader.js
blob: e1303a0aae1303cc1ba77d3f8da68238e6588939 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import React, { Component } from 'react';
import { PropTypes } from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Axios from 'axios';
import SyntaxHighlighter, { registerLanguage } from 'react-syntax-highlighter/prism-light';
import jsx from 'react-syntax-highlighter/languages/prism/jsx';
import themeSource from 'react-syntax-highlighter/styles/prism/xonokai';
import classNames from 'classnames';
import Code from '@material-ui/icons/Code';
import Close from '@material-ui/icons/Close';
import { Button, LinearProgress, Icon } from '@material-ui/core';
import codePreview from '../../config/codePreview';


const url = '/api/docs?src=';

const styles = theme => ({
  button: {
    margin: '8px 5px',
  },
  iconSmall: {
    fontSize: 20,
  },
  leftIcon: {
    marginRight: theme.spacing(1),
  },
  source: {
    overflow: 'hidden',
    height: 0,
    position: 'relative',
    transition: 'all .5s',
    margin: '0 -10px'
  },
  preloader: {
    position: 'absolute',
    top: 36,
    left: 0,
    width: '100%'
  },
  open: {
    height: 'auto',
  },
  src: {
    textAlign: 'center',
    margin: 10,
    fontFamily: 'monospace',
    '& span': {
      fontSize: 14,
      marginRight: 5,
      top: 3,
      position: 'relative'
    }
  }
});

class SourceReader extends Component {
  state = { raws: [], open: false, loading: false };

  sourceOpen = () => {
    const name = this.props.componentName;
    this.setState({ loading: true }, () => {
      Axios.get(url + name).then(result => this.setState({
        raws: result.data.records,
        loading: false
      }));
      this.setState({ open: !this.state.open });
    });
  };

  render() {
    const { raws, open, loading } = this.state;
    const { classes } = this.props;
    registerLanguage('jsx', jsx);
    if (codePreview.enable) {
      return (
        <div>
          <Button onClick={this.sourceOpen} color="secondary" className={classes.button} size="small">
            { open
              ? <Close className={classNames(classes.leftIcon, classes.iconSmall)} />
              : <Code className={classNames(classes.leftIcon, classes.iconSmall)} />
            }
            { open ? 'Hide Code' : 'Show Code' }
          </Button>
          <section className={classNames(classes.source, open ? classes.open : '')}>
            <p className={classes.src}>
              <Icon className="description">description</Icon>
              src/app/
              {this.props.componentName}
            </p>
            {loading
              && <LinearProgress color="secondary" className={classes.preloader} />
            }
            {raws.map((raw, index) => ([
              <div key={index.toString()}>
                <SyntaxHighlighter language="jsx" style={themeSource} showLineNumbers="true">
                  {raw.source.toString()}
                </SyntaxHighlighter>
              </div>
            ])
            )}
          </section>
        </div>
      );
    }
    return false;
  }
}

SourceReader.propTypes = {
  componentName: PropTypes.string.isRequired,
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(SourceReader);