Browse Source

[RN] Make the calendar the default tab when there are calendar entries fetched.

master
Bettenbuk Zoltan 7 years ago
parent
commit
6a1e9e256d

+ 52
- 8
react/features/base/react/components/native/AbstractPagedList.js View File

55
     constructor(props: Props) {
55
     constructor(props: Props) {
56
         super(props);
56
         super(props);
57
 
57
 
58
-        // props.defaultPage may point to a non existing page if some of the
59
-        // pages are disabled.
60
-        const maxPageIndex
61
-            = props.pages.filter(page => page.component).length - 1;
62
-
63
         this.state = {
58
         this.state = {
64
-            pageIndex: Math.max(0, Math.min(maxPageIndex, props.defaultPage))
59
+            pageIndex: this._validatePageIndex(props.defaultPage)
65
         };
60
         };
66
     }
61
     }
67
 
62
 
63
+    /**
64
+     * Implements React {@code Component}'s componentWillReceiveProps.
65
+     *
66
+     * @inheritdoc
67
+     */
68
+    componentWillReceiveProps(newProps: Props) {
69
+        const { defaultPage } = newProps;
70
+
71
+        if (defaultPage !== this.props.defaultPage) {
72
+            // Default page changed due to a redux update. This is likely to
73
+            // happen after APP_WILL_MOUNT. So we update the active tab.
74
+            this._platformSpecificPageSelect(defaultPage);
75
+        }
76
+    }
77
+
68
     /**
78
     /**
69
      * Renders the component.
79
      * Renders the component.
70
      *
80
      *
95
         );
105
         );
96
     }
106
     }
97
 
107
 
108
+    _platformSpecificPageSelect: number => void
109
+
110
+    /**
111
+     * Method to be overriden by the components implementing this abstract class
112
+     * to handle platform specific actions on page select.
113
+     *
114
+     * @protected
115
+     * @param {number} pageIndex - The selected page index.
116
+     * @returns {void}
117
+     */
118
+    _platformSpecificPageSelect(pageIndex) {
119
+        this._selectPage(pageIndex);
120
+    }
121
+
98
     _renderPagedList: boolean => React$Node;
122
     _renderPagedList: boolean => React$Node;
99
 
123
 
100
     _selectPage: number => void;
124
     _selectPage: number => void;
107
      * @returns {void}
131
      * @returns {void}
108
      */
132
      */
109
     _selectPage(pageIndex: number) {
133
     _selectPage(pageIndex: number) {
134
+        const validatedPageIndex = this._validatePageIndex(pageIndex);
135
+
110
         this.setState({
136
         this.setState({
111
-            pageIndex
137
+            pageIndex: validatedPageIndex
112
         });
138
         });
113
 
139
 
114
         // The page's Component may have a refresh(dispatch) function which we
140
         // The page's Component may have a refresh(dispatch) function which we
115
         // invoke when the page is selected.
141
         // invoke when the page is selected.
116
-        const selectedPage = this.props.pages[pageIndex];
142
+        const selectedPage = this.props.pages[validatedPageIndex];
117
 
143
 
118
         if (selectedPage && selectedPage.component) {
144
         if (selectedPage && selectedPage.component) {
119
             const { refresh } = selectedPage.component;
145
             const { refresh } = selectedPage.component;
121
             typeof refresh === 'function' && refresh(this.props.dispatch);
147
             typeof refresh === 'function' && refresh(this.props.dispatch);
122
         }
148
         }
123
     }
149
     }
150
+
151
+    _validatePageIndex: number => number
152
+
153
+    /**
154
+     * Validates the requested page index and returns a safe value.
155
+     *
156
+     * @private
157
+     * @param {number} pageIndex - The requested page index.
158
+     * @returns {number}
159
+     */
160
+    _validatePageIndex(pageIndex) {
161
+        // pageIndex may point to a non existing page if some of the pages are
162
+        // disabled (their component property is undefined).
163
+        const maxPageIndex
164
+            = this.props.pages.filter(page => page.component).length - 1;
165
+
166
+        return Math.max(0, Math.min(maxPageIndex, pageIndex));
167
+    }
124
 }
168
 }

+ 12
- 0
react/features/base/react/components/native/PagedList.android.js View File

86
         }
86
         }
87
     }
87
     }
88
 
88
 
89
+    /**
90
+     * Platform specific actions to run on page select.
91
+     *
92
+     * @private
93
+     * @param {number} pageIndex - The selected page index.
94
+     * @returns {void}
95
+     */
96
+    _platformSpecificPageSelect(pageIndex) {
97
+        this._viewPager.setPage(pageIndex);
98
+        this._selectPage(pageIndex);
99
+    }
100
+
89
     /**
101
     /**
90
      * Renders a single page of the page list.
102
      * Renders a single page of the page list.
91
      *
103
      *

+ 1
- 3
react/features/calendar-sync/middleware.js View File

66
                     && store.dispatch(addKnownDomains(knownDomains));
66
                     && store.dispatch(addKnownDomains(knownDomains));
67
             }
67
             }
68
 
68
 
69
-            const result = next(action);
70
-
71
             _fetchCalendarEntries(store, false, false);
69
             _fetchCalendarEntries(store, false, false);
72
 
70
 
73
-            return result;
71
+            return next(action);
74
         }
72
         }
75
 
73
 
76
         case REFRESH_CALENDAR: {
74
         case REFRESH_CALENDAR: {

+ 27
- 3
react/features/welcome/components/WelcomePageLists.js View File

2
 
2
 
3
 import React, { Component } from 'react';
3
 import React, { Component } from 'react';
4
 import { Platform } from 'react-native';
4
 import { Platform } from 'react-native';
5
+import { connect } from 'react-redux';
5
 
6
 
6
 import { translate } from '../../base/i18n';
7
 import { translate } from '../../base/i18n';
7
 import { PagedList } from '../../base/react';
8
 import { PagedList } from '../../base/react';
10
 
11
 
11
 type Props = {
12
 type Props = {
12
 
13
 
14
+    /**
15
+     * True if the calendar feature has fetched entries, false otherwise
16
+     */
17
+    _hasCalendarEntries: boolean,
18
+
13
     /**
19
     /**
14
      * Renders the lists disabled.
20
      * Renders the lists disabled.
15
      */
21
      */
74
      * @inheritdoc
80
      * @inheritdoc
75
      */
81
      */
76
     render() {
82
     render() {
77
-        const { disabled } = this.props;
83
+        const { disabled, _hasCalendarEntries } = this.props;
78
 
84
 
79
         return (
85
         return (
80
             <PagedList
86
             <PagedList
81
-                defaultPage = { 0 }
87
+                defaultPage = { _hasCalendarEntries ? 1 : 0 }
82
                 disabled = { disabled }
88
                 disabled = { disabled }
83
                 pages = { this.pages } />
89
                 pages = { this.pages } />
84
         );
90
         );
85
     }
91
     }
86
 }
92
 }
87
 
93
 
88
-export default translate(WelcomePageLists);
94
+/**
95
+ * Maps (parts of) the redux state to the React {@code Component} props of
96
+ * {@code WelcomePageLists}.
97
+ *
98
+ * @param {Object} state - The redux state.
99
+ * @protected
100
+ * @returns {{
101
+ *     _hasCalendarEntries: boolean
102
+ * }}
103
+ */
104
+function _mapStateToProps(state: Object) {
105
+    const { events } = state['features/calendar-sync'];
106
+
107
+    return {
108
+        _hasCalendarEntries: Boolean(events && events.length)
109
+    };
110
+}
111
+
112
+export default translate(connect(_mapStateToProps)(WelcomePageLists));

Loading…
Cancel
Save