1
+ define ( [
2
+ 'knockout' ,
3
+ 'text!./snapshot-lock-modal.html' ,
4
+ 'components/Component' ,
5
+ 'utils/CommonUtils' ,
6
+ 'utils/AutoBind' ,
7
+ 'services/AuthAPI' ,
8
+ 'services/SourceAPI' ,
9
+ 'services/ConceptSet' ,
10
+ 'atlas-state' ,
11
+ 'less!./snapshot-lock-modal.less' ,
12
+ 'databindings' ,
13
+ ] , function (
14
+ ko ,
15
+ view ,
16
+ Component ,
17
+ commonUtils ,
18
+ AutoBind ,
19
+ authApi ,
20
+ sourceApi ,
21
+ conceptSetService ,
22
+ sharedState ,
23
+ ) {
24
+ class SnapshotLockModal extends AutoBind ( Component ) {
25
+ constructor ( params ) {
26
+ super ( params ) ;
27
+ this . isModalShown = params . isModalShown ;
28
+ this . isLocked = params . isLocked ;
29
+ this . takeSnapshotWhenUnlocking = ko . observable ( false ) ;
30
+ this . currentConceptSetId = params . currentConceptSetId ;
31
+ this . currentVocabularyVersion = params . currentVocabularyVersion ;
32
+ this . currentVocabularySchema = ko . observable ( ) ;
33
+ this . snapshotDescriptionMessage = ko . observable ( '' ) ;
34
+ this . canExecuteActions = ko . pureComputed ( ( ) => {
35
+ const hasSnapshotDesc = this . snapshotDescriptionMessage ( ) . trim ( ) . length > 0 ;
36
+ return hasSnapshotDesc ;
37
+ } ) ;
38
+ this . fetchAndSetVocabularySchema ( ) ;
39
+ }
40
+
41
+ async fetchAndSetVocabularySchema ( ) {
42
+ try {
43
+ const source = await sourceApi . getSourceInfo ( sharedState . sourceKeyOfVocabUrl ( ) ) ;
44
+ this . processSourceData ( source ) ;
45
+ } catch ( error ) {
46
+ console . error ( `Error fetching source information: ${ error } ` ) ;
47
+ this . currentVocabularySchema ( undefined ) ;
48
+ }
49
+ }
50
+
51
+ processSourceData ( source ) {
52
+ if ( ! source || ! source . daimons ) {
53
+ this . currentVocabularySchema ( undefined ) ;
54
+ } else {
55
+ const vocabularyDaimon = source . daimons . find ( daimon => daimon . daimonType === 'Vocabulary' ) ;
56
+ this . currentVocabularySchema ( vocabularyDaimon ? vocabularyDaimon . tableQualifier : undefined ) ;
57
+ }
58
+ }
59
+
60
+
61
+ createSnapshotActionRequest ( action , takeSnapshot = true ) {
62
+ return {
63
+ sourceKey : sharedState . sourceKeyOfVocabUrl ( ) ,
64
+ action : action ,
65
+ user : authApi . subject ( ) ,
66
+ message : this . snapshotDescriptionMessage ( ) ,
67
+ takeSnapshot : takeSnapshot
68
+ } ;
69
+ }
70
+
71
+ snapshotAndLock ( ) {
72
+ const request = this . createSnapshotActionRequest ( "LOCK" ) ;
73
+ conceptSetService . invokeConceptSetSnapshotAction ( this . currentConceptSetId ( ) , request )
74
+ . then ( ( ) => {
75
+ this . isLocked ( true ) ;
76
+ this . snapshotDescriptionMessage ( "" ) ;
77
+ this . isModalShown ( false ) ;
78
+ console . log ( "Concept set locked and snapshot created" ) ;
79
+ } )
80
+ . catch ( error => console . error ( `Error locking concept set: ${ error } ` ) ) ;
81
+ }
82
+
83
+ snapshotOnly ( ) {
84
+ const request = this . createSnapshotActionRequest ( "SNAPSHOT" ) ;
85
+ conceptSetService . invokeConceptSetSnapshotAction ( this . currentConceptSetId ( ) , request )
86
+ . then ( ( ) => {
87
+ this . snapshotDescriptionMessage ( "" ) ;
88
+ console . log ( "Concept set snapshot created" ) ;
89
+ this . isModalShown ( false ) ;
90
+ } )
91
+ . catch ( error => console . error ( `Error creating snapshot: ${ error } ` ) ) ;
92
+ }
93
+
94
+ unlockConceptSet ( takeSnapshot ) {
95
+ const request = this . createSnapshotActionRequest ( "UNLOCK" , takeSnapshot ) ;
96
+ conceptSetService . invokeConceptSetSnapshotAction ( this . currentConceptSetId ( ) , request )
97
+ . then ( ( ) => {
98
+ this . isLocked ( false ) ;
99
+ this . snapshotDescriptionMessage ( "" ) ;
100
+ this . isModalShown ( false ) ;
101
+ console . log ( "Concept set unlocked" ) ;
102
+ } )
103
+ . catch ( error => console . error ( `Error unlocking concept set: ${ error } ` ) ) ;
104
+ }
105
+ }
106
+
107
+ return commonUtils . build ( 'snapshot-lock-modal' , SnapshotLockModal , view ) ;
108
+ } ) ;
0 commit comments