Skip to content

Commit

Permalink
Fixed issue with validation on name field
Browse files Browse the repository at this point in the history
  • Loading branch information
rmathis committed Sep 1, 2022
1 parent 6ceb832 commit cf6f472
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 11 deletions.
1 change: 1 addition & 0 deletions backend/src/main/resources/i18n/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ label.configuration-value=Value
label.configuration-action=Action
message.delete-property-title=Delete Configuration?
message.delete-property-body=You are requesting to delete a configuration set. If you complete this process the set will be removed. This cannot be undone. Do you wish to continue?
message.name-required=Name is required.

label.external-description=Description

Expand Down
13 changes: 11 additions & 2 deletions ui/src/app/admin/IdpConfiguration.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@ export function IdpConfiguration() {
</ConfigurationsProvider>
} />
<Route path={`${path}/new`} render={() =>
<NewConfiguration />
<ConfigurationsProvider>
{(configurations) =>
<NewConfiguration configurations={configurations} />
}
</ConfigurationsProvider>

} />
<Route path={`${path}/:id/edit`} render={() =>
<EditConfiguration />
<ConfigurationsProvider>
{(configurations) =>
<EditConfiguration configurations={configurations} />
}
</ConfigurationsProvider>
} />
<Route path={`${path}`} exact render={() =>
<Redirect to={`${url}/list`} />
Expand Down
40 changes: 34 additions & 6 deletions ui/src/app/admin/component/ConfigurationForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ import FloatingLabel from 'react-bootstrap/FloatingLabel';
import { useTranslator } from '../../i18n/hooks';
import { includes } from 'lodash';

export function ConfigurationForm({ configuration = {}, loading, onSave, onCancel }) {
export function ConfigurationForm({ configurations, configuration = {}, loading, onSave, onCancel }) {

const { control, register, getValues, watch, formState: { errors } } = useForm({
const [names, setNames] = React.useState([]);

const { control, register, getValues, watch, formState: { errors, isValid }, handleSubmit } = useForm({
defaultValues: {
...configuration
}
},
reValidateMode: 'onChange',
mode: 'onChange',
});

const { fields, append, remove } = useFieldArray({
Expand Down Expand Up @@ -63,14 +67,24 @@ export function ConfigurationForm({ configuration = {}, loading, onSave, onCance

const translator = useTranslator();

React.useEffect(() => {
setNames(configurations.map(p => p.name));
}, [configurations]);

React.useEffect(() => console.log(errors, names), [errors, names]);

const onNext = (data) => {
console.log(data);
};

return (<>
<div className="container-fluid">
<div className="d-flex justify-content-end align-items-center">
<React.Fragment>
<Button variant="info" className="me-2"
type="button"
onClick={() => saveConfig(getValues())}
disabled={errors.length > 0 || loading}
disabled={ !isValid || loading}
aria-label="Save changes to the metadata source. You will return to the dashboard">
<FontAwesomeIcon icon={loading ? faSpinner : faSave} pulse={loading} />&nbsp;
<Translate value="action.save">Save</Translate>
Expand All @@ -85,12 +99,26 @@ export function ConfigurationForm({ configuration = {}, loading, onSave, onCance
</React.Fragment>
</div>
<hr />
<Form>
<Form onSubmit={handleSubmit(onNext)}>
<div className="row">
<div className="col-12 col-lg-5">
<Form.Group className="mb-3" controlId="formName">
<Form.Label><Translate value="label.configuration-name">Name</Translate></Form.Label>
<Form.Control type="text" placeholder={translator('label.configuration-name-placeholder')} required {...register(`name`)} />
<Form.Control
type="text"
required
isInvalid={errors.name}
placeholder={translator('label.configuration-name-placeholder')}
{...register(`name`, {
required: true,
validate: {
unique: v => !includes(names, v)
}
})} />
<Form.Text className={errors.name ? 'text-danger' : 'text-muted'}>
{errors?.name?.type === 'unique' && <Translate value={`message.must-be-unique`} />}
{errors?.name?.type === 'required' && <Translate value={`message.name-required`} />}
</Form.Text>
</Form.Group>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion ui/src/app/admin/container/EditConfiguration.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { PropertiesProvider } from '../hoc/PropertiesProvider';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faSpinner } from '@fortawesome/free-solid-svg-icons';

export function EditConfiguration() {
export function EditConfiguration({ configurations }) {
const history = useHistory();
const notifier = useNotificationDispatcher();
const translator = useTranslator();
Expand Down Expand Up @@ -81,6 +81,7 @@ export function EditConfiguration() {
<PropertiesProvider>
{configuration && <ConfigurationForm
configuration={configuration}
configurations={configurations}
loading={loading}
onSave={(data) => save(data)}
onCancel={() => cancel()} /> }
Expand Down
3 changes: 2 additions & 1 deletion ui/src/app/admin/container/NewConfiguration.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useTranslator } from '../../i18n/hooks';
import { BASE_PATH } from '../../App.constant';
import { PropertiesProvider } from '../hoc/PropertiesProvider';

export function NewConfiguration() {
export function NewConfiguration({ configurations }) {
const history = useHistory();
const notifier = useNotificationDispatcher();
const translator = useTranslator();
Expand Down Expand Up @@ -67,6 +67,7 @@ export function NewConfiguration() {
{(schema) =>
<ConfigurationForm
configuration={configuration}
configurations={configurations}
schema={schema}
loading={loading}
onSave={(data) => save(data)}
Expand Down
2 changes: 1 addition & 1 deletion ui/src/app/admin/hoc/ConfigurationsProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ export function ConfigurationsProvider({ children, cache = 'no-cache' }) {
React.useEffect(() => { loadConfigurations() }, []);

return (<>{children(configurations, removeConfiguration, loading)}</>);
}
}

0 comments on commit cf6f472

Please sign in to comment.