Skip to content

Commit

Permalink
fix: error
Browse files Browse the repository at this point in the history
  • Loading branch information
maxgfr committed Aug 4, 2023
1 parent 86738d8 commit d458519
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ function AgreementStep({
</>
)}
{error?.errorPublicodes && (
<InlineError>
Une erreur liée au moteur de calcul bloque la simulation.
</InlineError>
<InlineError>{error.errorPublicodes}</InlineError>
)}
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ const initialState: Omit<
input: {
isAgreementSupportedIndemniteLicenciement: false,
hasNoEnterpriseSelected: false,
informationError: false,
},
error: {
errorPublicodes: false,
},
error: {},
hasBeenSubmit: false,
isStepValid: true,
};
Expand Down Expand Up @@ -73,7 +72,7 @@ const createCommonAgreementStore: StoreSlicePublicode<
set(
produce(
(state: CommonAgreementStoreSlice<PublicodesSimulator>) => {
state.agreementData.error.errorPublicodes = isOk
state.agreementData.input.informationError = isOk
? false
: true;
}
Expand Down Expand Up @@ -101,7 +100,7 @@ const createCommonAgreementStore: StoreSlicePublicode<
const isOk = get().informationsFunction.generatePublicodesQuestions();
set(
produce((state: CommonAgreementStoreSlice<PublicodesSimulator>) => {
state.agreementData.error.errorPublicodes = isOk ? false : true;
state.agreementData.input.informationError = isOk ? false : true;
})
);
},
Expand All @@ -128,7 +127,7 @@ const createCommonAgreementStore: StoreSlicePublicode<
const isOk = get().informationsFunction.generatePublicodesQuestions();
set(
produce((state: CommonAgreementStoreSlice<PublicodesSimulator>) => {
state.agreementData.error.errorPublicodes = isOk ? false : true;
state.agreementData.input.informationError = isOk ? false : true;
})
);
},
Expand All @@ -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(
Expand Down Expand Up @@ -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<PublicodesSimulator>) => {
state.agreementData.error = errorState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends PublicodesSimulator> = {
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ const CommonInformationStep = ({
);
})}
{errors.errorPublicodes && (
<InlineError>
Une erreur liée au moteur de calcul bloque la simulation.
</InlineError>
<InlineError>{errors.errorPublicodes}</InlineError>
)}
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ const initialState: CommonInformationsStoreData = {
isStepHidden: true,
isStepSalaryHidden: false,
hasNoMissingQuestions: false,
informationError: false,
},
error: {
errorInformations: {},
errorPublicodes: false,
},
hasBeenSubmit: false,
isStepValid: true,
Expand Down Expand Up @@ -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;
Expand All @@ -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 =
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -203,6 +200,7 @@ const createCommonInformationsStore: StoreSlice<
"hasNoMissingQuestions",
hasNoMissingQuestions
);
applyGenericValidation(get, set, "informationError", informationError);
},
onSetStepHidden: () => {
const publicodesInformations =
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ export type CommonInformationsStoreInput = {
isStepSalaryHidden: boolean;
hasNoMissingQuestions: boolean;
blockingNotification?: string;
informationError: boolean;
};

export type CommonInformationsStoreError = {
errorInformations: Record<string, string>;
errorEligibility?: string;
errorPublicodes: boolean;
errorPublicodes?: string;
};

export type CommonInformationsStoreData = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = {};
informations.forEach((info) => {
Expand All @@ -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 };
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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<FormValues> = {
title: TitleProps;
Expand All @@ -33,6 +34,7 @@ type Props<FormValues> = {
annotations?: JSX.Element;
debug?: JSX.Element;
};
hasErrorPublicodes?: boolean;
};

const SimulatorDecorator = <FormValues,>({
Expand All @@ -45,6 +47,7 @@ const SimulatorDecorator = <FormValues,>({
formOptions,
renderStep,
options,
hasErrorPublicodes,
}: Props<FormValues>): JSX.Element => {
const onPrevious = navigation.onPrevious;

Expand Down Expand Up @@ -80,15 +83,22 @@ const SimulatorDecorator = <FormValues,>({
renderStep(form)
)}

{hasErrorPublicodes && (
<InlineError>
Une erreur liée au moteur de calcul bloque la simulation.{" "}
</InlineError>
)}

<Navigation
{...navigation}
hasError={invalid && submitFailed}
hasError={invalid && submitFailed && hasErrorPublicodes}
onPrevious={
onPrevious &&
(() => {
onPrevious(form.getState().values);
})
}
showNext={navigation.showNext && !hasErrorPublicodes}
/>
{options?.annotations && <p>{options.annotations}</p>}
{process.env.NODE_ENV !== "production" &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
);

Expand All @@ -35,6 +36,7 @@ const PreavisRetraiteSimulator = ({
onFormValuesChange={onChange}
onStepChange={onStepChange}
steps={steps}
hasErrorPublicodes={errorPublicodes}
/>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type PreavisRetraiteState = {
result?: ResultStepProps;
};
formValues: PreavisRetraiteFormState;
errorPublicodes: boolean | undefined;
};

export type PublicodesState = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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é",
Expand Down Expand Up @@ -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;
Loading

0 comments on commit d458519

Please sign in to comment.