Skip to content
Next Next commit
Tests passing
  • Loading branch information
Scott Prue committed Jun 27, 2017
commit ab5ebaa42cb0389d6c54925312859579824ea4c4
2 changes: 1 addition & 1 deletion examples/complete/material/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"lodash": "^4.17.2",
"material-ui": "0.16.0",
"normalize.css": "^4.1.1",
"prop-types": "^15.5.8",
"prop-types": "^15.5.10",
"react": "15.3.2",
"react-dom": "15.3.2",
"react-google-button": "^0.1.0",
Expand Down
21 changes: 10 additions & 11 deletions examples/complete/material/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2947,7 +2947,7 @@ json-stable-stringify@^1.0.0, json-stable-stringify@^1.0.1:
dependencies:
jsonify "~0.0.0"

json-stringify-safe@^5.0.1, json-stringify-safe@~5.0.1:
json-stringify-safe@~5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"

Expand Down Expand Up @@ -3083,7 +3083,7 @@ loader-utils@^1.0.2:
emojis-list "^2.0.0"
json5 "^0.5.0"

lodash-es@^4.17.3, lodash-es@^4.17.4, lodash-es@^4.2.1:
lodash-es@^4.17.3, lodash-es@^4.2.1:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.4.tgz#dcc1d7552e150a0640073ba9cb31d70f032950e7"

Expand Down Expand Up @@ -3286,7 +3286,7 @@ longest@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097"

loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.2.0:
loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.2.0, loose-envify@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.3.1.tgz#d1a8ad33fa9ce0e713d65fdd0ac8b748d478c848"
dependencies:
Expand Down Expand Up @@ -4204,6 +4204,13 @@ [email protected], prop-types@^15.5.4, prop-types@^15.5.6:
dependencies:
fbjs "^0.8.9"

prop-types@^15.5.10:
version "15.5.10"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.5.10.tgz#2797dfc3126182e3a95e3dfbb2e893ddd7456154"
dependencies:
fbjs "^0.8.9"
loose-envify "^1.3.1"

proxy-addr@~1.1.3:
version "1.1.4"
resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-1.1.4.tgz#27e545f6960a44a627d9b44467e35c1b6b4ce2f3"
Expand Down Expand Up @@ -4494,14 +4501,6 @@ redux-logger@^3.0.6:
dependencies:
deep-diff "^0.3.5"

redux-persist@^4.8.0:
version "4.8.0"
resolved "https://registry.yarnpkg.com/redux-persist/-/redux-persist-4.8.0.tgz#17fd998949bdeef9275e4cf60ad5bbe1c73675fc"
dependencies:
json-stringify-safe "^5.0.1"
lodash "^4.17.4"
lodash-es "^4.17.4"

redux-thunk@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/redux-thunk/-/redux-thunk-2.2.0.tgz#e615a16e16b47a19a515766133d1e3e99b7852e5"
Expand Down
7 changes: 0 additions & 7 deletions src/actions/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ export const watchEvent = (firebase, dispatch, { type, path, populates, queryPar
if (snapshot.val() === null) {
dispatch({
type: NO_VALUE,
timestamp: Date.now(),
requesting: false,
requested: true,
path: storeAs || path
})
}
Expand All @@ -74,9 +71,6 @@ export const watchEvent = (firebase, dispatch, { type, path, populates, queryPar
const runQuery = (q, e, p, params) => {
dispatch({
type: START,
timestamp: Date.now(),
requesting: true,
requested: false,
path: storeAs || path
})

Expand Down Expand Up @@ -123,7 +117,6 @@ export const watchEvent = (firebase, dispatch, { type, path, populates, queryPar
path: storeAs || resultPath,
ordered: size(ordered) ? ordered : undefined,
data,
timestamp: Date.now(),
requesting: false,
requested: true
})
Expand Down
3 changes: 1 addition & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import {
defaultsDeep,
isString,
compact,
isFunction,
defaultsDeep
isFunction
} from 'lodash'
import { getPopulateObjs } from './utils/populate'

Expand Down
120 changes: 90 additions & 30 deletions src/reducer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { combineReducers } from 'redux'
import { set } from 'lodash'
import { set, get, has } from 'lodash'
import { actionTypes } from './constants'

const {
Expand All @@ -19,55 +19,109 @@ const {
} = actionTypes

const pathToArr = path => path ? path.split(/\//).filter(p => !!p) : []
const getSlashStrPath = path => pathToArr(path).join('/')
const getDotStrPath = path => pathToArr(path).join('.')

/**
* Reducer for requesting state. Changed by `START` and `SET` actions.
* @param {Object} state - Current requesting redux state
* @param {object} action - Object containing the action that was dispatched
* @param {Object} action - Object containing the action that was dispatched
* @return {Object} Profile state after reduction
*/
const requestingReducer = (state = {}, action) => {
const { path, requesting } = action
switch (action.type) {
const requestingReducer = (state = {}, { type, path }) => {
switch (type) {
case START:
return {
...state,
[getSlashStrPath(path)]: true
}
case NO_VALUE:
case SET:
return {
...state,
[pathToArr(path).join('/')]: requesting
[getSlashStrPath(path)]: false
}
// TODO: Handle NO_VALUE case
// case NO_VALUE:
default:
return state
}
}

const getPathStr = (path) => path ? path.replace('/', '.') : ''
/**
* Reducer for requested state. Changed by `START` `NO_VALUE`, and `SET` actions.
* @param {Object} state - Current requesting redux state
* @param {Object} action - Object containing the action that was dispatched
* @return {Object} Profile state after reduction
*/
const requestedReducer = (state = {}, { type, path }) => {
switch (type) {
case START:
return {
...state,
[getSlashStrPath(path)]: false
}
case NO_VALUE:
case SET:
return {
...state,
[getSlashStrPath(path)]: true
}
default:
return state
}
}

/**
* Reducer for timestamps state. Changed by `START` and `SET` actions.
* @param {Object} state - Current requesting redux state
* @param {Object} action - Object containing the action that was dispatched
* @return {Object} Profile state after reduction
*/
const timestampsReducer = (state = {}, { type, path }) => {
switch (type) {
case START:
case NO_VALUE:
case SET:
return {
...state,
[getSlashStrPath(path)]: Date.now()
}
default:
return state
}
}

/**
* Reducer for data state. Changed by `LOGIN`, `LOGOUT`, and `LOGIN_ERROR`
* actions.
* @param {Object} state - Current data redux state
* @param {object} action - Object containing the action that was dispatched
* @param {Object} action - Object containing the action that was dispatched
* @return {Object} Profile state after reduction
*/
const dataReducer = (state = {}, action) => {
const { path, data, ordered } = action
switch (action.type) {
const dataReducer = (state = {}, { type, path, data, ordered }) => {
switch (type) {
case SET:
return {
...state,
...set({}, getPathStr(path), data)
if (!state) {
return set({}, getDotStrPath(path), data)
}
case SET_ORDERED:
return {
...state,
...set({}, getPathStr(path), ordered)
...set({}, getDotStrPath(path), data)
}
// case SET_ORDERED:
// if (!state) {
// return set({}, getDotStrPath(path), ordered)
// }
// return {
// ...state,
// ...set({}, getDotStrPath(path), ordered)
// }
case NO_VALUE:
if (!state || !get(state, getDotStrPath(path))) {
return set({}, getDotStrPath(path), {})
}
return {
...state,
...set({}, getPathStr(path), {})
...set({}, getDotStrPath(path), {})
}
default:
return state
Expand All @@ -78,17 +132,17 @@ const dataReducer = (state = {}, action) => {
* Reducer for auth state. Changed by `LOGIN`, `LOGOUT`, and `LOGIN_ERROR`
* actions.
* @param {Object} state - Current auth redux state
* @param {object} action - Object containing the action that was dispatched
* @param {Object} action - Object containing the action that was dispatched
* @return {Object} Profile state after reduction
*/
const authReducer = (state = {}, action) => {
switch (action.type) {
const authReducer = (state = { isLoaded: false }, { type, auth }) => {
switch (type) {
case LOGIN:
case AUTH_UPDATE_SUCCESS:
return action.auth || undefined
return auth || state
case LOGOUT:
case LOGIN_ERROR:
return null
return { isLoaded: true }
default:
return state
}
Expand All @@ -101,16 +155,17 @@ const authReducer = (state = {}, action) => {
* @param {object} action - Object containing the action that was dispatched
* @return {Object} Profile state after reduction
*/
const profileReducer = (state = null, action) => {
switch (action.type) {
const profileReducer = (state = { isLoaded: false }, { type, profile }) => {
switch (type) {
case SET_PROFILE:
return {
...state,
...action.profile
...profile,
isLoaded: true
}
case LOGOUT:
case LOGIN_ERROR:
return null
return { isLoaded: true }
default:
return state
}
Expand Down Expand Up @@ -144,9 +199,12 @@ const isInitializingReducer = (state = false, action) => {
const errorsReducer = (state = [], action) => {
switch (action.type) {
case UNAUTHORIZED_ERROR:
return [...state, action.payload]
if (!state) {
return [action.authError]
}
return [...state, action.authError]
case LOGOUT:
return null
return []
default:
return state
}
Expand All @@ -166,6 +224,8 @@ const errorsReducer = (state = [], action) => {
*/
export default combineReducers({
requesting: requestingReducer,
requested: requestedReducer,
timestamps: timestampsReducer,
data: dataReducer,
auth: authReducer,
profile: profileReducer,
Expand Down
Loading