-
-
Notifications
You must be signed in to change notification settings - Fork 553
Population of profile params #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { | ||
|
|
@@ -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) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) => { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
There was a problem hiding this comment.
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.