|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { formPropTypes } from 'redux-form'; |
| 4 | +import { connect } from 'react-redux'; |
| 5 | +import { FormattedMessage } from 'react-intl'; |
| 6 | +import { Undertittel } from 'nav-frontend-typografi'; |
| 7 | +import { VerticalSpacer } from '@fpsak-frontend/shared-components'; |
| 8 | +import aksjonspunktCodes from '@fpsak-frontend/kodeverk/src/aksjonspunktCodes'; |
| 9 | +import { isAksjonspunktOpen } from '@fpsak-frontend/kodeverk/src/aksjonspunktStatus'; |
| 10 | +import { FaktaBegrunnelseTextField, FaktaSubmitButton } from '@fpsak-frontend/fakta-felles'; |
| 11 | +import { behandlingForm } from '@fpsak-frontend/form'; |
| 12 | +import fordelBeregningsgrunnlagAksjonspunkterPropType from '../../propTypes/fordelBeregningsgrunnlagAksjonspunkterPropType'; |
| 13 | +import beregningsgrunnlagPropType from '../../propTypes/beregningsgrunnlagPropType'; |
| 14 | +import TidligereUtbetalinger from './TidligereUtbetalinger'; |
| 15 | +import VurderEndringRefusjonRad, { lagNøkkel } from './VurderEndringRefusjonRad'; |
| 16 | + |
| 17 | +const FORM_NAME = 'VURDER_REFUSJON_BERGRUNN_FORM'; |
| 18 | +const BEGRUNNELSE_FIELD = 'VURDER_REFUSJON_BERGRUNN_BEGRUNNELSE'; |
| 19 | + |
| 20 | +const { |
| 21 | + VURDER_REFUSJON_BERGRUNN, |
| 22 | +} = aksjonspunktCodes; |
| 23 | + |
| 24 | +const finnAksjonspunkt = (aksjonspunkter) => (aksjonspunkter ? aksjonspunkter.find((ap) => ap.definisjon.kode === VURDER_REFUSJON_BERGRUNN) : undefined); |
| 25 | + |
| 26 | +export const VurderEndringRefusjonFormImpl = ({ |
| 27 | + submitEnabled, |
| 28 | + submittable, |
| 29 | + readOnly, |
| 30 | + behandlingId, |
| 31 | + behandlingVersjon, |
| 32 | + beregningsgrunnlag, |
| 33 | + aksjonspunkter, |
| 34 | + ...formProps |
| 35 | +}) => { |
| 36 | + const { andeler } = beregningsgrunnlag.refusjonTilVurdering; |
| 37 | + const ap = finnAksjonspunkt(aksjonspunkter); |
| 38 | + const isAksjonspunktClosed = ap ? isAksjonspunktOpen(ap.status.kode) : false; |
| 39 | + return ( |
| 40 | + <> |
| 41 | + <form onSubmit={formProps.handleSubmit}> |
| 42 | + <Undertittel><FormattedMessage id="BeregningInfoPanel.RefusjonBG.Tittel" /></Undertittel> |
| 43 | + <VerticalSpacer sixteenPx /> |
| 44 | + <TidligereUtbetalinger beregningsgrunnlag={beregningsgrunnlag} /> |
| 45 | + { andeler.map((andel) => ( |
| 46 | + <VurderEndringRefusjonRad |
| 47 | + refusjonAndel={andel} |
| 48 | + readOnly={readOnly} |
| 49 | + key={andel.arbeidsgiverNavn} |
| 50 | + /> |
| 51 | + ))} |
| 52 | + <> |
| 53 | + <VerticalSpacer twentyPx /> |
| 54 | + <FaktaBegrunnelseTextField |
| 55 | + name={BEGRUNNELSE_FIELD} |
| 56 | + isDirty={formProps.dirty} |
| 57 | + isSubmittable={submittable} |
| 58 | + isReadOnly={readOnly} |
| 59 | + hasBegrunnelse={!!(ap && ap.begrunnelse)} |
| 60 | + /> |
| 61 | + |
| 62 | + <VerticalSpacer twentyPx /> |
| 63 | + <FaktaSubmitButton |
| 64 | + formName={formProps.form} |
| 65 | + isSubmittable={submittable && submitEnabled} |
| 66 | + isReadOnly={readOnly} |
| 67 | + hasOpenAksjonspunkter={!isAksjonspunktClosed} |
| 68 | + behandlingId={behandlingId} |
| 69 | + behandlingVersjon={behandlingVersjon} |
| 70 | + /> |
| 71 | + </> |
| 72 | + </form> |
| 73 | + </> |
| 74 | + ); |
| 75 | +}; |
| 76 | + |
| 77 | +export const buildInitialValues = (bg, aksjonspunkter) => { |
| 78 | + const { andeler } = bg.refusjonTilVurdering; |
| 79 | + const initialValues = {}; |
| 80 | + andeler.forEach((andel) => { |
| 81 | + initialValues[lagNøkkel(andel)] = VurderEndringRefusjonRad.buildInitialValues(andel); |
| 82 | + }); |
| 83 | + const refusjonAP = finnAksjonspunkt(aksjonspunkter); |
| 84 | + initialValues[BEGRUNNELSE_FIELD] = refusjonAP && refusjonAP.begrunnelse ? refusjonAP.begrunnelse : ''; |
| 85 | + return initialValues; |
| 86 | +}; |
| 87 | + |
| 88 | +export const transformValues = (values, bg) => { |
| 89 | + const { andeler } = bg.refusjonTilVurdering; |
| 90 | + const transformedAndeler = andeler.map((andel) => VurderEndringRefusjonRad.transformValues(values, andel)); |
| 91 | + return { |
| 92 | + begrunnelse: values[BEGRUNNELSE_FIELD], |
| 93 | + kode: VURDER_REFUSJON_BERGRUNN, |
| 94 | + fastsatteAndeler: transformedAndeler, |
| 95 | + }; |
| 96 | +}; |
| 97 | + |
| 98 | +VurderEndringRefusjonFormImpl.propTypes = { |
| 99 | + submitCallback: PropTypes.func.isRequired, |
| 100 | + readOnly: PropTypes.bool.isRequired, |
| 101 | + submittable: PropTypes.bool.isRequired, |
| 102 | + submitEnabled: PropTypes.bool.isRequired, |
| 103 | + behandlingId: PropTypes.number.isRequired, |
| 104 | + behandlingVersjon: PropTypes.number.isRequired, |
| 105 | + beregningsgrunnlag: beregningsgrunnlagPropType, |
| 106 | + aksjonspunkter: PropTypes.arrayOf(fordelBeregningsgrunnlagAksjonspunkterPropType).isRequired, |
| 107 | + ...formPropTypes, |
| 108 | +}; |
| 109 | + |
| 110 | +const mapStateToProps = (initialState, initialProps) => { |
| 111 | + const onSubmit = (values) => initialProps.submitCallback([transformValues(values, initialProps.beregningsgrunnlag)]); |
| 112 | + return (state, ownProps) => { |
| 113 | + const initialValues = buildInitialValues(ownProps.beregningsgrunnlag, ownProps.aksjonspunkter); |
| 114 | + return ({ |
| 115 | + initialValues, |
| 116 | + onSubmit, |
| 117 | + }); |
| 118 | + }; |
| 119 | +}; |
| 120 | + |
| 121 | +export default connect(mapStateToProps)(behandlingForm({ |
| 122 | + form: FORM_NAME, |
| 123 | + enableReinitialize: true, |
| 124 | +})(VurderEndringRefusjonFormImpl)); |
0 commit comments