import firebase from 'src/firebase' ; import { User } from 'src/models/users' ; import { IUserAddress , PrivilegedUserInformation , } from 'src/models/users/privilegedInformation' ; import { deleteUserData as deleteUserDataFunc , getUserProfile as getUserProfileFunc , observePrivileged as observePrivilegedFunc , observeProfile as observeProfileFunc , setUserProfile as setUserProfileFunc , updateUserPrivilegedInformationData , updateUserProfileData , } from './functions' ; import { DELETE_ME , GET , IgetUserProfile , OBSERVE_PRIVILEGED , OBSERVE_PROFILE , SET , UPDATE , UPDATE_PRIVILEGED , } from './types' ; export const getUserProfile = ( payload ) => ( dispatch ) : void => { dispatch ( { type : GET , payload , firebase : getUserProfileFunc , } ) ; } ; export const observeProfile = ( dispatch , payload ) : Function => { dispatch ( { type : OBSERVE_PROFILE , observer : observeProfileFunc , payload , } ) ; return ( ) => dispatch ( { type : OBSERVE_PROFILE . UNSUBSCRIBE , observerName : OBSERVE_PROFILE , } ) ; } ; export const observePrivileged = ( dispatch , payload ) : Function => { dispatch ( { type : OBSERVE_PRIVILEGED , observer : observePrivilegedFunc , payload , } ) ; return ( ) => dispatch ( { type : OBSERVE_PRIVILEGED . UNSUBSCRIBE , observerName : OBSERVE_PRIVILEGED , } ) ; } ; export const setUserProfile = ( address , addressFromGoogle , termsAndPrivacyAccepted , displayName , uid , sendNotifications , displayPic ? ) => ( dispatch ) => { const privilegedPayload = PrivilegedUserInformation . factory ( { addressFromGoogle , address , termsAccepted : firebase . firestore . Timestamp . fromDate ( termsAndPrivacyAccepted ) , termsVersion : '1.0' , privacyAccepted : firebase . firestore . Timestamp . fromDate ( termsAndPrivacyAccepted ) , privacyVersion : '1.0' , sendNotifications , } ) ; const userPayload = User . factory ( { username : displayName , displayName , displayPicture : displayPic , } ) ; dispatch ( { type : SET , payload : { uid , privilegedPayload , userPayload , } , firebase : setUserProfileFunc , } ) ; } ; export const updateUserProfile = ( uid , user ) => ( dispatch ) => dispatch ( { type : UPDATE , payload : { uid , userPayload : user , } , firebase : updateUserProfileData , } ) ; export const updateUserPrivilegedInformation = ( uid , data ) => ( dispatch ) => dispatch ( { type : UPDATE_PRIVILEGED , payload : { uid , dataPayload : data , } , firebase : updateUserPrivilegedInformationData , } ) ; export const deleteUserData = ( ) => ( dispatch ) => dispatch ( { type : DELETE_ME , payload : { } , firebase : deleteUserDataFunc , } ) ;