import { assoc, dissoc, pick, pipe } from 'ramda'; import React from 'react'; import { connect } from 'react-redux'; import { createServer } from './reducers/server'; import { resetSelectedServer } from './reducers/selectedServer'; import { v4 as uuid } from 'uuid'; import './CreateServer.scss'; import ImportServersBtn from './helpers/ImportServersBtn'; export class CreateServer extends React.Component { state = { name: '', url: '', apiKey: '', serversImported: false, }; submit = e => { e.preventDefault(); const { createServer, history: { push } } = this.props; const server = pipe( assoc('id', uuid()), dissoc('serversImported') )(this.state); createServer(server); push(`/server/${server.id}/list-short-urls/1`) }; componentDidMount() { this.props.resetSelectedServer(); } render() { const renderInputGroup = (id, placeholder, type = 'text') =>
this.setState({ [id]: e.target.value })} required />
; return (
{renderInputGroup('name', 'Name')} {renderInputGroup('url', 'URL', 'url')} {renderInputGroup('apiKey', 'API key')}
{ this.setState({ serversImported: true }); setTimeout(() => this.setState({ serversImported: false }), 4000); }} />
{this.state.serversImported && (
Servers properly imported. You can now select one from the list :)
)}
); } } export default connect( pick(['selectedServer']), {createServer, resetSelectedServer } )(CreateServer);