import React from 'react'; import { connect } from 'react-redux'; import { Form, Input, Button, Icon } from 'UI'; import { save, edit, update, fetchList, remove } from 'Duck/site'; import { pushNewSite } from 'Duck/user'; import { setSiteId } from 'Duck/site'; import { withRouter } from 'react-router-dom'; import styles from './siteForm.module.css'; import { confirm } from 'UI'; import { clearSearch } from 'Duck/search'; import { clearSearch as clearSearchLive } from 'Duck/liveSearch'; import { withStore } from 'App/mstore'; import { toast } from 'react-toastify'; @connect( (state) => ({ site: state.getIn(['site', 'instance']), sites: state.getIn(['site', 'list']), siteList: state.getIn(['site', 'list']), loading: state.getIn(['site', 'save', 'loading']) || state.getIn(['site', 'remove', 'loading']), }), { save, remove, edit, update, pushNewSite, fetchList, setSiteId, clearSearch, clearSearchLive, } ) @withRouter @withStore export default class NewSiteForm extends React.PureComponent { state = { existsError: false, }; componentDidMount() { const { location: { pathname }, match: { params: { siteId }, }, } = this.props; if (pathname.includes('onboarding')) { this.props.setSiteId(siteId); } } onSubmit = (e) => { e.preventDefault(); const { site, siteList, location: { pathname }, } = this.props; if (site.exists()) { this.props.update(this.props.site, this.props.site.id).then((response) => { if (!response || !response.errors || response.errors.size === 0) { this.props.onClose(null); this.props.fetchList(); toast.success('Project updated successfully'); } else { toast.error(response.errors[0]); } }); } else { this.props.save(this.props.site).then((response) => { if (!response || !response.errors || response.errors.size === 0) { this.props.onClose(null); this.props.clearSearch(); this.props.clearSearchLive(); this.props.mstore.initClient(); toast.success('Project added successfully'); } else { toast.error(response.errors[0]); } }); } }; remove = async (site) => { if ( await confirm({ header: 'Projects', confirmation: `Are you sure you want to delete this Project? We won't be able to record anymore sessions.`, }) ) { this.props.remove(site.id).then(() => { this.props.onClose(null); }); } }; edit = ({ target: { name, value } }) => { this.setState({ existsError: false }); this.props.edit({ [name]: value }); }; render() { const { site, loading } = this.props; return (