2018-07-21 11:38:54 +03:00
|
|
|
import downIcon from '@fortawesome/fontawesome-free-solid/faAngleDoubleDown';
|
|
|
|
import upIcon from '@fortawesome/fontawesome-free-solid/faAngleDoubleUp';
|
|
|
|
import FontAwesomeIcon from '@fortawesome/react-fontawesome';
|
2018-08-11 22:39:27 +03:00
|
|
|
import { assoc, dissoc, isNil, pick, pipe, replace, trim } from 'ramda';
|
2018-07-21 20:27:00 +03:00
|
|
|
import React from 'react';
|
2018-07-28 19:59:32 +03:00
|
|
|
import { connect } from 'react-redux';
|
2018-07-21 20:27:00 +03:00
|
|
|
import { Collapse } from 'reactstrap';
|
2018-07-31 22:24:34 +03:00
|
|
|
import DateInput from '../common/DateInput';
|
2018-08-26 00:39:27 +03:00
|
|
|
import TagsSelector from '../utils/TagsSelector';
|
2018-07-28 11:41:05 +03:00
|
|
|
import CreateShortUrlResult from './helpers/CreateShortUrlResult';
|
2018-07-29 19:13:18 +03:00
|
|
|
import { createShortUrl, resetCreateShortUrl } from './reducers/shortUrlCreationResult';
|
2018-07-20 23:32:50 +03:00
|
|
|
|
2018-08-26 00:39:27 +03:00
|
|
|
export class CreateShortUrlComponent extends React.Component {
|
2018-07-21 11:38:54 +03:00
|
|
|
state = {
|
|
|
|
longUrl: '',
|
|
|
|
tags: [],
|
2018-07-21 20:27:00 +03:00
|
|
|
customSlug: undefined,
|
|
|
|
validSince: undefined,
|
|
|
|
validUntil: undefined,
|
|
|
|
maxVisits: undefined,
|
2018-08-26 00:39:27 +03:00
|
|
|
moreOptionsVisible: false,
|
2018-07-21 11:38:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2018-07-29 19:13:18 +03:00
|
|
|
const { createShortUrl, shortUrlCreationResult, resetCreateShortUrl } = this.props;
|
|
|
|
|
2018-08-26 00:39:27 +03:00
|
|
|
const changeTags = (tags) => this.setState({ tags: tags.map(pipe(trim, replace(/ /g, '-'))) });
|
|
|
|
const renderOptionalInput = (id, placeholder, type = 'text', props = {}) => (
|
2018-07-21 11:38:54 +03:00
|
|
|
<input
|
|
|
|
className="form-control"
|
|
|
|
type={type}
|
|
|
|
placeholder={placeholder}
|
|
|
|
value={this.state[id]}
|
2018-08-26 00:39:27 +03:00
|
|
|
onChange={(e) => this.setState({ [id]: e.target.value })}
|
2018-07-21 11:38:54 +03:00
|
|
|
{...props}
|
2018-08-26 00:39:27 +03:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
const createDateInput = (id, placeholder, props = {}) => (
|
2018-07-31 22:24:34 +03:00
|
|
|
<DateInput
|
2018-07-28 10:34:31 +03:00
|
|
|
selected={this.state[id]}
|
|
|
|
placeholderText={placeholder}
|
2018-08-09 21:13:46 +03:00
|
|
|
isClearable
|
2018-08-26 00:39:27 +03:00
|
|
|
onChange={(date) => this.setState({ [id]: date })}
|
2018-07-28 10:34:31 +03:00
|
|
|
{...props}
|
2018-08-26 00:39:27 +03:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
const formatDate = (date) => isNil(date) ? date : date.format();
|
|
|
|
const save = (e) => {
|
2018-07-28 11:41:05 +03:00
|
|
|
e.preventDefault();
|
2018-07-29 19:13:18 +03:00
|
|
|
createShortUrl(pipe(
|
2018-08-26 00:39:27 +03:00
|
|
|
dissoc('moreOptionsVisible'),
|
2018-07-28 19:59:32 +03:00
|
|
|
assoc('validSince', formatDate(this.state.validSince)),
|
|
|
|
assoc('validUntil', formatDate(this.state.validUntil))
|
|
|
|
)(this.state));
|
2018-07-28 11:41:05 +03:00
|
|
|
};
|
2018-07-21 11:38:54 +03:00
|
|
|
|
|
|
|
return (
|
2018-08-16 19:59:00 +03:00
|
|
|
<div className="shlink-container">
|
2018-07-28 11:41:05 +03:00
|
|
|
<form onSubmit={save}>
|
2018-07-21 11:38:54 +03:00
|
|
|
<div className="form-group">
|
|
|
|
<input
|
|
|
|
className="form-control form-control-lg"
|
|
|
|
type="url"
|
|
|
|
placeholder="Insert the URL to be shortened"
|
|
|
|
required
|
|
|
|
value={this.state.longUrl}
|
2018-08-26 00:39:27 +03:00
|
|
|
onChange={(e) => this.setState({ longUrl: e.target.value })}
|
2018-07-21 11:38:54 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Collapse isOpen={this.state.moreOptionsVisible}>
|
|
|
|
<div className="form-group">
|
2018-08-15 12:14:44 +03:00
|
|
|
<TagsSelector tags={this.state.tags} onChange={changeTags} />
|
2018-07-21 11:38:54 +03:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="row">
|
|
|
|
<div className="col-sm-6">
|
|
|
|
<div className="form-group">
|
|
|
|
{renderOptionalInput('customSlug', 'Custom slug')}
|
|
|
|
</div>
|
|
|
|
<div className="form-group">
|
|
|
|
{renderOptionalInput('maxVisits', 'Maximum number of visits allowed', 'number', { min: 1 })}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="col-sm-6">
|
2018-07-31 22:24:34 +03:00
|
|
|
<div className="form-group">
|
2018-07-21 20:27:00 +03:00
|
|
|
{createDateInput('validSince', 'Enabled since...', { maxDate: this.state.validUntil })}
|
2018-07-21 11:38:54 +03:00
|
|
|
</div>
|
2018-07-31 22:24:34 +03:00
|
|
|
<div className="form-group">
|
2018-07-21 20:27:00 +03:00
|
|
|
{createDateInput('validUntil', 'Enabled until...', { minDate: this.state.validSince })}
|
2018-07-21 11:38:54 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Collapse>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="btn btn-outline-secondary create-short-url__btn"
|
2018-08-26 00:39:27 +03:00
|
|
|
onClick={() => this.setState(({ moreOptionsVisible }) => ({ moreOptionsVisible: !moreOptionsVisible }))}
|
2018-07-21 11:38:54 +03:00
|
|
|
>
|
|
|
|
<FontAwesomeIcon icon={this.state.moreOptionsVisible ? upIcon : downIcon} />
|
|
|
|
|
|
|
|
{this.state.moreOptionsVisible ? 'Less' : 'More'} options
|
|
|
|
</button>
|
2018-07-29 19:13:18 +03:00
|
|
|
<button
|
|
|
|
className="btn btn-outline-primary create-short-url__btn float-right"
|
|
|
|
disabled={shortUrlCreationResult.loading}
|
|
|
|
>
|
|
|
|
{shortUrlCreationResult.loading ? 'Creating...' : 'Create'}
|
|
|
|
</button>
|
2018-07-21 11:38:54 +03:00
|
|
|
</div>
|
2018-07-28 11:41:05 +03:00
|
|
|
|
2018-07-29 19:13:18 +03:00
|
|
|
<CreateShortUrlResult {...shortUrlCreationResult} resetCreateShortUrl={resetCreateShortUrl} />
|
2018-07-21 11:38:54 +03:00
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2018-07-20 23:32:50 +03:00
|
|
|
}
|
2018-07-28 11:41:05 +03:00
|
|
|
|
2018-08-26 00:39:27 +03:00
|
|
|
const CreateShortUrl = connect(pick([ 'shortUrlCreationResult' ]), {
|
2018-07-29 19:13:18 +03:00
|
|
|
createShortUrl,
|
2018-08-26 00:39:27 +03:00
|
|
|
resetCreateShortUrl,
|
|
|
|
})(CreateShortUrlComponent);
|
|
|
|
|
|
|
|
export default CreateShortUrl;
|