1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6 |
|
7 | var React = require('react');
|
8 | var React__default = _interopDefault(React);
|
9 | var PropTypes = _interopDefault(require('prop-types'));
|
10 | var _inheritsLoose = _interopDefault(require('@babel/runtime/helpers/inheritsLoose'));
|
11 | var _extends = _interopDefault(require('@babel/runtime/helpers/extends'));
|
12 | var _objectWithoutPropertiesLoose = _interopDefault(require('@babel/runtime/helpers/objectWithoutPropertiesLoose'));
|
13 | var hoistStatics = _interopDefault(require('hoist-non-react-statics'));
|
14 |
|
15 | var FirestoreCache = function () {};
|
16 |
|
17 | var FirestoreContext = React__default.createContext(null);
|
18 |
|
19 | var FirestoreProvider =
|
20 |
|
21 | function (_Component) {
|
22 | _inheritsLoose(FirestoreProvider, _Component);
|
23 |
|
24 | function FirestoreProvider(props) {
|
25 | var _this = _Component.call(this, props) || this;
|
26 |
|
27 | var firebase = props.firebase,
|
28 | useTimestampsInSnapshots = props.useTimestampsInSnapshots;
|
29 | var firestore = firebase.firestore();
|
30 |
|
31 | if (typeof useTimestampsInSnapshots !== 'undefined') {
|
32 | firestore.settings({
|
33 | timestampsInSnapshots: useTimestampsInSnapshots
|
34 | });
|
35 | }
|
36 |
|
37 | _this.state = {
|
38 | firestoreDatabase: firestore,
|
39 | firestoreCache: new FirestoreCache()
|
40 | };
|
41 | return _this;
|
42 | }
|
43 |
|
44 | var _proto = FirestoreProvider.prototype;
|
45 |
|
46 | _proto.render = function render() {
|
47 | return React__default.createElement(FirestoreContext.Provider, {
|
48 | value: this.state
|
49 | }, this.props.children);
|
50 | };
|
51 |
|
52 | return FirestoreProvider;
|
53 | }(React.Component);
|
54 |
|
55 | FirestoreProvider.defaultProps = {};
|
56 | process.env.NODE_ENV !== "production" ? FirestoreProvider.propTypes = {
|
57 | firebase: PropTypes.object.isRequired,
|
58 | children: PropTypes.node.isRequired,
|
59 | useTimestampsInSnapshots: PropTypes.bool
|
60 | } : void 0;
|
61 |
|
62 | var Firestore = function (_ref) {
|
63 | var render = _ref.render;
|
64 | return React__default.createElement(FirestoreContext.Consumer, null, function (_ref2) {
|
65 | var firestoreDatabase = _ref2.firestoreDatabase;
|
66 | return render({
|
67 | firestore: firestoreDatabase
|
68 | });
|
69 | });
|
70 | };
|
71 |
|
72 | process.env.NODE_ENV !== "production" ? Firestore.propTypes = {
|
73 | render: PropTypes.func.isRequired
|
74 | } : void 0;
|
75 |
|
76 | var withFirestore = function (Component) {
|
77 | var C = function (_ref) {
|
78 | var wrappedComponentRef = _ref.wrappedComponentRef,
|
79 | remainingProps = _objectWithoutPropertiesLoose(_ref, ["wrappedComponentRef"]);
|
80 |
|
81 | return React__default.createElement(FirestoreContext.Consumer, null, function (value) {
|
82 | if (!value) {
|
83 | throw new Error('FirestoreProvider is missing');
|
84 | }
|
85 |
|
86 | var firestoreDatabase = value.firestoreDatabase;
|
87 | return React__default.createElement(Component, _extends({}, remainingProps, {
|
88 | firestore: firestoreDatabase,
|
89 | ref: wrappedComponentRef
|
90 | }));
|
91 | });
|
92 | };
|
93 |
|
94 | C.displayName = "withFirestore(" + (Component.displayName || Component.name) + ")";
|
95 | C.WrappedComponent = Component;
|
96 | process.env.NODE_ENV !== "production" ? C.propTypes = {
|
97 | wrappedComponentRef: PropTypes.func
|
98 | } : void 0;
|
99 | return hoistStatics(C, Component);
|
100 | };
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | function deepEqual(a, b) {
|
109 | if (Array.isArray(a) && Array.isArray(b)) {
|
110 | if (a.length !== b.length) {
|
111 | return false;
|
112 | }
|
113 |
|
114 | for (var i = 0; i < a.length; i++) {
|
115 | if (!deepEqual(a[i], b[i])) {
|
116 | return false;
|
117 | }
|
118 | }
|
119 |
|
120 | return true;
|
121 | } else {
|
122 | return a === b;
|
123 | }
|
124 | }
|
125 |
|
126 | var FirestoreCollection =
|
127 |
|
128 | function (_Component) {
|
129 | _inheritsLoose(FirestoreCollection, _Component);
|
130 |
|
131 | function FirestoreCollection() {
|
132 | var _this;
|
133 |
|
134 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
135 | args[_key] = arguments[_key];
|
136 | }
|
137 |
|
138 | _this = _Component.call.apply(_Component, [this].concat(args)) || this;
|
139 | _this.state = {
|
140 | isLoading: true,
|
141 | data: [],
|
142 | error: null,
|
143 | snapshot: null
|
144 | };
|
145 |
|
146 | _this.setupFirestoreListener = function () {
|
147 | var _this$props = _this.props,
|
148 | firestore = _this$props.firestore,
|
149 | path = _this$props.path,
|
150 | queryProps = _objectWithoutPropertiesLoose(_this$props, ["firestore", "path"]);
|
151 |
|
152 | var collectionRef = firestore.collection(path);
|
153 |
|
154 | var query = _this.buildQuery(collectionRef, queryProps);
|
155 |
|
156 | _this.unsubscribe = query.onSnapshot(_this.handleOnSnapshotSuccess, _this.handleOnSnapshotError);
|
157 | };
|
158 |
|
159 | _this.handleOnSnapshotSuccess = function (snapshot) {
|
160 | if (snapshot) {
|
161 | _this.setState({
|
162 | isLoading: false,
|
163 | data: snapshot.docs.map(function (doc) {
|
164 | return _extends({
|
165 | id: doc.id
|
166 | }, doc.data());
|
167 | }),
|
168 | error: null,
|
169 | snapshot: snapshot
|
170 | });
|
171 | }
|
172 | };
|
173 |
|
174 | _this.handleOnSnapshotError = function (error) {
|
175 | _this.setState({
|
176 | isLoading: false,
|
177 | data: [],
|
178 | error: error,
|
179 | snapshot: null
|
180 | });
|
181 | };
|
182 |
|
183 | _this.buildQuery = function (collectionRef, queryProps) {
|
184 | var sort = queryProps.sort,
|
185 | limit = queryProps.limit,
|
186 | filter = queryProps.filter;
|
187 | var query = collectionRef;
|
188 |
|
189 | if (sort) {
|
190 | sort.split(',').forEach(function (sortItem) {
|
191 | var _sortItem$split = sortItem.split(':'),
|
192 | field = _sortItem$split[0],
|
193 | order = _sortItem$split[1];
|
194 |
|
195 | query = query.orderBy(field, order);
|
196 | });
|
197 | }
|
198 |
|
199 | if (limit) {
|
200 | query = query.limit(limit);
|
201 | }
|
202 |
|
203 | if (filter) {
|
204 |
|
205 | if (Array.isArray(filter[0])) {
|
206 | filter.forEach(function (clause) {
|
207 | var _query;
|
208 |
|
209 | query = (_query = query).where.apply(_query, clause);
|
210 | });
|
211 | } else {
|
212 | var _query2;
|
213 |
|
214 |
|
215 | query = (_query2 = query).where.apply(_query2, filter);
|
216 | }
|
217 | }
|
218 |
|
219 | return query;
|
220 | };
|
221 |
|
222 | return _this;
|
223 | }
|
224 |
|
225 | var _proto = FirestoreCollection.prototype;
|
226 |
|
227 | _proto.componentDidMount = function componentDidMount() {
|
228 | this.setupFirestoreListener();
|
229 | };
|
230 |
|
231 | _proto.componentWillUnmount = function componentWillUnmount() {
|
232 | this.handleUnsubscribe();
|
233 | };
|
234 |
|
235 | _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
|
236 | var _this2 = this;
|
237 |
|
238 | if (nextProps.path !== this.props.path || nextProps.sort !== this.props.sort || nextProps.limit !== this.props.limit || !deepEqual(nextProps.filter, this.props.filter)) {
|
239 | this.handleUnsubscribe();
|
240 | this.setState({
|
241 | isLoading: true
|
242 | }, function () {
|
243 | return _this2.setupFirestoreListener();
|
244 | });
|
245 | }
|
246 | };
|
247 |
|
248 | _proto.handleUnsubscribe = function handleUnsubscribe() {
|
249 | if (this.unsubscribe) {
|
250 | this.unsubscribe();
|
251 | }
|
252 | };
|
253 |
|
254 | _proto.render = function () {
|
255 | var _this$props2 = this.props,
|
256 | children = _this$props2.children,
|
257 | render = _this$props2.render;
|
258 | if (render) return render(this.state);
|
259 | if (typeof children === 'function') return children(this.state);
|
260 | return null;
|
261 | };
|
262 |
|
263 | return FirestoreCollection;
|
264 | }(React.Component);
|
265 |
|
266 | process.env.NODE_ENV !== "production" ? FirestoreCollection.propTypes = {
|
267 | path: PropTypes.string.isRequired,
|
268 | sort: PropTypes.string,
|
269 | limit: PropTypes.number,
|
270 | filter: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.object])), PropTypes.arrayOf(PropTypes.array)]),
|
271 | children: PropTypes.func,
|
272 | render: PropTypes.func,
|
273 | firestore: PropTypes.object.isRequired
|
274 | } : void 0;
|
275 | var FirestoreCollection$1 = withFirestore(FirestoreCollection);
|
276 |
|
277 | var FirestoreDocument =
|
278 |
|
279 | function (_Component) {
|
280 | _inheritsLoose(FirestoreDocument, _Component);
|
281 |
|
282 | function FirestoreDocument() {
|
283 | var _this;
|
284 |
|
285 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
286 | args[_key] = arguments[_key];
|
287 | }
|
288 |
|
289 | _this = _Component.call.apply(_Component, [this].concat(args)) || this;
|
290 | _this.state = {
|
291 | isLoading: true,
|
292 | data: null,
|
293 | error: null,
|
294 | snapshot: null
|
295 | };
|
296 |
|
297 | _this.setupFirestoreListener = function () {
|
298 | var _this$props = _this.props,
|
299 | firestore = _this$props.firestore,
|
300 | path = _this$props.path;
|
301 | var documentRef = firestore.doc(path);
|
302 | _this.unsubscribe = documentRef.onSnapshot(_this.handleOnSnapshotSuccess, _this.handleOnSnapshotError);
|
303 | };
|
304 |
|
305 | _this.handleOnSnapshotError = function (error) {
|
306 | _this.setState({
|
307 | isLoading: false,
|
308 | error: error,
|
309 | data: null,
|
310 | snapshot: null
|
311 | });
|
312 | };
|
313 |
|
314 | _this.handleOnSnapshotSuccess = function (snapshot) {
|
315 | if (snapshot) {
|
316 | var newState = {
|
317 | isLoading: false,
|
318 | error: null,
|
319 | snapshot: snapshot
|
320 | };
|
321 |
|
322 | try {
|
323 | var documentData = snapshot.data();
|
324 | newState.data = _extends({
|
325 | id: snapshot.id
|
326 | }, documentData);
|
327 | } catch (error) {
|
328 | newState.error = error;
|
329 | }
|
330 |
|
331 | _this.setState(newState);
|
332 | }
|
333 | };
|
334 |
|
335 | return _this;
|
336 | }
|
337 |
|
338 | var _proto = FirestoreDocument.prototype;
|
339 |
|
340 | _proto.componentDidMount = function componentDidMount() {
|
341 | this.setupFirestoreListener();
|
342 | };
|
343 |
|
344 | _proto.componentWillUnmount = function componentWillUnmount() {
|
345 | this.handleUnsubscribe();
|
346 | };
|
347 |
|
348 | _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
|
349 | var _this2 = this;
|
350 |
|
351 | if (nextProps.path !== this.props.path) {
|
352 | this.handleUnsubscribe();
|
353 | this.setState({
|
354 | isLoading: true
|
355 | }, function () {
|
356 | return _this2.setupFirestoreListener();
|
357 | });
|
358 | }
|
359 | };
|
360 |
|
361 | _proto.handleUnsubscribe = function handleUnsubscribe() {
|
362 | if (this.unsubscribe) {
|
363 | this.unsubscribe();
|
364 | }
|
365 | };
|
366 |
|
367 | _proto.render = function () {
|
368 | var _this$props2 = this.props,
|
369 | children = _this$props2.children,
|
370 | render = _this$props2.render;
|
371 | if (render) return render(this.state);
|
372 | if (typeof children === 'function') return children(this.state);
|
373 | return null;
|
374 | };
|
375 |
|
376 | return FirestoreDocument;
|
377 | }(React.Component);
|
378 |
|
379 | process.env.NODE_ENV !== "production" ? FirestoreDocument.propTypes = {
|
380 | path: PropTypes.string.isRequired,
|
381 | children: PropTypes.func,
|
382 | render: PropTypes.func,
|
383 | firestore: PropTypes.object.isRequired
|
384 | } : void 0;
|
385 | var FirestoreDocument$1 = withFirestore(FirestoreDocument);
|
386 |
|
387 | exports.Firestore = Firestore;
|
388 | exports.FirestoreProvider = FirestoreProvider;
|
389 | exports.FirestoreCollection = FirestoreCollection$1;
|
390 | exports.FirestoreDocument = FirestoreDocument$1;
|
391 | exports.withFirestore = withFirestore;
|