import React, { useState, useEffect } from 'react'; import useDeepCompareEffect from 'use-deep-compare-effect'; import { STATUS } from 'foremanReact/constants'; import { translate as __ } from 'foremanReact/common/I18n'; import PropTypes from 'prop-types'; import { useDispatch, useSelector } from 'react-redux'; import { Redirect } from 'react-router-dom'; import { Form, FormGroup, TextInput, TextArea, Checkbox, ActionGroup, Button, Tile, Grid, GridItem } from '@patternfly/react-core'; import { createContentView } from '../ContentViewsActions'; import { selectCreateContentViews, selectCreateContentViewStatus, selectCreateContentViewError } from './ContentViewCreateSelectors'; import { LabelDependencies, LabelAutoPublish, LabelImportOnly } from './ContentViewFormComponents'; import ContentViewIcon from '../components/ContentViewIcon'; import './CreateContentViewForm.scss'; const CreateContentViewForm = ({ setModalOpen }) => { const dispatch = useDispatch(); const [name, setName] = useState(''); const [label, setLabel] = useState(''); const [description, setDescription] = useState(''); const [composite, setComposite] = useState(false); const [component, setComponent] = useState(true); const [autoPublish, setAutoPublish] = useState(false); const [importOnly, setImportOnly] = useState(false); const [dependencies, setDependencies] = useState(false); const [redirect, setRedirect] = useState(false); const [saving, setSaving] = useState(false); const response = useSelector(selectCreateContentViews); const status = useSelector(selectCreateContentViewStatus); const error = useSelector(selectCreateContentViewError); useDeepCompareEffect(() => { const { id } = response; if (id && status === STATUS.RESOLVED && saving) { setSaving(false); setRedirect(true); } else if (status === STATUS.ERROR) { setSaving(false); } }, [response, status, error, saving]); const onSave = () => { setSaving(true); dispatch(createContentView({ name, label, description, composite, solve_dependencies: dependencies, auto_publish: (autoPublish && composite), import_only: importOnly, })); }; useEffect( () => { setLabel(name.replace(/ /g, '_')); }, [name], ); if (redirect) { const { id } = response; if (composite) { return (); } return (); } const submitDisabled = !name?.length || !label?.length || saving; return (
{ e.preventDefault(); onSave(); }} > setName(value)} /> setLabel(value)} />