1
0
Fork 0
mirror of https://github.com/betagouv/mon-entreprise synced 2025-02-10 01:05:02 +00:00
mon-entreprise/source/actions/actions.ts
Johan Girod 00b122fa97 ⚙️ ajoute la conversion d'unité
Gros changements en perspective :
- Supprime la notion de période, au bénéfice de celle d'unité
  (`période : mensuelle` devient `unité: €/mois`)
- Améliore les rapports d'erreur avec des messages plus clair
- Ajoute un avertissement lorsque des types ne sont pas compatible
- Ajoute la conversion automatique d'unité dans le moteur
- Ajoute une notion d'unité par défaut de la simulation,
  c'est l'unité vers laquelle les règles qui ne spécifient pas
  d'unité seront converties
- Ajoute une notion d'unité par défaut des règles, qui spécifie
  l'unité de la règle qui prévaut lorsque qu'il n'y a pas
  d'unité par défaut de la simulation (utile pour les question ou
  pour s'assurer du bon type d'une règle)
2019-12-16 11:34:04 +01:00

162 lines
3.6 KiB
TypeScript

import { SitePaths } from 'Components/utils/withSitePaths'
import { History } from 'history'
import { RootState, SimulationConfig } from 'Reducers/rootReducer'
import { ThunkAction } from 'redux-thunk'
import { DottedName } from 'Types/rule'
import { deletePersistedSimulation } from '../storage/persistSimulation'
export type Action =
| ResetSimulationAction
| StepAction
| UpdateAction
| SetSimulationConfigAction
| DeletePreviousSimulationAction
| SetExempleAction
| ExplainVariableAction
| UpdateSituationAction
| HideControlAction
| LoadPreviousSimulationAction
| SetSituationBranchAction
| UpdateDefaultUnit
| SetActiveTargetAction
type ThunkResult<R> = ThunkAction<
R,
RootState,
{ history: History; sitePaths: SitePaths },
Action
>
type StepAction = {
type: 'STEP_ACTION'
name: 'fold' | 'unfold'
step: string
}
type SetSimulationConfigAction = {
type: 'SET_SIMULATION'
url: string
config: SimulationConfig
}
type DeletePreviousSimulationAction = {
type: 'DELETE_PREVIOUS_SIMULATION'
}
type SetExempleAction = {
type: 'SET_EXAMPLE'
name: null | string
situation?: object
dottedName?: string
}
type ResetSimulationAction = ReturnType<typeof resetSimulation>
type UpdateAction = ReturnType<typeof updateSituation>
type UpdateSituationAction = ReturnType<typeof updateSituation>
type LoadPreviousSimulationAction = ReturnType<typeof loadPreviousSimulation>
type SetSituationBranchAction = ReturnType<typeof setSituationBranch>
type SetActiveTargetAction = ReturnType<typeof setActiveTarget>
type HideControlAction = ReturnType<typeof hideControl>
type ExplainVariableAction = ReturnType<typeof explainVariable>
type UpdateDefaultUnit = ReturnType<typeof updateUnit>
export const resetSimulation = () =>
({
type: 'RESET_SIMULATION'
} as const)
export const goToQuestion = (question: string) =>
({
type: 'STEP_ACTION',
name: 'unfold',
step: question
} as const)
export const validateStepWithValue = (
dottedName: DottedName,
value: any
): ThunkResult<void> => dispatch => {
dispatch(updateSituation(dottedName, value))
dispatch({
type: 'STEP_ACTION',
name: 'fold',
step: dottedName
})
}
export const setSituationBranch = (id: number) =>
({
type: 'SET_SITUATION_BRANCH',
id
} as const)
export const setSimulationConfig = (config: Object): ThunkResult<void> => (
dispatch,
getState,
{ history }
): void => {
if (getState().simulation?.config === config) {
return
}
const url = history.location.pathname
dispatch({
type: 'SET_SIMULATION',
url,
config
})
}
export const setActiveTarget = (targetName: string) =>
({
type: 'SET_ACTIVE_TARGET_INPUT',
name: targetName
} as const)
export const deletePreviousSimulation = (): ThunkResult<void> => dispatch => {
dispatch({
type: 'DELETE_PREVIOUS_SIMULATION'
})
deletePersistedSimulation()
}
export const updateSituation = (fieldName: DottedName, value: any) =>
({
type: 'UPDATE_SITUATION',
fieldName,
value
} as const)
export const updateUnit = (defaultUnit: string) =>
({
type: 'UPDATE_DEFAULT_UNIT',
defaultUnit
} as const)
export function setExample(name: string, situation, dottedName: string) {
return { type: 'SET_EXAMPLE', name, situation, dottedName } as const
}
export const goBackToSimulation = (): ThunkResult<void> => (
dispatch,
getState,
{ history }
) => {
dispatch({ type: 'SET_EXAMPLE', name: null })
history.push(getState().simulation.url)
}
export function loadPreviousSimulation() {
return {
type: 'LOAD_PREVIOUS_SIMULATION'
} as const
}
export function hideControl(id: string) {
return { type: 'HIDE_CONTROL', id } as const
}
export const explainVariable = (variableName = null) =>
({
type: 'EXPLAIN_VARIABLE',
variableName
} as const)