import React, { useState, useEffect, useRef } from 'react'; import PropTypes from 'prop-types'; import { useDispatch, useSelector } from 'react-redux'; import { FormattedMessage } from 'react-intl'; import { ActionGroup, Alert, Button, Form, FormAlert, FormGroup, FormSelect, FormSelectOption, TextInput, Text, } from '@patternfly/react-core'; import { translate as __ } from 'foremanReact/common/I18n'; import { noop } from 'foremanReact/common/helpers'; import { UPSTREAM_SERVER, DEFAULT_CONTENT_VIEW_LABEL, DEFAULT_LIFECYCLE_ENVIRONMENT_LABEL, DEFAULT_ORGANIZATION_LABEL } from './CdnConfigurationConstants'; import EditableTextInput from '../../../../components/EditableTextInput'; import { selectUpdatingCdnConfiguration, } from '../../../Organizations/OrganizationSelectors'; import { updateCdnConfiguration } from '../../../Organizations/OrganizationActions'; import './CdnConfigurationForm.scss'; const UpstreamServerTypeForm = ({ showUpdate, contentCredentials, cdnConfiguration, onUpdate, }) => { const dispatch = useDispatch(); const urlValue = cdnConfiguration.type === UPSTREAM_SERVER ? cdnConfiguration.url : ''; const [url, setUrl] = useState(urlValue); const [username, setUsername] = useState(cdnConfiguration.username); const [password, setPassword] = useState(null); const [organizationLabel, setOrganizationLabel] = useState(cdnConfiguration.upstream_organization_label || DEFAULT_ORGANIZATION_LABEL); const [sslCaCredentialId, setSslCaCredentialId] = useState(cdnConfiguration.ssl_ca_credential_id); const updatingCdnConfiguration = useSelector(state => selectUpdatingCdnConfiguration(state)); const [contentViewLabel, setContentViewLabel] = useState(cdnConfiguration.upstream_content_view_label || DEFAULT_CONTENT_VIEW_LABEL); const [lifecycleEnvironmentLabel, setLifecycleEnvironmentLabel] = useState(cdnConfiguration.upstream_lifecycle_environment_label || DEFAULT_LIFECYCLE_ENVIRONMENT_LABEL); const [updateEnabled, setUpdateEnabled] = useState(false); const firstUpdate = useRef(true); useEffect(() => { if (firstUpdate.current) { firstUpdate.current = false; return; } setUpdateEnabled(true); }, [url, username, password, organizationLabel, contentViewLabel, lifecycleEnvironmentLabel, sslCaCredentialId, cdnConfiguration]); const editPassword = (value) => { if (value === null) { setPassword(''); } else { setPassword(value); } }; const hasPassword = (cdnConfiguration.password_exists && !password) || password?.length > 0; const requiredFields = [username, organizationLabel, sslCaCredentialId]; if (!hasPassword) { requiredFields.push(password); } const validated = !requiredFields.some(field => !field); const onError = () => setUpdateEnabled(true); const performUpdate = () => { setUpdateEnabled(false); dispatch(updateCdnConfiguration({ url, username, password, upstream_organization_label: organizationLabel || DEFAULT_ORGANIZATION_LABEL, ssl_ca_credential_id: sslCaCredentialId, upstream_content_view_label: contentViewLabel || DEFAULT_CONTENT_VIEW_LABEL, upstream_lifecycle_environment_label: lifecycleEnvironmentLabel || DEFAULT_LIFECYCLE_ENVIRONMENT_LABEL, type: UPSTREAM_SERVER, }, onUpdate, onError)); }; return (