import { Event , UserSection } from '@client/forms' ; import { APPLICATION_DETAIL , CERTIFICATE_COLLECTOR , CREATE_USER , CREATE_USER_ON_LOCATION , CREATE_USER_SECTION , DRAFT_BIRTH_APPLICANT_FORM , DRAFT_BIRTH_PARENT_FORM , DRAFT_DEATH_FORM , EVENT_INFO , EVENT_REGISTRATION_RATES , FIELD_AGENT_HOME_TAB , HOME , OPERATIONAL_REPORT , PERFORMANCE_FIELD_AGENT_LIST , PERFORMANCE_HOME , PERFORMANCE_REPORT , PERFORMANCE_REPORT_LIST , PRINT_CERTIFICATE_PAYMENT , REGISTRAR_HOME_TAB , REVIEW_CERTIFICATE , REVIEW_DUPLICATES , REVIEW_USER_DETAILS , REVIEW_USER_FORM , SEARCH , SEARCH_RESULT , SELECT_BIRTH_INFORMANT , SELECT_BIRTH_MAIN_CONTACT_POINT , SELECT_BIRTH_PRIMARY_APPLICANT , SELECT_DEATH_INFORMANT , SELECT_DEATH_MAIN_CONTACT_POINT , SELECT_VITAL_EVENT , SETTINGS , SYS_ADMIN_HOME_TAB , TEAM_SEARCH , VERIFY_COLLECTOR , WORKFLOW_STATUS , TEAM_USER_LIST , USER_PROFILE , CONFIG } from '@client/navigation/routes' ; import { getCurrentUserScope } from '@client/utils/authUtils' ; import { OPERATIONAL_REPORT_SECTION } from '@client/views/SysAdmin/Performance/OperationalReport' ; import { IStatusMapping } from '@client/views/SysAdmin/Performance/reports/operational/StatusWiseApplicationCountView' ; import { ISearchLocation } from '@opencrvs/components/lib/interface' ; import { goBack as back , push , replace } from 'connected-react-router' ; import moment from 'moment' ; import { stringify } from 'query-string' ; import { Cmd , loop } from 'redux-loop' ; export interface IDynamicValues { [ key ] : any ; } export function formatUrl ( url , props ) { const formattedUrl = Object . keys ( props ) . reduce ( ( str , key ) => str . replace ( `:${ key }` , props [ key ] ) , url ) ; return formattedUrl . endsWith ( '?' ) ? formattedUrl . slice ( 0 , - 1 ) : formattedUrl ; } export const GO_TO_PAGE = 'navigation/GO_TO_PAGE' ; type GoToPageAction = { type : typeof GO_TO_PAGE ; payload : { pageRoute : string ; applicationId : string ; pageId : string ; groupId ? : string ; event : string ; fieldNameHash ? : string ; historyState ? : IDynamicValues ; } ; } ; export const GO_TO_REGISTRAR_HOME = 'navigation/GO_TO_REGISTRAR_HOME' ; type GoToRegistrarHome = { type : typeof GO_TO_REGISTRAR_HOME ; payload : { tabId : string ; } ; } ; export const GO_TO_FIELD_AGENT_HOME = 'navigation/GO_TO_FIELD_AGENT_HOME' ; type GoToFieldAgentHome = { type : typeof GO_TO_FIELD_AGENT_HOME ; payload : { tabId : string ; } ; } ; export const GO_TO_REVIEW_USER_DETAILS = 'navigation/GO_TO_REVIEW_USER_DETAILS' ; type GoToReviewUserDetails = { type : typeof GO_TO_REVIEW_USER_DETAILS ; payload : { userId : string ; } ; } ; export const GO_TO_USER_PROFILE = 'navigation/GO_TO_USER_PROFILE' ; type GoToUserProfile = { type : typeof GO_TO_USER_PROFILE ; payload : { userId : string ; } ; } ; export type Action = GoToPageAction | GoToRegistrarHome | GoToFieldAgentHome | GoToSysAdminHome | GoToReviewUserDetails | GoToUserProfile ; export const GO_TO_SYS_ADMIN_HOME = 'navigation/GO_TO_SYS_ADMIN_HOME' ; type GoToSysAdminHome = { type : typeof GO_TO_SYS_ADMIN_HOME ; payload : { tabId : string ; } ; } ; export function goToBirthInformant ( applicationId ) { return push ( formatUrl ( SELECT_BIRTH_INFORMANT , { applicationId } ) ) ; } export function goToDeathInformant ( applicationId ) { return push ( formatUrl ( SELECT_DEATH_INFORMANT , { applicationId } ) ) ; } export function goToBirthContactPoint ( applicationId ) { return push ( formatUrl ( SELECT_BIRTH_MAIN_CONTACT_POINT , { applicationId } ) ) ; } export function goToEventInfo ( eventType ) { return push ( formatUrl ( EVENT_INFO , { eventType } ) ) ; } export function goToDeathContactPoint ( applicationId ) { return push ( formatUrl ( SELECT_DEATH_MAIN_CONTACT_POINT , { applicationId } ) ) ; } export function goToEvents ( ) { return push ( SELECT_VITAL_EVENT ) ; } export function goBack ( ) { return back ( ) ; } export function goToHome ( ) { return push ( HOME ) ; } export function goToConfig ( ) { return push ( CONFIG ) ; } export function goToHomeTab ( tabId , selectorId = '' ) { const path = getCurrentUserScope ( ) . includes ( 'declare' ) ? FIELD_AGENT_HOME_TAB : REGISTRAR_HOME_TAB ; return push ( formatUrl ( path , { tabId , selectorId } ) ) ; } type searchedLocation = { selectedLocation : ISearchLocation ; } ; export function goToTeamSearch ( searchedLocation ? ) { return searchedLocation && searchedLocation . selectedLocation ? push ( TEAM_SEARCH , { selectedLocation : searchedLocation . selectedLocation } ) : push ( TEAM_SEARCH ) ; } export function goToPerformanceHome ( state ? ) { return state && state . selectedLocation ? push ( PERFORMANCE_HOME , { selectedLocation : state . selectedLocation } ) : push ( PERFORMANCE_HOME ) ; } export function goToPerformanceReportList ( ) { return push ( PERFORMANCE_REPORT_LIST ) ; } export function goToPerformanceReport ( selectedLocation , eventType , timeStart , timeEnd ) { return push ( PERFORMANCE_REPORT , { selectedLocation , timeRange : { start : timeStart , end : timeEnd } , eventType } ) ; } export function goToOperationalReport ( locationId , sectionId = OPERATIONAL_REPORT_SECTION . OPERATIONAL , timeStart = moment ( ) . subtract ( 1 , 'years' ) . toDate ( ) , timeEnd = moment ( ) . toDate ( ) ) { return push ( { pathname : OPERATIONAL_REPORT , search : stringify ( { sectionId , locationId , timeStart : timeStart . toISOString ( ) , timeEnd : timeEnd . toISOString ( ) } ) } ) ; } export function goToTeamUserList ( selectedLocation , viewOnly ? ) { return push ( { pathname : TEAM_USER_LIST , search : stringify ( { locationId : selectedLocation . id , viewOnly } ) } ) ; } export function goToSearchResult ( searchText , searchType , mobile ? ) { return mobile ? replace ( formatUrl ( SEARCH_RESULT , { searchText , searchType } ) ) : push ( formatUrl ( SEARCH_RESULT , { searchText , searchType } ) ) ; } export function goToSearch ( ) { return push ( SEARCH ) ; } export function goToApplicationDetails ( applicationId , forceDetailsQuery ? ) { return push ( formatUrl ( APPLICATION_DETAIL , { applicationId } ) , { forceDetailsQuery } ) ; } export function goToBirthRegistrationAsParent ( applicationId ) { return push ( formatUrl ( DRAFT_BIRTH_PARENT_FORM , { applicationId : applicationId . toString ( ) } ) ) ; } export function goToApplicationContact ( informant ) { return push ( formatUrl ( DRAFT_BIRTH_APPLICANT_FORM , { informant : informant . toString ( ) } ) ) ; } export function goToPrimaryApplicant ( applicationId ) { return push ( formatUrl ( SELECT_BIRTH_PRIMARY_APPLICANT , { applicationId } ) ) ; } export function goToReviewDuplicate ( applicationId ) { return push ( formatUrl ( REVIEW_DUPLICATES , { applicationId : applicationId . toString ( ) } ) ) ; } export function goToPrintCertificate ( registrationId , event , groupId ? ) { return push ( formatUrl ( CERTIFICATE_COLLECTOR , { registrationId : registrationId . toString ( ) , eventType : event . toLowerCase ( ) . toString ( ) , groupId : groupId || 'certCollector' } ) ) ; } export function goToReviewCertificate ( registrationId , event ) { return push ( formatUrl ( REVIEW_CERTIFICATE , { registrationId : registrationId . toString ( ) , eventType : event } ) , { isNavigatedInsideApp : true } ) ; } export function goToVerifyCollector ( registrationId , event , collector ) { return push ( formatUrl ( VERIFY_COLLECTOR , { registrationId : registrationId . toString ( ) , eventType : event . toLowerCase ( ) . toString ( ) , collector : collector . toLowerCase ( ) . toString ( ) } ) ) ; } export function goToPrintCertificatePayment ( registrationId , event ) { return push ( formatUrl ( PRINT_CERTIFICATE_PAYMENT , { registrationId : registrationId . toString ( ) , eventType : event } ) ) ; } export function goToDeathRegistration ( applicationId ) { return push ( formatUrl ( DRAFT_DEATH_FORM , { applicationId : applicationId . toString ( ) } ) ) ; } export function goToRegistrarHomeTab ( tabId , selectorId ? ) { return { type : GO_TO_REGISTRAR_HOME , payload : { tabId , selectorId } } ; } export function goToFieldAgentHomeTab ( tabId ) { return { type : GO_TO_FIELD_AGENT_HOME , payload : { tabId } } ; } export function goToSysAdminHomeTab ( tabId ) { return { type : GO_TO_SYS_ADMIN_HOME , payload : { tabId } } ; } export function goToSettings ( ) { return push ( SETTINGS ) ; } export function goToCreateNewUser ( ) { return push ( CREATE_USER ) ; } export function goToCreateNewUserWithLocationId ( locationId ) { return push ( formatUrl ( CREATE_USER_ON_LOCATION , { locationId } ) ) ; } export function goToRegistrationRates ( eventType , title , locationId , timeStart , timeEnd ) { return push ( { pathname : formatUrl ( EVENT_REGISTRATION_RATES , { eventType } ) , search : stringify ( { locationId , title , timeStart : timeStart . toISOString ( ) , timeEnd : timeEnd . toISOString ( ) } ) } ) ; } export function goToFieldAgentList ( locationId , timeStart , timeEnd , event ? ) { return push ( { pathname : PERFORMANCE_FIELD_AGENT_LIST , search : stringify ( { locationId , timeStart , timeEnd } ) } ) ; } export function goToWorkflowStatus ( sectionId , locationId , timeStart , timeEnd , status ? , event ? ) { return push ( { pathname : WORKFLOW_STATUS , search : stringify ( { locationId , status , event } ) , state : { sectionId , timeStart , timeEnd } } ) ; } export function goToReviewUserDetails ( userId ) { return { type : GO_TO_REVIEW_USER_DETAILS , payload : { userId } } ; } export function goToUserProfile ( userId ) { return { type : GO_TO_USER_PROFILE , payload : { userId } } ; } export const GO_TO_CREATE_USER_SECTION = 'navigation/GO_TO_CREATE_USER_SECTION' ; type GoToCreateUserSection = { type : typeof GO_TO_CREATE_USER_SECTION ; payload : { sectionId : string ; nextGroupId : string ; userFormFieldNameHash ? : string ; formHistoryState ? : IDynamicValues ; } ; } ; export const GO_TO_USER_REVIEW_FORM = 'navigation/GO_TO_USER_REVIEW_FORM' ; type GoToUserReviewForm = { type : typeof GO_TO_USER_REVIEW_FORM ; payload : { userId : string ; sectionId : string ; nextGroupId : string ; userFormFieldNameHash ? : string ; formHistoryState ? : IDynamicValues ; } ; } ; export function goToCreateUserSection ( sectionId , nextGroupId , fieldNameHash ? , historyState ? ) { return { type : GO_TO_CREATE_USER_SECTION , payload : { sectionId , nextGroupId , userFormFieldNameHash : fieldNameHash , formHistoryState : historyState } } ; } export function goToUserReviewForm ( userId , sectionId , nextGroupId , fieldNameHash ? , historyState ? ) { return { type : GO_TO_USER_REVIEW_FORM , payload : { userId , sectionId , nextGroupId , userFormFieldNameHash : fieldNameHash , formHistoryState : historyState } } ; } export function goToPageGroup ( pageRoute , applicationId , pageId , groupId , event , fieldNameHash ? , historyState ? ) { return { type : GO_TO_PAGE , payload : { applicationId , pageId , groupId , event , fieldNameHash , pageRoute , historyState } } ; } export function goToPage ( pageRoute , applicationId , pageId , event , fieldNameHash ? , historyState ? ) { return { type : GO_TO_PAGE , payload : { applicationId , pageId , event , fieldNameHash , pageRoute , historyState } } ; } export type INavigationState = undefined ; export function navigationReducer ( state , action ) { switch ( action . type ) { case GO_TO_PAGE : const { fieldNameHash , applicationId , pageId , groupId , event , pageRoute , historyState } = action . payload ; return loop ( state , Cmd . action ( push ( formatUrl ( pageRoute , { applicationId : applicationId . toString ( ) , pageId , groupId , event } ) + ( fieldNameHash ? `#${ fieldNameHash }` : '' ) , historyState ) ) ) ; case GO_TO_REGISTRAR_HOME : const { tabId : RegistrarHomeTabId , selectorId : RegistrarHomeSelectorId = '' } = action . payload ; return loop ( state , Cmd . action ( push ( formatUrl ( REGISTRAR_HOME_TAB , { tabId : RegistrarHomeTabId , selectorId : RegistrarHomeSelectorId } ) ) ) ) ; case GO_TO_FIELD_AGENT_HOME : const { tabId : FieldAgentHomeTabId } = action . payload ; return loop ( state , Cmd . action ( push ( formatUrl ( FIELD_AGENT_HOME_TAB , { tabId : FieldAgentHomeTabId } ) ) ) ) ; case GO_TO_SYS_ADMIN_HOME : const { tabId : SysAdminHomeTabId } = action . payload ; return loop ( state , Cmd . action ( push ( formatUrl ( SYS_ADMIN_HOME_TAB , { tabId : SysAdminHomeTabId } ) ) ) ) ; case GO_TO_CREATE_USER_SECTION : const { sectionId , nextGroupId , userFormFieldNameHash , formHistoryState } = action . payload ; return loop ( state , Cmd . action ( push ( formatUrl ( CREATE_USER_SECTION , { sectionId , groupId : nextGroupId } ) + ( userFormFieldNameHash ? `#${ userFormFieldNameHash }` : '' ) , formHistoryState ) ) ) ; case GO_TO_USER_REVIEW_FORM : return loop ( state , Cmd . action ( push ( formatUrl ( REVIEW_USER_FORM , { userId : action . payload . userId , sectionId : action . payload . sectionId , groupId : action . payload . nextGroupId } ) + ( action . payload . userFormFieldNameHash ? `#${ action . payload . userFormFieldNameHash }` : '' ) , action . payload . formHistoryState ) ) ) ; case GO_TO_REVIEW_USER_DETAILS : return loop ( state , Cmd . action ( push ( formatUrl ( REVIEW_USER_DETAILS , { userId : action . payload . userId , sectionId : UserSection . Preview } ) ) ) ) ; case GO_TO_USER_PROFILE : return loop ( state , Cmd . action ( push ( formatUrl ( USER_PROFILE , { userId : action . payload . userId } ) ) ) ) ; } }