import { STATUS } from 'foremanReact/constants';
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { useDispatch, useSelector } from 'react-redux';
import { Redirect } from 'react-router-dom';
import { Form, FormGroup, TextInput, ActionGroup, Button } from '@patternfly/react-core';
import {
selectCopyContentViewError, selectCopyContentViews,
selectCopyContentViewStatus,
} from './ContentViewCopySelectors';
import { copyContentView } from '../ContentViewsActions';
const CopyContentViewForm = ({ cvId, setModalOpen }) => {
const dispatch = useDispatch();
const [name, setName] = useState('');
const [redirect, setRedirect] = useState(false);
const [saving, setSaving] = useState(false);
const response = useSelector(selectCopyContentViews);
const status = useSelector(selectCopyContentViewStatus);
const error = useSelector(selectCopyContentViewError);
useEffect(() => {
const { id } = response;
if (id && status === STATUS.RESOLVED) {
setSaving(false);
setRedirect(true);
} else if (status === STATUS.ERROR) {
setSaving(false);
}
}, [JSON.stringify(response), status, error]);
const onSubmit = () => {
setSaving(true);
dispatch(copyContentView({
id: cvId,
name,
}));
};
if (redirect) {
const { id } = response;
return (