Skip to content

Commit 163c07d

Browse files
authored
v0.4.1
* fix(orderedReducer): completely remove doc on delete instead of setting it to `null` * fix(examples): update basic example to `react` `^16.3.0` by making a simple `withFirestore` util * feat(core): add typescript definition for firestoreReducer - #78
2 parents e1b7387 + 8409aa7 commit 163c07d

File tree

9 files changed

+1238
-418
lines changed

9 files changed

+1238
-418
lines changed

examples/basic/package-lock.json

+1,182-387
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/basic/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"firebase": "^4.5.0",
6+
"firebase": "^4.12.0",
77
"lodash": "^4.17.5",
8-
"react": "^15.5.4",
9-
"react-dom": "^15.5.4",
8+
"react": "^16.3.0",
9+
"react-dom": "^16.3.0",
1010
"react-redux": "^5.0.6",
1111
"react-scripts": "1.0.14",
1212
"recompose": "^0.25.1",

examples/basic/src/Todo.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import PropTypes from 'prop-types'
3-
import { compose, flattenProp, withHandlers } from 'recompose'
4-
import { withStore } from './utils';
3+
import { compose, flattenProp, withHandlers, pure } from 'recompose'
4+
import { withFirestore } from './utils';
55

66
const styles = {
77
container: {
@@ -28,7 +28,7 @@ const Todo = ({
2828
}) => (
2929
<div style={styles.container}>
3030
<input
31-
value={done}
31+
checked={done}
3232
onChange={onDoneClick}
3333
disabled={disabled}
3434
type="checkbox"
@@ -46,21 +46,24 @@ Todo.propTypes = {
4646
done: PropTypes.bool, // from enhancer (flattenProp)
4747
disabled: PropTypes.bool, // from enhancer (flattenProp)
4848
onDoneClick: PropTypes.func.isRequired, // from enhancer (withHandlers)
49-
store: PropTypes.shape({
50-
firestore: PropTypes.object
49+
firestore: PropTypes.shape({
50+
update: PropTypes.func.isRequired
5151
})
5252
}
5353

5454
const enhance = compose(
5555
// Add props.firestore
56-
withStore,
57-
// Flatten todo (creates props.text, props.owner, props.isDone )
56+
withFirestore,
57+
// Flatten todo prop (creates id, text, owner, done and disabled props)
5858
flattenProp('todo'),
59-
// Handler functions as props
59+
// Handlers as props
6060
withHandlers({
61-
onDoneClick: props => () =>
62-
props.store.firestore.update(`todos/${props.todo.id}`, { done: !props.done })
63-
})
61+
onDoneClick: props => () => {
62+
return props.firestore.update(`todos/${props.id}`, { done: !props.done })
63+
}
64+
}),
65+
// Prevent unnessesary re-renders by doing shallow comparison of props
66+
pure
6467
)
6568

6669
export default enhance(Todo)

examples/basic/src/Todos.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types'
33
import { compose } from 'redux';
44
import { withHandlers, lifecycle } from 'recompose'
55
import { connect } from 'react-redux';
6-
import { withStore } from './utils';
6+
import { withFirestore } from './utils';
77
import Todo from './Todo';
88
import NewTodo from './NewTodo';
99

@@ -16,7 +16,7 @@ const Todos = ({ todos, onNewSubmit, onDoneClick }) => (
1616
: !todos.length
1717
? <span>No todos found</span>
1818
:
19-
todos.filter(todo => !!todo).map((todo, i) => (
19+
todos.map((todo, i) => (
2020
<Todo
2121
key={`${todo.id}-${i}`}
2222
todo={todo}
@@ -37,19 +37,19 @@ Todos.propTypes = {
3737
// Create HOC that loads data and adds it as todos prop
3838
const enhance = compose(
3939
// add redux store (from react context) as a prop
40-
withStore,
40+
withFirestore,
4141
// Handler functions as props
4242
withHandlers({
43-
loadData: props => err => props.store.firestore.setListener({
43+
loadData: props => err => props.firestore.setListener({
4444
collection: 'todos',
4545
orderBy: ['createdAt', 'desc'],
4646
limit: 10
4747
}),
4848
onNewSubmit: props => newTodo =>
49-
props.store.firestore.add('todos', {
49+
props.firestore.add('todos', {
5050
...newTodo,
5151
owner: 'Anonymous',
52-
createdAt: props.store.firestore.FieldValue.serverTimestamp()
52+
createdAt: props.firestore.FieldValue.serverTimestamp()
5353
}),
5454
}),
5555
// Run functionality on component lifecycle
@@ -59,7 +59,7 @@ const enhance = compose(
5959
this.props.loadData()
6060
},
6161
componentWillUnmount() {
62-
this.props.store.firestore.unsetListener({
62+
this.props.firestore.unsetListener({
6363
collection: 'todos',
6464
orderBy: ['createdAt', 'desc'],
6565
limit: 10

examples/basic/src/utils.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1+
import React, { Component } from 'react';
12
import PropTypes from 'prop-types'
2-
import { compose, withContext, getContext } from 'recompose'
33

44
// Create HOC that gets firestore from react context and passes it as a prop
5-
export const withStore = compose(
6-
withContext({ store: PropTypes.object }, () => {}),
7-
getContext({ store: PropTypes.object })
8-
)
5+
// NOTE: Modified version of withFirestore for a simple example. For a full
6+
// application, use react-redux-firebase's withFirestore: https://goo.gl/4pxmPv
7+
export const withFirestore = (WrappedComponent) => {
8+
class WithFirestore extends Component {
9+
static contextTypes = {
10+
store: PropTypes.object.isRequired
11+
}
12+
13+
render() {
14+
return (
15+
<WrappedComponent
16+
{...this.props}
17+
dispatch={this.context.store.dispatch}
18+
firestore={this.context.store.firestore}
19+
/>
20+
)
21+
}
22+
}
23+
// Note, for full statics support, use hoist-non-react-statics as done
24+
// in react-redux-firebase's withFirestore: https://goo.gl/4pxmPv
25+
return WithFirestore
26+
}

index.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ export function reduxFirestore(firebaseInstance: object, otherConfig: object): a
9292
*/
9393
export function getFirestore(firebaseInstance: object, otherConfig: object): any;
9494

95+
/**
96+
* A redux store reducer for Firestore state
97+
*/
98+
export function firestoreReducer(state: object, action: object): any;
9599

96100
/**
97101
* A redux store reducer for Firestore state

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-firestore",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"description": "Redux bindings for Firestore.",
55
"main": "lib/index.js",
66
"module": "es/index.js",

src/reducers/orderedReducer.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { size, get, unionBy } from 'lodash';
1+
import { size, get, unionBy, reject } from 'lodash';
22
import { merge as mergeObjects } from 'lodash/fp';
33
import { actionTypes } from '../constants';
44
import {
@@ -49,8 +49,8 @@ function modifyDoc(collectionState, action) {
4949
* @param {Object} action - The action that was dispatched
5050
* @return {Array} State with document modified
5151
*/
52-
function removeDoc(collectionState, action) {
53-
return updateItemInArray(collectionState, action.meta.doc, () => null);
52+
function removeDoc(array, action) {
53+
return reject(array, { id: action.meta.doc }); // returns a new array
5454
}
5555

5656
/**

0 commit comments

Comments
 (0)