import { Action } from '@ngrx/store' ; import { AppState , AppActionTypes , NodeActionTypes , SetUserProfileAction , SetCurrentFolderAction , SetCurrentUrlAction , SetInitialStateAction , SetSelectedNodesAction , SetRepositoryInfoAction , SetInfoDrawerStateAction , SetInfoDrawerMetadataAspectAction , SetHeaderColorAction , SetCurrentNodeVersionAction , SetFileUploadingDialogAction , SetInfoDrawerPreviewStateAction } from '@alfresco/aca-shared/store' ; import { INITIAL_APP_STATE } from '../initial-state' ; export function appReducer ( state = INITIAL_APP_STATE , action ) { let newState ; switch ( action . type ) { case AppActionTypes . SetInitialState : newState = Object . assign ( { } , ( action as SetInitialStateAction ) . payload ) ; break ; case AppActionTypes . SetHeaderColor : newState = { ... state , headerColor : ( action as SetHeaderColorAction ) . color } ; break ; case NodeActionTypes . SetSelection : newState = updateSelectedNodes ( state , action as SetSelectedNodesAction ) ; break ; case AppActionTypes . SetUserProfile : newState = updateUser ( state , action as SetUserProfileAction ) ; break ; case AppActionTypes . SetCurrentFolder : newState = updateCurrentFolder ( state , action as SetCurrentFolderAction ) ; break ; case AppActionTypes . SetCurrentVersion : newState = updateCurrentNodeVersion ( state , action as SetCurrentNodeVersionAction ) ; break ; case AppActionTypes . SetCurrentUrl : newState = updateCurrentUrl ( state , action as SetCurrentUrlAction ) ; break ; case AppActionTypes . ToggleInfoDrawer : newState = toggleInfoDrawer ( state ) ; break ; case AppActionTypes . SetInfoDrawerState : newState = setInfoDrawer ( state , action as SetInfoDrawerStateAction ) ; break ; case AppActionTypes . SetInfoDrawerMetadataAspect : newState = setInfoDrawerAspect ( state , action as SetInfoDrawerMetadataAspectAction ) ; break ; case AppActionTypes . ToggleDocumentDisplayMode : newState = toggleDocumentDisplayMode ( state ) ; break ; case AppActionTypes . SetRepositoryInfo : newState = updateRepositoryStatus ( state , action as SetRepositoryInfoAction ) ; break ; case AppActionTypes . SetFileUploadingDialog : newState = setUploadDialogVisibility ( state , action as SetFileUploadingDialogAction ) ; break ; case AppActionTypes . ShowInfoDrawerPreview : newState = showInfoDrawerPreview ( state ) ; break ; case AppActionTypes . SetInfoDrawerPreviewState : newState = setInfoDrawerPreview ( state , action as SetInfoDrawerPreviewStateAction ) ; break ; default : newState = { ... state } ; } return newState ; } function updateUser ( state , action ) { const newState = { ... state } ; const user = action . payload . person ; const groups = [ ... ( action . payload . groups || [ ] ) ] ; const id = user . id ; const firstName = user . firstName || '' ; const lastName = user . lastName || '' ; const userName = `${ firstName } ${ lastName }` ; const initials = [ firstName [ 0 ] , lastName [ 0 ] ] . join ( '' ) ; const capabilities = user . capabilities ; const isAdmin = capabilities ? capabilities . isAdmin : true ; newState . user = { firstName , lastName , userName , initials , isAdmin , id , groups } ; return newState ; } function updateCurrentFolder ( state , action ) { const newState = { ... state } ; newState . navigation . currentFolder = action . payload ; return newState ; } function updateCurrentNodeVersion ( state , action ) { const newState = { ... state } ; newState . currentNodeVersion = action . payload ; return newState ; } function updateCurrentUrl ( state , action ) { const newState = { ... state } ; newState . navigation . url = action . payload ; return newState ; } function toggleInfoDrawer ( state ) { const newState = { ... state } ; let value = state . infoDrawerOpened ; if ( state . selection . isEmpty ) { value = false ; } else { value = ! value ; } newState . infoDrawerOpened = value ; return newState ; } function showInfoDrawerPreview ( state ) { const newState = { ... state } ; let value = state . infoDrawerPreview ; if ( state . selection . isEmpty ) { value = false ; } else { value = ! value ; } newState . infoDrawerPreview = value ; return newState ; } function toggleDocumentDisplayMode ( state ) { return { ... state , documentDisplayMode : state . documentDisplayMode === 'list' ? 'gallery' : 'list' } ; } function updateSelectedNodes ( state , action ) { const newState = { ... state } ; const nodes = [ ... action . payload ] ; const count = nodes . length ; const isEmpty = nodes . length === 0 ; let first = null ; let last = null ; let file = null ; let folder = null ; let library = null ; if ( nodes . length > 0 ) { first = nodes [ 0 ] ; last = nodes [ nodes . length - 1 ] ; if ( nodes . length === 1 ) { file = nodes . find ( ( entity ) => { return ! ! ( entity . entry . isFile || entity . entry . nodeId || entity . entry . sharedByUser ) ; } ) ; folder = nodes . find ( ( entity ) => entity . entry . isFolder ) ; } } const libraries = [ ... action . payload ] . filter ( ( node ) => node . isLibrary ) ; if ( libraries . length === 1 ) { library = libraries [ 0 ] as any ; } if ( isEmpty ) { newState . infoDrawerOpened = false ; } newState . selection = { count , nodes , isEmpty , first , last , file , folder , libraries , library } ; return newState ; } function setInfoDrawer ( state , action ) { return { ... state , infoDrawerOpened : action . payload } ; } function setInfoDrawerPreview ( state , action ) { return { ... state , infoDrawerPreview : action . payload } ; } function setInfoDrawerAspect ( state , action ) { return { ... state , infoDrawerMetadataAspect : action . payload } ; } function updateRepositoryStatus ( state , action ) { const newState = { ... state } ; newState . repository = action . payload ; return newState ; } function setUploadDialogVisibility ( state , action ) { const newState = { ... state } ; newState . fileUploadingDialog = action . payload ; return newState ; }