import { assoc, dissoc, pick, pipe } from 'ramda'; import React from 'react'; import { connect } from 'react-redux'; import { v4 as uuid } from 'uuid'; import PropTypes from 'prop-types'; import { stateFlagTimeout } from '../utils/utils'; import { resetSelectedServer } from './reducers/selectedServer'; import { createServer } from './reducers/server'; import './CreateServer.scss'; import ImportServersBtn from './helpers/ImportServersBtn'; const SHOW_IMPORT_MSG_TIME = 4000; export class CreateServerComponent extends React.Component { static propTypes = { createServer: PropTypes.func, history: PropTypes.shape({ push: PropTypes.func, }), resetSelectedServer: PropTypes.func, }; state = { name: '', url: '', apiKey: '', serversImported: false, }; handleSubmit = (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 })} />
); return (
{renderInputGroup('name', 'Name')} {renderInputGroup('url', 'URL', 'url')} {renderInputGroup('apiKey', 'API key')}
stateFlagTimeout(this.setState.bind(this), 'serversImported', true, SHOW_IMPORT_MSG_TIME)} />
{this.state.serversImported && (
Servers properly imported. You can now select one from the list :)
)}
); } } const CreateServer = connect( pick([ 'selectedServer' ]), { createServer, resetSelectedServer } )(CreateServerComponent); export default CreateServer;