import { faAngleDoubleDown as downIcon, faAngleDoubleUp as upIcon } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { assoc, dissoc, isNil, pipe, replace, trim } from 'ramda'; import React from 'react'; import { Collapse } from 'reactstrap'; import * as PropTypes from 'prop-types'; import DateInput from '../utils/DateInput'; import { createShortUrlResultType } from './reducers/shortUrlCreation'; const normalizeTag = pipe(trim, replace(/ /g, '-')); const formatDate = (date) => isNil(date) ? date : date.format(); const CreateShortUrl = (TagsSelector, CreateShortUrlResult) => class CreateShortUrl extends React.Component { static propTypes = { createShortUrl: PropTypes.func, shortUrlCreationResult: createShortUrlResultType, resetCreateShortUrl: PropTypes.func, }; state = { longUrl: '', tags: [], customSlug: undefined, validSince: undefined, validUntil: undefined, maxVisits: undefined, moreOptionsVisible: false, }; render() { const { createShortUrl, shortUrlCreationResult, resetCreateShortUrl } = this.props; const changeTags = (tags) => this.setState({ tags: tags.map(normalizeTag) }); const renderOptionalInput = (id, placeholder, type = 'text', props = {}) => (
this.setState({ [id]: e.target.value })} {...props} />
); const renderDateInput = (id, placeholder, props = {}) => (
this.setState({ [id]: date })} {...props} />
); const save = (e) => { e.preventDefault(); createShortUrl(pipe( dissoc('moreOptionsVisible'), assoc('validSince', formatDate(this.state.validSince)), assoc('validUntil', formatDate(this.state.validUntil)) )(this.state)); }; return (
this.setState({ longUrl: e.target.value })} />
{renderOptionalInput('customSlug', 'Custom slug')} {renderOptionalInput('maxVisits', 'Maximum number of visits allowed', 'number', { min: 1 })}
{renderDateInput('validSince', 'Enabled since...', { maxDate: this.state.validUntil })} {renderDateInput('validUntil', 'Enabled until...', { minDate: this.state.validSince })}
); } }; export default CreateShortUrl;