@@ -14,6 +14,7 @@ import Notification from 'dashboard/Data/Browser/Notification.react';
14
14
import TableView from 'dashboard/TableView.react' ;
15
15
import tableStyles from 'dashboard/TableView.scss' ;
16
16
import * as ViewPreferences from 'lib/ViewPreferences' ;
17
+ import ViewPreferencesManager from 'lib/ViewPreferencesManager' ;
17
18
import generatePath from 'lib/generatePath' ;
18
19
import stringCompare from 'lib/stringCompare' ;
19
20
import { ActionTypes as SchemaActionTypes } from 'lib/stores/SchemaStore' ;
@@ -37,6 +38,7 @@ class Views extends TableView {
37
38
this . section = 'Core' ;
38
39
this . subsection = 'Views' ;
39
40
this . _isMounted = false ;
41
+ this . viewPreferencesManager = null ; // Will be initialized when context is available
40
42
this . state = {
41
43
views : [ ] ,
42
44
counts : { } ,
@@ -83,49 +85,65 @@ class Views extends TableView {
83
85
}
84
86
}
85
87
86
- loadViews ( app ) {
87
- const views = ViewPreferences . getViews ( app . applicationId ) ;
88
- this . setState ( { views, counts : { } } , ( ) => {
89
- views . forEach ( view => {
90
- if ( view . showCounter ) {
91
- if ( view . cloudFunction ) {
92
- // For Cloud Function views, call the function to get count
93
- Parse . Cloud . run ( view . cloudFunction , { } , { useMasterKey : true } )
94
- . then ( res => {
95
- if ( this . _isMounted ) {
96
- this . setState ( ( { counts } ) => ( {
97
- counts : { ...counts , [ view . name ] : Array . isArray ( res ) ? res . length : 0 } ,
98
- } ) ) ;
99
- }
100
- } )
101
- . catch ( error => {
102
- if ( this . _isMounted ) {
103
- this . showNote ( `Request failed: ${ error . message || 'Unknown error occurred' } ` , true ) ;
104
- }
105
- } ) ;
106
- } else if ( view . query && Array . isArray ( view . query ) ) {
107
- // For aggregation pipeline views, use existing logic
108
- new Parse . Query ( view . className )
109
- . aggregate ( view . query , { useMasterKey : true } )
110
- . then ( res => {
111
- if ( this . _isMounted ) {
112
- this . setState ( ( { counts } ) => ( {
113
- counts : { ...counts , [ view . name ] : res . length } ,
114
- } ) ) ;
115
- }
116
- } )
117
- . catch ( error => {
118
- if ( this . _isMounted ) {
119
- this . showNote ( `Request failed: ${ error . message || 'Unknown error occurred' } ` , true ) ;
120
- }
121
- } ) ;
88
+ async loadViews ( app ) {
89
+ // Initialize ViewPreferencesManager if not already done or if app changed
90
+ if ( ! this . viewPreferencesManager || this . viewPreferencesManager . app !== app ) {
91
+ this . viewPreferencesManager = new ViewPreferencesManager ( app ) ;
92
+ }
93
+
94
+ try {
95
+ const views = await this . viewPreferencesManager . getViews ( app . applicationId ) ;
96
+ this . setState ( { views, counts : { } } , ( ) => {
97
+ views . forEach ( view => {
98
+ if ( view . showCounter ) {
99
+ if ( view . cloudFunction ) {
100
+ // For Cloud Function views, call the function to get count
101
+ Parse . Cloud . run ( view . cloudFunction , { } , { useMasterKey : true } )
102
+ . then ( res => {
103
+ if ( this . _isMounted ) {
104
+ this . setState ( ( { counts } ) => ( {
105
+ counts : { ...counts , [ view . name ] : Array . isArray ( res ) ? res . length : 0 } ,
106
+ } ) ) ;
107
+ }
108
+ } )
109
+ . catch ( error => {
110
+ if ( this . _isMounted ) {
111
+ this . showNote ( `Request failed: ${ error . message || 'Unknown error occurred' } ` , true ) ;
112
+ }
113
+ } ) ;
114
+ } else if ( view . query && Array . isArray ( view . query ) ) {
115
+ // For aggregation pipeline views, use existing logic
116
+ new Parse . Query ( view . className )
117
+ . aggregate ( view . query , { useMasterKey : true } )
118
+ . then ( res => {
119
+ if ( this . _isMounted ) {
120
+ this . setState ( ( { counts } ) => ( {
121
+ counts : { ...counts , [ view . name ] : res . length } ,
122
+ } ) ) ;
123
+ }
124
+ } )
125
+ . catch ( error => {
126
+ if ( this . _isMounted ) {
127
+ this . showNote ( `Request failed: ${ error . message || 'Unknown error occurred' } ` , true ) ;
128
+ }
129
+ } ) ;
130
+ }
122
131
}
132
+ } ) ;
133
+ if ( this . _isMounted ) {
134
+ this . loadData ( this . props . params . name ) ;
123
135
}
124
136
} ) ;
125
- if ( this . _isMounted ) {
126
- this . loadData ( this . props . params . name ) ;
127
- }
128
- } ) ;
137
+ } catch ( error ) {
138
+ console . error ( 'Failed to load views from server, falling back to local storage:' , error ) ;
139
+ // Fallback to local storage
140
+ const views = ViewPreferences . getViews ( app . applicationId ) ;
141
+ this . setState ( { views, counts : { } } , ( ) => {
142
+ if ( this . _isMounted ) {
143
+ this . loadData ( this . props . params . name ) ;
144
+ }
145
+ } ) ;
146
+ }
129
147
}
130
148
131
149
loadData ( name ) {
@@ -671,8 +689,15 @@ class Views extends TableView {
671
689
onConfirm = { view => {
672
690
this . setState (
673
691
state => ( { showCreate : false , views : [ ...state . views , view ] } ) ,
674
- ( ) => {
675
- ViewPreferences . saveViews ( this . context . applicationId , this . state . views ) ;
692
+ async ( ) => {
693
+ if ( this . viewPreferencesManager ) {
694
+ try {
695
+ await this . viewPreferencesManager . saveViews ( this . context . applicationId , this . state . views ) ;
696
+ } catch ( error ) {
697
+ console . error ( 'Failed to save views:' , error ) ;
698
+ this . showNote ( 'Failed to save view changes' , true ) ;
699
+ }
700
+ }
676
701
this . loadViews ( this . context ) ;
677
702
}
678
703
) ;
@@ -699,8 +724,15 @@ class Views extends TableView {
699
724
newViews [ state . editIndex ] = view ;
700
725
return { editView : null , editIndex : null , views : newViews } ;
701
726
} ,
702
- ( ) => {
703
- ViewPreferences . saveViews ( this . context . applicationId , this . state . views ) ;
727
+ async ( ) => {
728
+ if ( this . viewPreferencesManager ) {
729
+ try {
730
+ await this . viewPreferencesManager . saveViews ( this . context . applicationId , this . state . views ) ;
731
+ } catch ( error ) {
732
+ console . error ( 'Failed to save views:' , error ) ;
733
+ this . showNote ( 'Failed to save view changes' , true ) ;
734
+ }
735
+ }
704
736
this . loadViews ( this . context ) ;
705
737
}
706
738
) ;
@@ -719,8 +751,15 @@ class Views extends TableView {
719
751
const newViews = state . views . filter ( ( _ , i ) => i !== state . deleteIndex ) ;
720
752
return { deleteIndex : null , views : newViews } ;
721
753
} ,
722
- ( ) => {
723
- ViewPreferences . saveViews ( this . context . applicationId , this . state . views ) ;
754
+ async ( ) => {
755
+ if ( this . viewPreferencesManager ) {
756
+ try {
757
+ await this . viewPreferencesManager . saveViews ( this . context . applicationId , this . state . views ) ;
758
+ } catch ( error ) {
759
+ console . error ( 'Failed to save views:' , error ) ;
760
+ this . showNote ( 'Failed to save view changes' , true ) ;
761
+ }
762
+ }
724
763
if ( this . props . params . name === name ) {
725
764
const path = generatePath ( this . context , 'views' ) ;
726
765
this . props . navigate ( path ) ;
0 commit comments