@@ -14,6 +14,7 @@ import Notification from 'dashboard/Data/Browser/Notification.react';
1414import TableView from 'dashboard/TableView.react' ;
1515import tableStyles from 'dashboard/TableView.scss' ;
1616import * as ViewPreferences from 'lib/ViewPreferences' ;
17+ import ViewPreferencesManager from 'lib/ViewPreferencesManager' ;
1718import generatePath from 'lib/generatePath' ;
1819import stringCompare from 'lib/stringCompare' ;
1920import { ActionTypes as SchemaActionTypes } from 'lib/stores/SchemaStore' ;
@@ -37,6 +38,7 @@ class Views extends TableView {
3738 this . section = 'Core' ;
3839 this . subsection = 'Views' ;
3940 this . _isMounted = false ;
41+ this . viewPreferencesManager = null ; // Will be initialized when context is available
4042 this . state = {
4143 views : [ ] ,
4244 counts : { } ,
@@ -83,49 +85,65 @@ class Views extends TableView {
8385 }
8486 }
8587
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+ }
122131 }
132+ } ) ;
133+ if ( this . _isMounted ) {
134+ this . loadData ( this . props . params . name ) ;
123135 }
124136 } ) ;
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+ }
129147 }
130148
131149 loadData ( name ) {
@@ -671,8 +689,15 @@ class Views extends TableView {
671689 onConfirm = { view => {
672690 this . setState (
673691 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+ }
676701 this . loadViews ( this . context ) ;
677702 }
678703 ) ;
@@ -699,8 +724,15 @@ class Views extends TableView {
699724 newViews [ state . editIndex ] = view ;
700725 return { editView : null , editIndex : null , views : newViews } ;
701726 } ,
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+ }
704736 this . loadViews ( this . context ) ;
705737 }
706738 ) ;
@@ -719,8 +751,15 @@ class Views extends TableView {
719751 const newViews = state . views . filter ( ( _ , i ) => i !== state . deleteIndex ) ;
720752 return { deleteIndex : null , views : newViews } ;
721753 } ,
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+ }
724763 if ( this . props . params . name === name ) {
725764 const path = generatePath ( this . context , 'views' ) ;
726765 this . props . navigate ( path ) ;
0 commit comments