mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-11 02:37:22 +03:00
Converted CreateServer into functional component
This commit is contained in:
parent
7db222664d
commit
f6baedc655
3 changed files with 55 additions and 68 deletions
|
@ -1,46 +1,34 @@
|
||||||
import { assoc, dissoc, pipe } from 'ramda';
|
import React, { useState, useEffect } from 'react';
|
||||||
import React from 'react';
|
|
||||||
import { v4 as uuid } from 'uuid';
|
import { v4 as uuid } from 'uuid';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import './CreateServer.scss';
|
import './CreateServer.scss';
|
||||||
|
|
||||||
const SHOW_IMPORT_MSG_TIME = 4000;
|
const SHOW_IMPORT_MSG_TIME = 4000;
|
||||||
|
const propTypes = {
|
||||||
|
createServer: PropTypes.func,
|
||||||
|
history: PropTypes.shape({
|
||||||
|
push: PropTypes.func,
|
||||||
|
}),
|
||||||
|
resetSelectedServer: PropTypes.func,
|
||||||
|
};
|
||||||
|
|
||||||
const CreateServer = (ImportServersBtn, stateFlagTimeout) => class CreateServer extends React.Component {
|
const CreateServer = (ImportServersBtn, useStateFlagTimeout) => {
|
||||||
static propTypes = {
|
const CreateServerComp = ({ createServer, history, resetSelectedServer }) => {
|
||||||
createServer: PropTypes.func,
|
const [ name, setName ] = useState('');
|
||||||
history: PropTypes.shape({
|
const [ url, setUrl ] = useState('');
|
||||||
push: PropTypes.func,
|
const [ apiKey, setApiKey ] = useState('');
|
||||||
}),
|
const [ serversImported, setServersImported ] = useStateFlagTimeout(false, SHOW_IMPORT_MSG_TIME);
|
||||||
resetSelectedServer: PropTypes.func,
|
const { push } = history;
|
||||||
};
|
const handleSubmit = (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
state = {
|
const id = uuid();
|
||||||
name: '',
|
const server = { id, name, url, apiKey };
|
||||||
url: '',
|
|
||||||
apiKey: '',
|
|
||||||
serversImported: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
handleSubmit = (e) => {
|
createServer(server);
|
||||||
e.preventDefault();
|
push(`/server/${id}/list-short-urls/1`);
|
||||||
|
};
|
||||||
const { createServer, history: { push } } = this.props;
|
const renderInputGroup = (id, placeholder, value, setState, type = 'text') => (
|
||||||
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') => (
|
|
||||||
<div className="form-group row">
|
<div className="form-group row">
|
||||||
<label htmlFor={id} className="col-lg-1 col-md-2 col-form-label create-server__label">
|
<label htmlFor={id} className="col-lg-1 col-md-2 col-form-label create-server__label">
|
||||||
{placeholder}:
|
{placeholder}:
|
||||||
|
@ -51,29 +39,31 @@ const CreateServer = (ImportServersBtn, stateFlagTimeout) => class CreateServer
|
||||||
className="form-control"
|
className="form-control"
|
||||||
id={id}
|
id={id}
|
||||||
placeholder={placeholder}
|
placeholder={placeholder}
|
||||||
value={this.state[id]}
|
value={value}
|
||||||
required
|
required
|
||||||
onChange={(e) => this.setState({ [id]: e.target.value })}
|
onChange={(e) => setState(e.target.value)}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
resetSelectedServer(); // FIXME Only when no serverId exists
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="create-server">
|
<div className="create-server">
|
||||||
<form onSubmit={this.handleSubmit}>
|
<form onSubmit={handleSubmit}>
|
||||||
{renderInputGroup('name', 'Name')}
|
{renderInputGroup('name', 'Name', name, setName)}
|
||||||
{renderInputGroup('url', 'URL', 'url')}
|
{renderInputGroup('url', 'URL', url, setUrl, 'url')}
|
||||||
{renderInputGroup('apiKey', 'API key')}
|
{renderInputGroup('apiKey', 'API key', apiKey, setApiKey)}
|
||||||
|
|
||||||
<div className="text-right">
|
<div className="text-right">
|
||||||
<ImportServersBtn
|
<ImportServersBtn onImport={setServersImported} />
|
||||||
onImport={() => stateFlagTimeout(this.setState.bind(this), 'serversImported', true, SHOW_IMPORT_MSG_TIME)}
|
|
||||||
/>
|
|
||||||
<button className="btn btn-outline-primary">Create server</button>
|
<button className="btn btn-outline-primary">Create server</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{this.state.serversImported && (
|
{serversImported && (
|
||||||
<div className="row create-server__import-success-msg">
|
<div className="row create-server__import-success-msg">
|
||||||
<div className="col-md-10 offset-md-1">
|
<div className="col-md-10 offset-md-1">
|
||||||
<div className="p-2 mt-3 bg-main text-white text-center">
|
<div className="p-2 mt-3 bg-main text-white text-center">
|
||||||
|
@ -85,7 +75,11 @@ const CreateServer = (ImportServersBtn, stateFlagTimeout) => class CreateServer
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
CreateServerComp.propTypes = propTypes;
|
||||||
|
|
||||||
|
return CreateServerComp;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default CreateServer;
|
export default CreateServer;
|
||||||
|
|
|
@ -14,7 +14,7 @@ import ServersExporter from './ServersExporter';
|
||||||
|
|
||||||
const provideServices = (bottle, connect, withRouter) => {
|
const provideServices = (bottle, connect, withRouter) => {
|
||||||
// Components
|
// Components
|
||||||
bottle.serviceFactory('CreateServer', CreateServer, 'ImportServersBtn', 'stateFlagTimeout');
|
bottle.serviceFactory('CreateServer', CreateServer, 'ImportServersBtn', 'useStateFlagTimeout');
|
||||||
bottle.decorator('CreateServer', connect([ 'selectedServer' ], [ 'createServer', 'resetSelectedServer' ]));
|
bottle.decorator('CreateServer', connect([ 'selectedServer' ], [ 'createServer', 'resetSelectedServer' ]));
|
||||||
|
|
||||||
bottle.serviceFactory('ServersDropdown', ServersDropdown, 'ServersExporter');
|
bottle.serviceFactory('ServersDropdown', ServersDropdown, 'ServersExporter');
|
||||||
|
|
|
@ -10,19 +10,24 @@ describe('<CreateServer />', () => {
|
||||||
const historyMock = {
|
const historyMock = {
|
||||||
push: jest.fn(),
|
push: jest.fn(),
|
||||||
};
|
};
|
||||||
|
const createWrapper = (serversImported = false) => {
|
||||||
beforeEach(() => {
|
const CreateServer = createServerConstruct(ImportServersBtn, () => [ serversImported, () => '' ]);
|
||||||
createServerMock.mockReset();
|
|
||||||
|
|
||||||
const CreateServer = createServerConstruct(ImportServersBtn);
|
|
||||||
|
|
||||||
wrapper = shallow(
|
wrapper = shallow(
|
||||||
<CreateServer createServer={createServerMock} resetSelectedServer={identity} history={historyMock} />
|
<CreateServer createServer={createServerMock} resetSelectedServer={identity} history={historyMock} />
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return wrapper;
|
||||||
|
};
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
jest.resetAllMocks();
|
||||||
|
wrapper && wrapper.unmount();
|
||||||
});
|
});
|
||||||
afterEach(() => wrapper.unmount());
|
|
||||||
|
|
||||||
it('renders components', () => {
|
it('renders components', () => {
|
||||||
|
const wrapper = createWrapper();
|
||||||
|
|
||||||
expect(wrapper.find('#name')).toHaveLength(1);
|
expect(wrapper.find('#name')).toHaveLength(1);
|
||||||
expect(wrapper.find('#url')).toHaveLength(1);
|
expect(wrapper.find('#url')).toHaveLength(1);
|
||||||
expect(wrapper.find('#apiKey')).toHaveLength(1);
|
expect(wrapper.find('#apiKey')).toHaveLength(1);
|
||||||
|
@ -31,11 +36,13 @@ describe('<CreateServer />', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('shows success message when imported is true', () => {
|
it('shows success message when imported is true', () => {
|
||||||
wrapper.setState({ serversImported: true });
|
const wrapper = createWrapper(true);
|
||||||
|
|
||||||
expect(wrapper.find('.create-server__import-success-msg')).toHaveLength(1);
|
expect(wrapper.find('.create-server__import-success-msg')).toHaveLength(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('creates server and redirects to it when form is submitted', () => {
|
it('creates server and redirects to it when form is submitted', () => {
|
||||||
|
const wrapper = createWrapper();
|
||||||
const form = wrapper.find('form');
|
const form = wrapper.find('form');
|
||||||
|
|
||||||
form.simulate('submit', { preventDefault() {
|
form.simulate('submit', { preventDefault() {
|
||||||
|
@ -45,18 +52,4 @@ describe('<CreateServer />', () => {
|
||||||
expect(createServerMock).toHaveBeenCalledTimes(1);
|
expect(createServerMock).toHaveBeenCalledTimes(1);
|
||||||
expect(historyMock.push).toHaveBeenCalledTimes(1);
|
expect(historyMock.push).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('updates state when inputs are changed', () => {
|
|
||||||
const nameInput = wrapper.find('#name');
|
|
||||||
const urlInput = wrapper.find('#url');
|
|
||||||
const apiKeyInput = wrapper.find('#apiKey');
|
|
||||||
|
|
||||||
nameInput.simulate('change', { target: { value: 'the_name' } });
|
|
||||||
urlInput.simulate('change', { target: { value: 'the_url' } });
|
|
||||||
apiKeyInput.simulate('change', { target: { value: 'the_api_key' } });
|
|
||||||
|
|
||||||
expect(wrapper.state('name')).toEqual('the_name');
|
|
||||||
expect(wrapper.state('url')).toEqual('the_url');
|
|
||||||
expect(wrapper.state('apiKey')).toEqual('the_api_key');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue