|
| 1 | +// @flow |
| 2 | +import { useDispatch } from 'react-redux'; |
| 3 | +import React, { useEffect } from 'react'; |
| 4 | +import type { ComponentType } from 'react'; |
| 5 | +import { EnrollmentRegistrationEntryComponent } from './EnrollmentRegistrationEntry.component'; |
| 6 | +import { startNewEnrollmentDataEntryInitialisation } from './EnrollmentRegistrationEntry.actions'; |
| 7 | +import type { OwnProps } from './EnrollmentRegistrationEntry.types'; |
| 8 | +import { useScopeInfo } from '../../../hooks/useScopeInfo'; |
| 9 | +import { useRegistrationFormInfoForSelectedScope } from '../common/useRegistrationFormInfoForSelectedScope'; |
| 10 | +import { useCurrentOrgUnitInfo } from '../../../hooks/useCurrentOrgUnitInfo'; |
| 11 | +import { scopeTypes } from '../../../metaData'; |
| 12 | + |
| 13 | +const useInitialiseEnrollmentRegistration = (selectedScopeId, dataEntryId) => { |
| 14 | + const dispatch = useDispatch(); |
| 15 | + const { scopeType } = useScopeInfo(selectedScopeId); |
| 16 | + const { id: selectedOrgUnitId } = useCurrentOrgUnitInfo(); |
| 17 | + const { formId, formFoundation } = useRegistrationFormInfoForSelectedScope(selectedScopeId); |
| 18 | + const registrationFormReady = !!formId; |
| 19 | + useEffect(() => { |
| 20 | + if (registrationFormReady && scopeType === scopeTypes.TRACKER_PROGRAM) { |
| 21 | + dispatch( |
| 22 | + startNewEnrollmentDataEntryInitialisation( |
| 23 | + { selectedOrgUnitId, selectedScopeId, dataEntryId, formFoundation }, |
| 24 | + ), |
| 25 | + ); |
| 26 | + } |
| 27 | + }, [ |
| 28 | + scopeType, |
| 29 | + dataEntryId, |
| 30 | + selectedScopeId, |
| 31 | + selectedOrgUnitId, |
| 32 | + registrationFormReady, |
| 33 | + formFoundation, |
| 34 | + dispatch, |
| 35 | + ]); |
| 36 | +}; |
| 37 | + |
| 38 | + |
| 39 | +export const EnrollmentRegistrationEntry: ComponentType<OwnProps> = ({ selectedScopeId, id, ...rest }) => { |
| 40 | + useInitialiseEnrollmentRegistration(selectedScopeId, id); |
| 41 | + |
| 42 | + return ( |
| 43 | + <EnrollmentRegistrationEntryComponent |
| 44 | + selectedScopeId={selectedScopeId} |
| 45 | + id={id} |
| 46 | + {...rest} |
| 47 | + />); |
| 48 | +}; |
0 commit comments