import { firestore } from 'firebase' ; import { firestore as db } from 'src/firebase' ; import { User , UserFirestoreConverter } from 'src/models/users' ; import { PrivilegedUserInformation } from 'src/models/users/privilegedInformation' ; import createReducer from 'src/store/utils/createReducer' ; import { DELETE_ME , GET , OBSERVE_PRIVILEGED , OBSERVE_PROFILE , ProfileState , SET , UPDATE , UPDATE_PRIVILEGED , } from './types' ; const initialState = { profile : undefined , privilegedInformation : undefined , userRef : undefined , uid : undefined , loading : false , setAction : undefined , updateAction : undefined , observerReceivedFirstUpdate : false , error : undefined , } ; export default createReducer < ProfileState > ( { [ GET . PENDING ] : ( state ) => { state . loading = true ; state . error = undefined ; } , [ GET . COMPLETED ] : ( state , { payload , } ) => { state . profile = payload [ 0 ] . data ( ) ; state . privilegedInformation = payload [ 1 ] ?. data ( ) ; state . userRef = db . collection ( 'users' ) . doc ( payload [ 0 ] . id ) . withConverter ( UserFirestoreConverter ) ; state . uid = payload [ 0 ] . id ; state . loading = false ; state . error = undefined ; } , [ GET . REJECTED ] : ( state , { payload } ) => { state . error = payload ; state . loading = false ; state . profile = undefined ; state . uid = undefined ; } , [ SET . PENDING ] : ( state ) => { state . loading = true ; state . error = undefined ; } , [ SET . COMPLETED ] : ( state , { payload } ) => { state . error = undefined ; state . loading = false ; state . setAction = payload ; } , [ SET . REJECTED ] : ( state , { payload } ) => { state . loading = false ; state . error = payload ; state . setAction = undefined ; } , [ UPDATE . PENDING ] : ( state ) => { state . loading = true ; state . error = undefined ; } , [ UPDATE . COMPLETED ] : ( state ) => { state . error = undefined ; state . loading = false ; state . updateAction = true ; } , [ UPDATE . REJECTED ] : ( state , { payload } ) => { state . loading = false ; state . error = payload ; state . updateAction = undefined ; } , [ UPDATE_PRIVILEGED . PENDING ] : ( state ) => { state . loading = true ; state . error = undefined ; } , [ UPDATE_PRIVILEGED . COMPLETED ] : ( state ) => { state . error = undefined ; state . loading = false ; state . updateAction = true ; } , [ UPDATE_PRIVILEGED . REJECTED ] : ( state , { payload } ) => { state . loading = false ; state . error = payload ; state . updateAction = undefined ; } , [ DELETE_ME . PENDING ] : ( state ) => { state . loading = true ; state . error = undefined ; } , [ DELETE_ME . COMPLETED ] : ( state ) => { state . error = undefined ; state . loading = false ; state . updateAction = true ; window . location . href = '/' ; } , [ DELETE_ME . REJECTED ] : ( state , { payload } ) => { state . loading = false ; state . error = payload ; state . updateAction = undefined ; } , [ OBSERVE_PRIVILEGED . SUBSCRIBE ] : ( state ) => { state . loading = true ; } , [ OBSERVE_PRIVILEGED . UPDATED ] : ( state , { payload , } ) => { state . privilegedInformation = payload . data ( ) ; state . loading = false ; state . observerReceivedFirstUpdate = true ; } , [ OBSERVE_PROFILE . SUBSCRIBE ] : ( state ) => { state . loading = true ; } , [ OBSERVE_PROFILE . UPDATED ] : ( state , { payload , } ) => { state . profile = payload . data ( ) ; state . userRef = db . collection ( 'users' ) . doc ( payload . id ) . withConverter ( UserFirestoreConverter ) ; state . uid = payload . id ; state . loading = false ; state . observerReceivedFirstUpdate = true ; } , } , initialState ) ;