1 | import { defaultConfig } from './constants';
|
2 | import createFirestoreInstance from './createFirestoreInstance';
|
3 |
|
4 | let firestoreInstance;
|
5 |
|
6 | /**
|
7 | * @name reduxFirestore
|
8 | * @external
|
9 | * @description Redux store enhancer that accepts configuration options and adds
|
10 | * store.firestore. Enhancers are most commonly placed in redux's `compose` call
|
11 | * along side applyMiddleware.
|
12 | * @property {Object} firebaseInstance - Initiated firebase instance
|
13 | * @property {Object} config - Containing react-redux-firebase specific configuration
|
14 | * @return {Function} That accepts a component and returns a Component which
|
15 | * wraps the provided component (higher order component).
|
16 | * @return {Function} That enhances a redux store with store.firestore
|
17 | * @example <caption>Setup</caption>
|
18 | * import { createStore, compose } from 'redux'
|
19 | * import { reduxFirestore } from 'redux-firestore'
|
20 | * import firebase from 'firebase' // must be 4.5.0 or higher
|
21 | import 'firebase/firestore' // make sure you add this for firestore
|
22 |
|
23 | * // Redux Firestore Config
|
24 | * const config = {
|
25 | * // here is where you place other config options
|
26 | * }
|
27 |
|
28 | * // initialize script from Firestore page
|
29 | * const fbConfg = {} // firebase config object
|
30 | * firebase.initializeApp(fbConfig)
|
31 | * firebase.firestore()
|
32 | *
|
33 | * // Add redux-firestore enhancer to store creation compose
|
34 | * // Note: In full projects this will often be within createStore.js or store.js
|
35 | const store = createStore(
|
36 | makeRootReducer(),
|
37 | initialState,
|
38 | compose(
|
39 | // pass firebase instance and config
|
40 | reduxFirestore(firebase, reduxConfig),
|
41 | // applyMiddleware(...middleware),
|
42 | // ...enhancers
|
43 | )
|
44 | )
|
45 | *
|
46 | * // Use Function later to create store
|
47 | * const store = createStoreWithFirestore(rootReducer, initialState)
|
48 | */
|
49 | export default function reduxFirestore(firebaseInstance, otherConfig) {
|
50 | return next => (reducer, initialState, middleware) => {
|
51 | const store = next(reducer, initialState, middleware);
|
52 |
|
53 | const configs = { ...defaultConfig, ...otherConfig };
|
54 | firestoreInstance = createFirestoreInstance(
|
55 | firebaseInstance.firebase_ || firebaseInstance, // eslint-disable-line no-underscore-dangle, no-undef, max-len
|
56 | configs,
|
57 | store.dispatch, // eslint-disable-line comma-dangle
|
58 | );
|
59 |
|
60 | store.firestore = firestoreInstance;
|
61 |
|
62 | return store;
|
63 | };
|
64 | }
|
65 |
|
66 | /**
|
67 | * @description Expose Firestore instance created internally. Useful for
|
68 | * integrations into external libraries such as redux-thunk and redux-observable.
|
69 | * @example <caption>redux-thunk integration</caption>
|
70 | * import { applyMiddleware, compose, createStore } from 'redux';
|
71 | * import thunk from 'redux-thunk';
|
72 | * import makeRootReducer from './reducers';
|
73 | * import { reduxFirestore, getFirestore } from 'redux-firestore';
|
74 | *
|
75 | * const fbConfig = {} // your firebase config
|
76 | *
|
77 | * const store = createStore(
|
78 | * makeRootReducer(),
|
79 | * initialState,
|
80 | * compose(
|
81 | * applyMiddleware([
|
82 | * // Pass getFirestore function as extra argument
|
83 | * thunk.withExtraArgument(getFirestore)
|
84 | * ]),
|
85 | * reduxFirestore(fbConfig)
|
86 | * )
|
87 | * );
|
88 | * // then later
|
89 | * export const addTodo = (newTodo) =>
|
90 | * (dispatch, getState, getFirestore) => {
|
91 | * const firebase = getFirestore()
|
92 | * firebase
|
93 | * .add('todos', newTodo)
|
94 | * .then(() => {
|
95 | * dispatch({ type: 'SOME_ACTION' })
|
96 | * })
|
97 | * };
|
98 | *
|
99 | */
|
100 | export const getFirestore = () => {
|
101 | // TODO: Handle recieveing config and creating firebase instance if it doesn't exist
|
102 | /* istanbul ignore next: Firebase instance always exists during tests */
|
103 | if (!firestoreInstance) {
|
104 | throw new Error(
|
105 | 'Firebase instance does not yet exist. Check your compose function.',
|
106 | );
|
107 | }
|
108 | // TODO: Create new firebase here with config passed in
|
109 | return firestoreInstance;
|
110 | };
|