import React from 'react'; import { copyToClipboard, getUrlData } from '$utils/history'; import { toTranslit, parseDesc } from '$utils/format'; import { TIPS } from '$constants/tips'; import { MODES } from '$constants/modes'; import { Icon } from '$components/panels/Icon'; import { Switch } from '$components/Switch'; import classnames from 'classnames'; import ExpandableTextarea from 'react-expandable-textarea'; import { connect } from 'react-redux'; import { selectMap } from '$redux/map/selectors'; import { selectUser } from '$redux/user/selectors'; import * as USER_ACTIONS from '$redux/user/actions'; const mapStateToProps = state => ({ map: selectMap(state), user: selectUser(state), }); const mapDispatchToProps = { setMode: USER_ACTIONS.setMode, sendSaveRequest: USER_ACTIONS.sendSaveRequest, }; type Props = ReturnType & typeof mapDispatchToProps & { width: number }; interface State { address: string; title: string; is_public: boolean; description: string; } class SaveDialogUnconnected extends React.Component { constructor(props) { super(props); this.state = { address: props.address || '', title: props.title || '', is_public: props.is_public || false, description: props.description || '', }; } getAddress = () => { const { path } = getUrlData(); const { title, address } = this.state; return ( toTranslit(address.trim()) || toTranslit(title.trim().toLowerCase()).substr(0, 32) || toTranslit(path.trim()).substr(0, 32) ); }; setTitle = ({ target: { value } }) => this.setState({ title: (value && value.substr(0, 64)) || '' }); setAddress = ({ target: { value } }) => this.setState({ address: (value && value.substr(0, 32)) || '' }); setDescription = ({ target: { value } }) => this.setState({ description: (value && value.substr(0, 256)) || '' }); sendSaveRequest = (e, force = false) => { const { title, is_public, description } = this.state; const address = this.getAddress(); this.props.sendSaveRequest({ title, address, force, is_public, description, }); }; forceSaveRequest = e => this.sendSaveRequest(e, true); cancelSaving = () => this.props.setMode(MODES.NONE); onCopy = e => { e.preventDefault(); const { host, protocol } = getUrlData(); copyToClipboard(`${protocol}//${host}/${this.getAddress()}`); }; togglePublic = () => { this.setState({ is_public: !this.state.is_public }); }; render() { const { title, is_public, description } = this.state; const { user: { save_error, save_finished, save_overwriting, save_loading }, width, } = this.props; const { host, protocol } = getUrlData(); return (
Название
{save_error || TIPS.SAVE_INFO}
{is_public ? ' В каталоге карт' : ' Только по ссылке'}
{!save_finished && (
Отмена
)} {!save_finished && !save_overwriting && (
Сохранить
)} {save_overwriting && (
Перезаписать
)} {save_finished && (
Скопировать
)} {save_finished && (
Отлично!
)}
); } } const SaveDialog = connect(mapStateToProps, mapDispatchToProps)(SaveDialogUnconnected); export { SaveDialog };