@@ -11,6 +11,10 @@ import captureException, { getApiErrorMessage } from 'ember-osf-web/utils/captur
1111import defaultTo from 'ember-osf-web/utils/default-to' ;
1212import 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' ;
1418import styles from './styles' ;
1519import 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 }
0 commit comments