Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GN-4378 Revalidate codelist options list when changing the label #113

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/components/codelist-form.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
<AuInput
@error={{labelError}}
@value={{option.label}}
{{on 'change' (fn this.setOptionLabel option)}}
{{on 'input' (fn this.setOptionLabel option)}}
/>
{{#if labelError}}
<AuHelpText>
Expand Down
20 changes: 15 additions & 5 deletions app/components/codelist-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,30 @@ export default class CodelistFormComponent extends Component {
@action
setOptionLabel(option, event) {
option.label = event.target.value;
// auto-validate the uniqueness of labels,
// just like ember-changeset does for other validation
this.validateUniqueLabels.perform();
}

validateUniqueLabels() {
validateUniqueLabels = dropTask(async () => {
const uniqueValues = new Set();
this.optionsChangesetList.changesets.forEach((option) => {
if (!isBlank(option.label) && uniqueValues.has(option.label)) {
let label = option.label;
//workaround to clear a custom error
option.label = label;

if (uniqueValues.has(label)) {
option.addError(
'label',
this.intl.t('codelist.options.label-unique-error')
);
} else {
uniqueValues.add(option.label);
if (label !== undefined && !isBlank(label)) {
uniqueValues.add(label);
}
}
});
}
});

@action
updateCodelistType(type) {
Expand All @@ -166,7 +175,8 @@ export default class CodelistFormComponent extends Component {
changeset.label = null;
}
});
this.validateUniqueLabels();

await this.validateUniqueLabels.perform();

if (
this.allValid &&
Expand Down