1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

fix: copy feature double validation popup (#1117)

* fix: copy feature double validation popup

* fix: add aria-required, validate on submit

* refactor: rename variable to be more consistent with codebase
This commit is contained in:
Nuno Góis 2022-06-28 07:54:31 +01:00 committed by GitHub
parent 853d763194
commit 38cfc549f2

View File

@ -1,10 +1,4 @@
import {
useState,
useRef,
useEffect,
FormEventHandler,
ChangeEventHandler,
} from 'react';
import { useState, FormEventHandler, ChangeEventHandler } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import {
Button,
@ -31,16 +25,11 @@ export const CopyFeatureToggle = () => {
const [nameError, setNameError] = useState<string | undefined>();
const [newToggleName, setNewToggleName] = useState<string>();
const { cloneFeatureToggle, validateFeatureToggleName } = useFeatureApi();
const inputRef = useRef<HTMLInputElement>();
const featureId = useRequiredPathParam('featureId');
const projectId = useRequiredPathParam('projectId');
const { feature } = useFeature(projectId, featureId);
const navigate = useNavigate();
useEffect(() => {
inputRef.current?.focus();
}, []);
const setValue: ChangeEventHandler<HTMLInputElement> = event => {
const value = trim(event.target.value);
setNewToggleName(value);
@ -53,17 +42,20 @@ export const CopyFeatureToggle = () => {
const onValidateName = async () => {
try {
await validateFeatureToggleName(newToggleName);
setNameError(undefined);
return true;
} catch (error) {
setNameError(formatUnknownError(error));
}
return false;
};
const onSubmit: FormEventHandler = async event => {
event.preventDefault();
if (nameError) {
const isValidName = await onValidateName();
if (!isValidName) {
return;
}
@ -113,8 +105,8 @@ export const CopyFeatureToggle = () => {
helperText={nameError}
variant="outlined"
size="small"
inputRef={inputRef}
required
aria-required
autoFocus
/>
<FormControlLabel
control={