From d4585190ad9fd6e1ffb002254113c6e95441c4ad Mon Sep 17 00:00:00 2001 From: maxgfr <25312957+maxgfr@users.noreply.github.com> Date: Fri, 4 Aug 2023 19:56:07 +0200 Subject: [PATCH] fix: error --- .../outils/CommonSteps/Agreement/index.tsx | 4 +- .../CommonSteps/Agreement/store/store.ts | 18 ++-- .../CommonSteps/Agreement/store/types.ts | 3 +- .../CommonSteps/Agreement/store/validator.ts | 11 +- .../outils/CommonSteps/Informations/index.tsx | 4 +- .../CommonSteps/Informations/store/store.ts | 17 ++- .../CommonSteps/Informations/store/types.ts | 3 +- .../Informations/store/validator.ts | 11 +- .../Components/SimulatorDecorator/index.tsx | 14 ++- .../PreavisRetraiteSimulator/index.tsx | 4 +- .../DureePreavisRetraite/state/types.ts | 1 + .../state/usecases/computeNextQuestion.ts | 37 ++++--- .../state/usecases/computeNotice.ts | 102 ++++++++++-------- .../src/outils/Simulator/Simulator.tsx | 3 + 14 files changed, 129 insertions(+), 103 deletions(-) diff --git a/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/index.tsx b/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/index.tsx index a776ac1f26..a2885b993a 100644 --- a/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/index.tsx +++ b/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/index.tsx @@ -130,9 +130,7 @@ function AgreementStep({ )} {error?.errorPublicodes && ( - - Une erreur liée au moteur de calcul bloque la simulation. - + {error.errorPublicodes} )} ); diff --git a/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/store/store.ts b/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/store/store.ts index 4f9f2783eb..b28944214e 100644 --- a/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/store/store.ts +++ b/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/store/store.ts @@ -25,10 +25,9 @@ const initialState: Omit< input: { isAgreementSupportedIndemniteLicenciement: false, hasNoEnterpriseSelected: false, + informationError: false, }, - error: { - errorPublicodes: false, - }, + error: {}, hasBeenSubmit: false, isStepValid: true, }; @@ -73,7 +72,7 @@ const createCommonAgreementStore: StoreSlicePublicode< set( produce( (state: CommonAgreementStoreSlice) => { - state.agreementData.error.errorPublicodes = isOk + state.agreementData.input.informationError = isOk ? false : true; } @@ -101,7 +100,7 @@ const createCommonAgreementStore: StoreSlicePublicode< const isOk = get().informationsFunction.generatePublicodesQuestions(); set( produce((state: CommonAgreementStoreSlice) => { - state.agreementData.error.errorPublicodes = isOk ? false : true; + state.agreementData.input.informationError = isOk ? false : true; }) ); }, @@ -128,7 +127,7 @@ const createCommonAgreementStore: StoreSlicePublicode< const isOk = get().informationsFunction.generatePublicodesQuestions(); set( produce((state: CommonAgreementStoreSlice) => { - state.agreementData.error.errorPublicodes = isOk ? false : true; + state.agreementData.input.informationError = isOk ? false : true; }) ); }, @@ -143,7 +142,7 @@ const createCommonAgreementStore: StoreSlicePublicode< onNextStep: () => { const input = get().agreementData.input; const error = get().agreementData.error; - const { isValid, errorState } = validateStep(input, error); + const { isValid, errorState } = validateStep(input); const { route, agreement, enterprise } = input; if (isValid && route) { const isTreated = !!supportedCcn.find( @@ -199,10 +198,7 @@ const applyGenericValidation = ( const nextState = produce(get(), (draft) => { draft.agreementData.input[paramName] = value; }); - const { isValid, errorState } = validateStep( - nextState.agreementData.input, - nextState.agreementData.error - ); + const { isValid, errorState } = validateStep(nextState.agreementData.input); set( produce((state: CommonAgreementStoreSlice) => { state.agreementData.error = errorState; diff --git a/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/store/types.ts b/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/store/types.ts index 938869dedb..338e89072d 100644 --- a/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/store/types.ts +++ b/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/store/types.ts @@ -13,13 +13,14 @@ export type CommonAgreementStoreInput = { enterprise?: Enterprise; isAgreementSupportedIndemniteLicenciement: boolean; hasNoEnterpriseSelected: boolean; + informationError: boolean; }; export type CommonAgreementStoreError = { route?: string; agreement?: string; enterprise?: string; - errorPublicodes: boolean; + errorPublicodes?: string; }; export type CommonAgreementStoreData = { diff --git a/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/store/validator.ts b/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/store/validator.ts index 0420561e24..d2003f3f71 100644 --- a/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/store/validator.ts +++ b/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/store/validator.ts @@ -1,10 +1,7 @@ import { deepEqualObject } from "../../../../lib"; import { CommonAgreementStoreError, CommonAgreementStoreInput } from "./types"; -export const validateStep = ( - state: CommonAgreementStoreInput, - error: CommonAgreementStoreError -) => { +export const validateStep = (state: CommonAgreementStoreInput) => { const errorState: CommonAgreementStoreError = { route: !state.route ? "Vous devez répondre à cette question" : undefined, agreement: @@ -19,14 +16,16 @@ export const validateStep = ( : state.route === "enterprise" && state.enterprise && !state.agreement ? "Vous devez sélectionner une convention collective" : undefined, - errorPublicodes: error.errorPublicodes, + errorPublicodes: state.informationError + ? "Une erreur liée au moteur de calcul bloque la simulation." + : undefined, }; return { isValid: deepEqualObject(errorState, { route: undefined, agreement: undefined, enterprise: undefined, - errorPublicodes: false, + errorPublicodes: undefined, }), errorState, }; diff --git a/packages/code-du-travail-frontend/src/outils/CommonSteps/Informations/index.tsx b/packages/code-du-travail-frontend/src/outils/CommonSteps/Informations/index.tsx index 9d971fcc27..5a82c33ee1 100644 --- a/packages/code-du-travail-frontend/src/outils/CommonSteps/Informations/index.tsx +++ b/packages/code-du-travail-frontend/src/outils/CommonSteps/Informations/index.tsx @@ -34,9 +34,7 @@ const CommonInformationStep = ({ ); })} {errors.errorPublicodes && ( - - Une erreur liée au moteur de calcul bloque la simulation. - + {errors.errorPublicodes} )} ); diff --git a/packages/code-du-travail-frontend/src/outils/CommonSteps/Informations/store/store.ts b/packages/code-du-travail-frontend/src/outils/CommonSteps/Informations/store/store.ts index 745d850460..a875f8a92c 100644 --- a/packages/code-du-travail-frontend/src/outils/CommonSteps/Informations/store/store.ts +++ b/packages/code-du-travail-frontend/src/outils/CommonSteps/Informations/store/store.ts @@ -27,10 +27,10 @@ const initialState: CommonInformationsStoreData = { isStepHidden: true, isStepSalaryHidden: false, hasNoMissingQuestions: false, + informationError: false, }, error: { errorInformations: {}, - errorPublicodes: false, }, hasBeenSubmit: false, isStepValid: true, @@ -96,7 +96,7 @@ const createCommonInformationsStore: StoreSlice< Sentry.captureException(e); set( produce((state: CommonInformationsStoreSlice) => { - state.informationsData.error.errorPublicodes = true; + state.informationsData.input.informationError = true; }) ); return false; @@ -105,6 +105,7 @@ const createCommonInformationsStore: StoreSlice< onInformationsChange: (key, value, type) => { let newPublicodesInformationsForNextQuestions: PublicodesInformation[]; let hasNoMissingQuestions = false; + let informationError = false; const publicodesInformations = get().informationsData.input.publicodesInformations; const isLicenciementInaptitude = @@ -150,11 +151,7 @@ const createCommonInformationsStore: StoreSlice< blockingNotification = notifBloquante[0].description; } } catch (e) { - set( - produce((state: CommonInformationsStoreSlice) => { - state.informationsData.error.errorPublicodes = true; - }) - ); + informationError = true; console.error(e); } set( @@ -203,6 +200,7 @@ const createCommonInformationsStore: StoreSlice< "hasNoMissingQuestions", hasNoMissingQuestions ); + applyGenericValidation(get, set, "informationError", informationError); }, onSetStepHidden: () => { const publicodesInformations = @@ -240,7 +238,7 @@ const createCommonInformationsStore: StoreSlice< onNextStep: () => { const state = get().informationsData.input; const currentError = get().informationsData.error; - const { isValid, errorState } = validateStep(state, currentError); + const { isValid, errorState } = validateStep(state); let errorEligibility; if (isValid) { @@ -277,8 +275,7 @@ const applyGenericValidation = ( draft.informationsData.input[paramName] = value; }); const { isValid, errorState } = validateStep( - nextState.informationsData.input, - nextState.informationsData.error + nextState.informationsData.input ); set( produce((state: CommonInformationsStoreSlice) => { diff --git a/packages/code-du-travail-frontend/src/outils/CommonSteps/Informations/store/types.ts b/packages/code-du-travail-frontend/src/outils/CommonSteps/Informations/store/types.ts index 133ccc20f5..6bd19c4530 100644 --- a/packages/code-du-travail-frontend/src/outils/CommonSteps/Informations/store/types.ts +++ b/packages/code-du-travail-frontend/src/outils/CommonSteps/Informations/store/types.ts @@ -18,12 +18,13 @@ export type CommonInformationsStoreInput = { isStepSalaryHidden: boolean; hasNoMissingQuestions: boolean; blockingNotification?: string; + informationError: boolean; }; export type CommonInformationsStoreError = { errorInformations: Record; errorEligibility?: string; - errorPublicodes: boolean; + errorPublicodes?: string; }; export type CommonInformationsStoreData = { diff --git a/packages/code-du-travail-frontend/src/outils/CommonSteps/Informations/store/validator.ts b/packages/code-du-travail-frontend/src/outils/CommonSteps/Informations/store/validator.ts index 45ff882fab..a120d58aa9 100644 --- a/packages/code-du-travail-frontend/src/outils/CommonSteps/Informations/store/validator.ts +++ b/packages/code-du-travail-frontend/src/outils/CommonSteps/Informations/store/validator.ts @@ -6,10 +6,7 @@ import { import { RuleType } from "@socialgouv/modeles-social"; import { isDate, isPositiveNumber } from "../../../common/validators"; -export const validateStep = ( - state: CommonInformationsStoreInput, - error: CommonInformationsStoreError -) => { +export const validateStep = (state: CommonInformationsStoreInput) => { const informations = state.publicodesInformations; let errorInformations: Record = {}; informations.forEach((info) => { @@ -21,12 +18,14 @@ export const validateStep = ( let errorState: CommonInformationsStoreError = { errorInformations, - errorPublicodes: error.errorPublicodes, + errorPublicodes: state.informationError + ? "Une erreur liée au moteur de calcul bloque la simulation." + : undefined, }; const isValid = deepEqualObject(errorState, { errorInformations: {}, - errorPublicodes: false, + errorPublicodes: undefined, }); return { isValid, errorState }; }; diff --git a/packages/code-du-travail-frontend/src/outils/Components/SimulatorDecorator/index.tsx b/packages/code-du-travail-frontend/src/outils/Components/SimulatorDecorator/index.tsx index 0506977781..d4b65171bc 100644 --- a/packages/code-du-travail-frontend/src/outils/Components/SimulatorDecorator/index.tsx +++ b/packages/code-du-travail-frontend/src/outils/Components/SimulatorDecorator/index.tsx @@ -1,5 +1,5 @@ import { Fieldset, Legend, theme, Wrapper } from "@socialgouv/cdtn-ui"; -import React from "react"; +import React, { useContext } from "react"; import { Form } from "react-final-form"; import styled from "styled-components"; @@ -10,6 +10,7 @@ import type { TitleProps, } from "./Components/types"; import { Decorator, FormApi, Mutator, ValidationErrors } from "final-form"; +import { InlineError } from "../../common/ErrorField"; type Props = { title: TitleProps; @@ -33,6 +34,7 @@ type Props = { annotations?: JSX.Element; debug?: JSX.Element; }; + hasErrorPublicodes?: boolean; }; const SimulatorDecorator = ({ @@ -45,6 +47,7 @@ const SimulatorDecorator = ({ formOptions, renderStep, options, + hasErrorPublicodes, }: Props): JSX.Element => { const onPrevious = navigation.onPrevious; @@ -80,15 +83,22 @@ const SimulatorDecorator = ({ renderStep(form) )} + {hasErrorPublicodes && ( + + Une erreur liée au moteur de calcul bloque la simulation.{" "} + + )} + { onPrevious(form.getState().values); }) } + showNext={navigation.showNext && !hasErrorPublicodes} /> {options?.annotations &&

{options.annotations}

} {process.env.NODE_ENV !== "production" && diff --git a/packages/code-du-travail-frontend/src/outils/DureePreavisRetraite/Components/PreavisRetraiteSimulator/index.tsx b/packages/code-du-travail-frontend/src/outils/DureePreavisRetraite/Components/PreavisRetraiteSimulator/index.tsx index 57e7bcb5aa..f793261314 100644 --- a/packages/code-du-travail-frontend/src/outils/DureePreavisRetraite/Components/PreavisRetraiteSimulator/index.tsx +++ b/packages/code-du-travail-frontend/src/outils/DureePreavisRetraite/Components/PreavisRetraiteSimulator/index.tsx @@ -17,11 +17,12 @@ const PreavisRetraiteSimulator = ({ displayTitle, }: Props): JSX.Element => { const store = useContext(PreavisRetraiteContext); - const { onChange, onStepChange } = usePreavisRetraiteStore( + const { onChange, onStepChange, errorPublicodes } = usePreavisRetraiteStore( store, (state) => ({ onChange: state.onFormValuesChange, onStepChange: state.onStepChange, + errorPublicodes: state.errorPublicodes, }) ); @@ -35,6 +36,7 @@ const PreavisRetraiteSimulator = ({ onFormValuesChange={onChange} onStepChange={onStepChange} steps={steps} + hasErrorPublicodes={errorPublicodes} /> ); }; diff --git a/packages/code-du-travail-frontend/src/outils/DureePreavisRetraite/state/types.ts b/packages/code-du-travail-frontend/src/outils/DureePreavisRetraite/state/types.ts index c6ebeaebc1..e613363453 100644 --- a/packages/code-du-travail-frontend/src/outils/DureePreavisRetraite/state/types.ts +++ b/packages/code-du-travail-frontend/src/outils/DureePreavisRetraite/state/types.ts @@ -23,6 +23,7 @@ export type PreavisRetraiteState = { result?: ResultStepProps; }; formValues: PreavisRetraiteFormState; + errorPublicodes: boolean | undefined; }; export type PublicodesState = { diff --git a/packages/code-du-travail-frontend/src/outils/DureePreavisRetraite/state/usecases/computeNextQuestion.ts b/packages/code-du-travail-frontend/src/outils/DureePreavisRetraite/state/usecases/computeNextQuestion.ts index 517d74737b..bf1d4bd77f 100644 --- a/packages/code-du-travail-frontend/src/outils/DureePreavisRetraite/state/usecases/computeNextQuestion.ts +++ b/packages/code-du-travail-frontend/src/outils/DureePreavisRetraite/state/usecases/computeNextQuestion.ts @@ -2,6 +2,7 @@ import { stateToPublicode } from "./helpers"; import { PreavisRetraiteStore, Question } from "../types"; import { MissingArgs } from "@socialgouv/modeles-social"; import validateInformationAgreement3239 from "./validateInformationAgreement3239"; +import * as Sentry from "@sentry/nextjs"; const excludedRules = [ "contrat salarié - ancienneté", @@ -47,20 +48,30 @@ const computeNextQuestion = ( } const currentQuestions = state.steps.informations.questions; const newSituation = stateToPublicode(state.formValues); - const missingQuestions = getNextQuestion( - state.publicodes.setSituation(newSituation).missingArgs, - currentQuestions - ); - const newQuestions = currentQuestions.concat(missingQuestions); - return { - ...state, - steps: { - ...state.steps, - informations: { - questions: newQuestions, + try { + const missingQuestions = getNextQuestion( + state.publicodes.setSituation(newSituation).missingArgs, + currentQuestions + ); + const newQuestions = currentQuestions.concat(missingQuestions); + return { + ...state, + steps: { + ...state.steps, + informations: { + questions: newQuestions, + }, }, - }, - }; + errorPublicodes: false, + }; + } catch (e) { + Sentry.captureException(e); + console.error(e); + return { + ...state, + errorPublicodes: true, + }; + } }; export default computeNextQuestion; diff --git a/packages/code-du-travail-frontend/src/outils/DureePreavisRetraite/state/usecases/computeNotice.ts b/packages/code-du-travail-frontend/src/outils/DureePreavisRetraite/state/usecases/computeNotice.ts index fbeb984a59..c51ffd78c2 100644 --- a/packages/code-du-travail-frontend/src/outils/DureePreavisRetraite/state/usecases/computeNotice.ts +++ b/packages/code-du-travail-frontend/src/outils/DureePreavisRetraite/state/usecases/computeNotice.ts @@ -1,61 +1,71 @@ import { PreavisRetraiteStore } from "../types"; import { stateToPublicode } from "./helpers"; import { computeWarningType } from "./computeWarningType"; +import * as Sentry from "@sentry/nextjs"; const computeNotice = (state: PreavisRetraiteStore): PreavisRetraiteStore => { const values = state.formValues; const publicodes = state.publicodes; - const result = publicodes.setSituation(stateToPublicode(values)); - const legalResult = publicodes.execute( - "contrat salarié . préavis de retraite légale en jours" - ); - let agreementResult = publicodes.execute( - "contrat salarié . préavis de retraite collective en jours" - ); - const agreementMaximumResult = publicodes.execute( - "contrat salarié . préavis de retraite collective maximum en jours" - ); + try { + const result = publicodes.setSituation(stateToPublicode(values)); + const legalResult = publicodes.execute( + "contrat salarié . préavis de retraite légale en jours" + ); + let agreementResult = publicodes.execute( + "contrat salarié . préavis de retraite collective en jours" + ); + const agreementMaximumResult = publicodes.execute( + "contrat salarié . préavis de retraite collective maximum en jours" + ); + const type = + values.origin?.isRetirementMandatory === "oui" ? "mise" : "départ"; - const type = - values.origin?.isRetirementMandatory === "oui" ? "mise" : "départ"; + const warningType = computeWarningType({ + resultValueInDays: result.result.valueInDays, + ccNumber: values.ccn?.selected?.num, + type, + }); - const warningType = computeWarningType({ - resultValueInDays: result.result.valueInDays, - ccNumber: values.ccn?.selected?.num, - type, - }); - - return { - ...state, - steps: { - ...state.steps, - result: { - notice: { - result: result.result, - agreement: { - result: agreementResult, - maximum: - agreementMaximumResult.valueInDays > 0 - ? agreementMaximumResult - : null, + return { + ...state, + steps: { + ...state.steps, + result: { + notice: { + result: result.result, + agreement: { + result: agreementResult, + maximum: + agreementMaximumResult.valueInDays > 0 + ? agreementMaximumResult + : null, + }, + legal: legalResult, + type, + notifications: publicodes.getNotifications(), + }, + detail: { + references: publicodes.getReferences(), + values, + situation: publicodes.data.situation, + minYearCount: state.steps.seniority.minYearCount, + }, + warning: { + type: warningType, + hasNotice: result.result.valueInDays > 0, }, - legal: legalResult, - type, - notifications: publicodes.getNotifications(), - }, - detail: { - references: publicodes.getReferences(), - values, - situation: publicodes.data.situation, - minYearCount: state.steps.seniority.minYearCount, - }, - warning: { - type: warningType, - hasNotice: result.result.valueInDays > 0, }, }, - }, - }; + errorPublicodes: false, + }; + } catch (e) { + Sentry.captureException(e); + console.error(e); + return { + ...state, + errorPublicodes: true, + }; + } }; export default computeNotice; diff --git a/packages/code-du-travail-frontend/src/outils/Simulator/Simulator.tsx b/packages/code-du-travail-frontend/src/outils/Simulator/Simulator.tsx index 4606d807c4..18ac75220d 100644 --- a/packages/code-du-travail-frontend/src/outils/Simulator/Simulator.tsx +++ b/packages/code-du-travail-frontend/src/outils/Simulator/Simulator.tsx @@ -23,6 +23,7 @@ type Props = { debug: JSX.Element; onFormValuesChange: (values: FormState) => void; onStepChange: (oldStep: Step, newStep: Step) => void; + hasErrorPublicodes?: boolean; }; const SimulatorContent = ({ @@ -34,6 +35,7 @@ const SimulatorContent = ({ debug, onFormValuesChange, onStepChange, + hasErrorPublicodes, }: Props): JSX.Element => { const store = useContext(SimulatorContext); const { currentStepIndex, previousStep, nextStep } = useSimulatorStepStore( @@ -141,6 +143,7 @@ const SimulatorContent = ({ }, legend: currentStep.options?.isForm ? currentStep.label : undefined, }} + hasErrorPublicodes={hasErrorPublicodes} /> ); };