Skip to content

Commit f0b06d2

Browse files
authored
change to use changeset and validation (#1142)
## Purpose Add maximum character limit for withdrawal justifications. ## Summary of Changes Changed `registries-states/is-public` component to use changeset and changeset validations.
1 parent 3f08c90 commit f0b06d2

File tree

3 files changed

+50
-26
lines changed

3 files changed

+50
-26
lines changed

lib/registries/addon/components/registries-states/is-public/component.ts

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import captureException, { getApiErrorMessage } from 'ember-osf-web/utils/captur
1111
import defaultTo from 'ember-osf-web/utils/default-to';
1212
import randomScientist from 'ember-osf-web/utils/random-scientist';
1313

14+
import Changeset from 'ember-changeset';
15+
import lookupValidator, { ValidationObject } from 'ember-changeset-validations';
16+
import { validateLength } from 'ember-changeset-validations/validators';
17+
import { ChangesetDef } from 'ember-changeset/types';
1418
import styles from './styles';
1519
import template from './template';
1620

@@ -20,41 +24,59 @@ export default class RegistrationIsPublic extends Component {
2024
@service toast!: Toast;
2125

2226
registration!: Registration;
27+
changeset!: ChangesetDef;
2328

2429
scientistName?: string;
2530
scientistNameInput?: string = '';
26-
withdrawalJustification?: string = '';
2731
closeDropdown!: () => void;
2832
showModal: boolean = defaultTo(this.showModal, false);
2933

34+
changesetValidation: ValidationObject<Registration> = {
35+
withdrawalJustification: validateLength({
36+
allowBlank: true,
37+
max: 2048,
38+
type: 'tooLong',
39+
translationArgs: {
40+
description: this.intl.t('registries.overview.withdraw.withdrawal_justification'),
41+
max: 2048,
42+
},
43+
}),
44+
};
45+
3046
@task({ withTestWaiter: true, drop: true })
3147
submitWithdrawal = task(function *(this: RegistrationIsPublic) {
3248
if (!this.registration) {
3349
return;
3450
}
3551

36-
this.registration.setProperties({
52+
this.changeset.setProperties({
3753
pendingWithdrawal: true,
38-
withdrawalJustification: this.withdrawalJustification,
3954
});
55+
this.changeset.validate();
56+
if (this.changeset.isValid) {
57+
try {
58+
yield this.changeset.save({});
59+
} catch (e) {
60+
const errorMessage = this.intl.t('registries.overview.withdraw.error');
61+
captureException(e, { errorMessage });
62+
this.toast.error(getApiErrorMessage(e), errorMessage);
63+
throw e;
64+
}
4065

41-
try {
42-
yield this.registration.save();
43-
} catch (e) {
44-
const errorMessage = this.intl.t('registries.overview.withdraw.error');
45-
captureException(e, { errorMessage });
46-
this.toast.error(getApiErrorMessage(e), errorMessage);
47-
throw e;
48-
}
49-
50-
this.toast.success(this.intl.t('registries.overview.withdraw.success'));
66+
this.toast.success(this.intl.t('registries.overview.withdraw.success'));
5167

52-
if (this.closeDropdown) {
53-
this.closeDropdown();
68+
if (this.closeDropdown) {
69+
this.closeDropdown();
70+
}
5471
}
5572
});
5673

5774
didReceiveAttrs() {
75+
this.changeset = new Changeset(
76+
this.registration,
77+
lookupValidator(this.changesetValidation),
78+
this.changesetValidation,
79+
) as ChangesetDef;
5880
this.setProperties({
5981
scientistNameInput: '',
6082
scientistName: randomScientist(),
@@ -65,16 +87,18 @@ export default class RegistrationIsPublic extends Component {
6587
'submitWithdrawal.isRunning',
6688
'scientistNameInput',
6789
'scientistName',
90+
'changeset.isInvalid',
6891
)
6992
get submitDisabled(): boolean {
7093
return this.submitWithdrawal.isRunning
71-
|| (this.scientistNameInput !== this.scientistName);
94+
|| (this.scientistNameInput !== this.scientistName)
95+
|| this.changeset.isInvalid;
7296
}
7397

7498
@action
7599
close() {
76-
if (this.registration.hasDirtyAttributes) {
77-
this.registration.rollbackAttributes();
100+
if (this.changeset.isDirty) {
101+
this.changeset.rollback();
78102
}
79103
this.closeDropdown();
80104
}

lib/registries/addon/components/registries-states/is-public/template.hbs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@
2222
<div local-class='WithdrawDropdown' data-test-withdrawal-form>
2323
<p>{{t 'registries.overview.withdraw.warning'}}</p>
2424
<p><strong>{{t 'registries.overview.withdraw.withdrawal_justification_label'}}</strong></p>
25-
<div class='form-group'>
26-
{{textarea
27-
class='form-control'
28-
rows='4'
29-
required=true
30-
value=this.withdrawalJustification
31-
}}
32-
</div>
25+
<FormControls
26+
@changeset={{this.changeset}}
27+
as |form|>
28+
<form.textarea
29+
@valuePath='withdrawalJustification'
30+
/>
31+
</FormControls>
3332
<p>{{t 'registries.overview.withdraw.random_scientist_x' x=this.scientistName htmlSafe=true}}</p>
3433
<div local-class='Container'>
3534
<div>

translations/en-us.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,7 @@ registries:
12661266
no_justification: 'None given'
12671267
state: Withdrawn
12681268
withdraw:
1269+
withdrawal_justification: 'Withdrawal justification'
12691270
withdraw: 'Withdraw registration'
12701271
withdrawal_justification_label: 'Please provide your justification for withdrawing this registration.'
12711272
random_scientist_x: 'Type <strong>{x}</strong> below and click Withdraw Registration if you are sure you want to continue.'

0 commit comments

Comments
 (0)