insomnia/app/components/modals/GenerateCodeModal.js
Gregory Schier 9e84bc4387 Workspaces, Cookies, and More! (#31)
* Start on workspace dropdown and upgrade fontawesome

* WorkspaceDropdown start and Elm components!

* Lots of CSS shit

* Refactor some db stuff and move filter out of sidebar

* Adjust dropdown css

* Handle duplicate header names, and stuff

* Shitty cookies tab

* fixed cookie table a bit

* Modal refactor

* Starteed cookie modal design

* Better cookie storage and filter cookie modal

* Cookie editor round 1

* Fix kve cursor jumping and form encoding templating

* New cookies now show up in filter

* Checkpoint

* Stuff and fix environments css

* Added manage cookies button to cookie pane

* Fix accidental sidebar item drag on sidebar resize

* Environments modal is looking pretty good now

* Pretty much done environments nad cookies

* Some changes

* Fixed codemirror in modals

* Fixed some things

* Add basic proxy support

* Updated shortcuts

* Code snippet generation

* Some style

* bug fix

* Code export now gets cookies for correct domain
2016-08-15 10:04:36 -07:00

139 lines
4.1 KiB
JavaScript

import React, {Component} from 'react';
import HTTPSnippet, {availableTargets} from 'httpsnippet';
import CopyButton from '../base/CopyButton';
import Dropdown from '../base/Dropdown';
import Editor from '../base/Editor';
import Modal from '../base/Modal';
import ModalBody from '../base/ModalBody';
import ModalHeader from '../base/ModalHeader';
import ModalFooter from '../base/ModalFooter';
import {exportHar} from '../../lib/export/har';
const DEFAULT_TARGET = availableTargets().find(t => t.key === 'shell');
const DEFAULT_CLIENT = DEFAULT_TARGET.clients.find(t => t.key === 'curl');
const MODE_MAP = {
c: 'clike',
java: 'clike',
csharp: 'clike',
node: 'javascript',
objc: 'clike',
ocaml: 'mllike'
};
class GenerateCodeModal extends Component {
constructor (props) {
super(props);
this.state = {
cmd: '',
request: null,
target: DEFAULT_TARGET,
client: DEFAULT_CLIENT
};
}
_handleClientChange (client) {
const {target} = this.state;
this._generateCode(this.state.request, target, client);
}
_handleTargetChange (target) {
const {target: currentTarget} = this.state;
if (currentTarget.key === target.key) {
// No change
return;
}
const client = target.clients.find(c => c.key === target.default);
this._generateCode(this.state.request, target, client);
}
_generateCode (request, target, client) {
exportHar(request._id).then(har => {
const snippet = new HTTPSnippet(har);
const cmd = snippet.convert(target.key, client.key);
this.setState({request, cmd, client, target});
});
}
show (request) {
this.modal.show();
this._generateCode(request, DEFAULT_TARGET, DEFAULT_CLIENT);
}
render () {
const {cmd, target, client} = this.state;
const targets = availableTargets();
return (
<Modal ref={m => this.modal = m} tall={true} {...this.props}>
<ModalHeader>Generate Client Code</ModalHeader>
<ModalBody noScroll={true} style={{
display: 'grid',
gridTemplateColumns: '1fr',
gridTemplateRows: 'auto 1fr'
}}>
<div className="pad">
<Dropdown outline={true}>
<button className="btn btn--super-compact btn--outlined">
{target.title}
<i className="fa fa-caret-down"></i>
</button>
<ul>
{targets.map(target => (
<li key={target.key}>
<button onClick={() => this._handleTargetChange(target)}>
{target.title}
</button>
</li>
))}
</ul>
</Dropdown>
&nbsp;&nbsp;
<Dropdown outline={true}>
<button className="btn btn--super-compact btn--outlined">
{client.title}
<i className="fa fa-caret-down"></i>
</button>
<ul>
{target.clients.map(client => (
<li key={client.key}>
<button onClick={() => this._handleClientChange(client)}>
{client.title}
</button>
</li>
))}
</ul>
</Dropdown>
&nbsp;&nbsp;
<CopyButton content={cmd} className="pull-right btn btn--super-compact btn--outlined"/>
</div>
<Editor
className="border-top"
key={Date.now()}
mode={MODE_MAP[target.key] || target.key}
ref={n => this._editor = n}
lightTheme={true}
lineWrapping={true}
value={cmd}
/>
</ModalBody>
<ModalFooter>
<div className="pull-right">
<button className="btn" onClick={e => this.modal.hide()}>Done</button>
</div>
<div className="pad faint italic txt-sm tall">
* copy/paste this command into a Unix terminal
</div>
</ModalFooter>
</Modal>
);
}
}
GenerateCodeModal.propTypes = {};
export default GenerateCodeModal;