Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"build:umd": "cross-env BABEL_ENV=commonjs NODE_ENV=development webpack src/index.js dist/react-redux-firebase.js",
"build:umd:min": "cross-env BABEL_ENV=commonjs NODE_ENV=production webpack src/index.js dist/react-redux-firebase.min.js",
"build": "npm run build:commonjs && npm run build:es && npm run build:umd && npm run build:umd:min",
"watch": "npm run build:commonjs -- --watch",
"watch": "npm run build:umd -- --watch",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. This is the more often use-case since most are requiring and using Webpack or some other bundler that points to the UMD version.

"prepublish": "npm run clean && npm run build",
"prepush": "npm run lint:fix",
"docs:clean": "rimraf _book",
Expand Down
52 changes: 49 additions & 3 deletions src/actions/auth.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import { omit, isArray, isString, isFunction, forEach, set, get } from 'lodash'
import {
omit,
isArray,
isString,
isFunction,
forEach,
set,
get,
map,
mapValues
} from 'lodash'
import jwtDecode from 'jwt-decode'
import { actionTypes, defaultJWTProps } from '../constants'
import { promisesForPopulate, getPopulateObjs } from '../utils/populate'
import {
promisesForPopulate,
getPopulateObjs,
getChildType
} from '../utils/populate'
import { getLoginMethodAndParams } from '../utils/auth'

const {
Expand Down Expand Up @@ -105,7 +119,39 @@ export const watchUserProfile = (dispatch, firebase) => {
const populates = getPopulateObjs(profileParamsToPopulate)
const profile = snap.val()
forEach(populates, (p) => {
set(profile, p.child, get(data, `${p.root}.${snap.val()[p.child]}`))
const child = get(profile, p.child)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of get here, this will keep it so that dot notation can be used for child parameter of populate objects.

const childType = getChildType(child)
let populatedChild

switch (childType) {

case 'object':
populatedChild = mapValues(
child,
(value, key) => {
if (value) { // Only populate keys with truthy values
return get(data, `${p.root}.${key}`)
}
return value
})
break

case 'string':
populatedChild = get(data, `${p.root}.${child}`)
break

case 'array':
populatedChild = map(
child,
(key) => get(data, `${p.root}.${key}`)
)
break

default:
populatedChild = child
}
// Overwrite the child value with the populated child
set(profile, p.child, populatedChild)
})
dispatch({
type: SET_PROFILE,
Expand Down
18 changes: 18 additions & 0 deletions src/utils/populate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ export const getPopulateObj = (str) => {
// TODO: Handle childParam
return { child: strArray[0], root: strArray[1] }
}
/**
* @private
* @description Determine the structure of the child parameter to populate onto
* @param {String|Object} child - Value at child parameter
*/
export const getChildType = (child) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great for now, but eventually we might want to make this more general (return type of all types of vars) to be used elsewhere. Wish there was a lodash function to just return a variable's type but it seems like they feel as though developers should make this themselves.

if (isString(child)) {
return 'string'
}
if (isArray(child)) {
return 'array'
}
if (isObject(child)) {
return 'object'
}
return 'other'
}

/**
* @private
* @description Create standardized populate object from strings or objects
Expand Down