@@ -10,6 +10,7 @@ import {
1010} from '../constants'
1111import { capitalize , omit , isArray , isString , isFunction } from 'lodash'
1212import jwtDecode from 'jwt-decode'
13+ import { promisesForPopulate } from '../utils'
1314
1415/**
1516 * @description Dispatch login error action
@@ -83,29 +84,7 @@ const unWatchUserProfile = (firebase) => {
8384 firebase . _ . profileWatch = null
8485 }
8586}
86- const promisesForPopulate = ( firebase , profile , populateString ) => {
87- const paramToPopulate = populateString . split ( ':' ) [ 0 ]
88- const populateRoot = populateString . split ( ':' ) [ 1 ]
89- let idList = profile [ paramToPopulate ]
90- if ( isString ( profile [ paramToPopulate ] ) ) {
91- idList = profile [ paramToPopulate ] . split ( ',' )
92- }
93- return Promise . all (
94- idList . map ( itemId =>
95- firebase . database ( )
96- . ref ( )
97- . child ( populateRoot )
98- . child ( itemId )
99- . once ( 'value' )
100- . then ( snap => snap . val ( ) || itemId )
101- ) ) . then ( data => {
102- const populatedObj = { }
103- idList . forEach ( item => populatedObj )
104- populatedObj [ paramToPopulate ] = data
105- return populatedObj
106- }
107- )
108- }
87+
10988/**
11089 * @description Watch user profile
11190 * @param {Function } dispatch - Action dispatch function
@@ -134,13 +113,21 @@ const watchUserProfile = (dispatch, firebase) => {
134113 : firebase . _ . config . profileParamsToPopulate . split ( ',' )
135114
136115 // Convert each populate string in array into an array of once query promises
137- Promise . all ( paramsToPopulate . map ( p => promisesForPopulate ( firebase , snap . val ( ) , p ) ) )
138- . then ( ( data ) => {
139- dispatch ( {
140- type : SET_PROFILE ,
141- profile : Object . assign ( snap . val ( ) , data . reduce ( ( a , b ) => Object . assign ( a , b ) ) )
142- } )
116+ Promise . all (
117+ paramsToPopulate . map ( p =>
118+ promisesForPopulate ( firebase , snap . val ( ) , p )
119+ )
120+ )
121+ . then ( data => {
122+ // Dispatch action with profile combined with populated parameters
123+ dispatch ( {
124+ type : SET_PROFILE ,
125+ profile : Object . assign (
126+ snap . val ( ) , // profile
127+ data . reduce ( ( a , b ) => Object . assign ( a , b ) ) // populated profile parameters
128+ )
143129 } )
130+ } )
144131 }
145132 } )
146133 }
@@ -212,8 +199,9 @@ export const createUserProfile = (dispatch, firebase, userData, profile) =>
212199 . child ( `${ firebase . _ . config . userProfile } /${ userData . uid } ` )
213200 . once ( 'value' )
214201 . then ( profileSnap =>
202+ // update profile only if doesn't exist or if set by config
215203 ! firebase . _ . config . updateProfileOnLogin && profileSnap . val ( ) !== null
216- ? profile
204+ ? profileSnap . val ( )
217205 : profileSnap . ref . update ( profile ) // Update the profile
218206 . then ( ( ) => profile )
219207 . catch ( err => {
@@ -312,6 +300,7 @@ export const logout = (dispatch, firebase) => {
312300 dispatch ( { type : LOGOUT } )
313301 firebase . _ . authUid = null
314302 unWatchUserProfile ( firebase )
303+ return Promise . resolve ( firebase )
315304}
316305
317306/**
@@ -336,7 +325,7 @@ export const createUser = (dispatch, firebase, { email, password, signIn }, prof
336325 firebase . auth ( ) . currentUser || ( ! ! signIn && signIn === false )
337326 ? createUserProfile ( dispatch , firebase , userData , profile )
338327 : login ( dispatch , firebase , { email, password } )
339- . then ( ( ) => createUserProfile ( dispatch , firebase , userData , profile ) )
328+ . then ( ( ) => createUserProfile ( dispatch , firebase , userData , profile || { email } ) )
340329 . catch ( err => {
341330 if ( err ) {
342331 switch ( err . code ) {
0 commit comments