@@ -83,7 +83,29 @@ const unWatchUserProfile = (firebase) => {
8383 firebase . _ . profileWatch = null
8484 }
8585}
86-
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+ }
87109/**
88110 * @description Watch user profile
89111 * @param {Function } dispatch - Action dispatch function
@@ -93,15 +115,33 @@ const watchUserProfile = (dispatch, firebase) => {
93115 const authUid = firebase . _ . authUid
94116 const userProfile = firebase . _ . config . userProfile
95117 unWatchUserProfile ( firebase )
118+
96119 if ( firebase . _ . config . userProfile ) {
97120 firebase . _ . profileWatch = firebase . database ( )
98121 . ref ( )
99122 . child ( `${ userProfile } /${ authUid } ` )
100123 . on ( 'value' , snap => {
101- dispatch ( {
102- type : SET_PROFILE ,
103- profile : snap . val ( )
104- } )
124+ const { profileParamsToPopulate } = firebase . _ . config
125+ if ( ! profileParamsToPopulate || ( ! isArray ( profileParamsToPopulate ) && ! isString ( profileParamsToPopulate ) ) ) {
126+ dispatch ( {
127+ type : SET_PROFILE ,
128+ profile : snap . val ( )
129+ } )
130+ } else {
131+ // Handle string and array for profileParamsToPopulate config option
132+ const paramsToPopulate = isArray ( firebase . _ . config . profileParamsToPopulate )
133+ ? firebase . _ . config . profileParamsToPopulate
134+ : firebase . _ . config . profileParamsToPopulate . split ( ',' )
135+
136+ // 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+ } )
143+ } )
144+ }
105145 } )
106146 }
107147}
@@ -244,6 +284,7 @@ export const login = (dispatch, firebase, credentials) => {
244284 {
245285 email : user . email ,
246286 displayName : user . providerData [ 0 ] . displayName || user . email ,
287+ avatarUrl : user . providerData [ 0 ] . photoURL ,
247288 providerData : user . providerData
248289 }
249290 )
0 commit comments