1
0
Fork 0
mirror of https://github.com/betagouv/mon-entreprise synced 2025-02-09 04:05:01 +00:00
mon-entreprise/source/actions/actions.ts
Alexandre Hajjar 4f882e3727 Eslint, prettier and tsc fixes
* eslint fixes batch
* fix @typescript-eslint/camelcase (=> warn)
* fix @typescript-eslint/consistent-type-assertions
* fix no-prototype-builtins
* @typescript-eslint/prefer-string-starts-ends-with => warn
* fix @typescript-eslint/prefer-regexp-exec
* fix prefer-const
* fix no-case-declarations
* fix @typescript-eslint/no-var-requires
* fix react/jsx-key
* fix react-hooks/rules-of-hooks
* fix typescript errors following eslint fixes. Includes downgrading:
** @typescript-eslint/no-unnecessary-type-assertion
** @typescript-eslint/no-inferrable-types
* apply prettier
* use `object` type whenever possible
2020-05-05 18:24:14 +02:00

153 lines
3.6 KiB
TypeScript

import { SitePaths } from 'Components/utils/SitePathsContext'
import { History } from 'history'
import { RootState, SimulationConfig } from 'Reducers/rootReducer'
import { ThunkAction } from 'redux-thunk'
import { DottedName, Situation } from 'Rules'
import { deletePersistedSimulation } from '../storage/persistSimulation'
import { CompanyStatusAction } from './companyStatusActions'
export type Action =
| ResetSimulationAction
| StepAction
| UpdateAction
| SetSimulationConfigAction
| DeletePreviousSimulationAction
| ExplainVariableAction
| UpdateSituationAction
| HideControlAction
| LoadPreviousSimulationAction
| SetSituationBranchAction
| UpdateTargetUnitAction
| SetActiveTargetAction
| CompanyStatusAction
export type ThunkResult<R = void> = ThunkAction<
R,
RootState,
{ history: History; sitePaths: SitePaths },
Action
>
type StepAction = {
type: 'STEP_ACTION'
name: 'fold' | 'unfold'
step: DottedName
}
type SetSimulationConfigAction = {
type: 'SET_SIMULATION'
url: string
config: SimulationConfig
useCompanyDetails: boolean
}
type DeletePreviousSimulationAction = {
type: 'DELETE_PREVIOUS_SIMULATION'
}
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 UpdateTargetUnitAction = ReturnType<typeof updateUnit>
export const resetSimulation = () =>
({
type: 'RESET_SIMULATION'
} as const)
export const goToQuestion = (question: DottedName) =>
({
type: 'STEP_ACTION',
name: 'unfold',
step: question
} as const)
export const validateStepWithValue = (
dottedName: DottedName,
value: unknown
): 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: SimulationConfig,
useCompanyDetails = false
): ThunkResult<void> => (dispatch, getState, { history }): void => {
if (getState().simulation?.config === config) {
return
}
const url = history.location.pathname
dispatch({
type: 'SET_SIMULATION',
url,
useCompanyDetails,
config
})
}
export const setActiveTarget = (targetName: DottedName) =>
({
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: unknown) =>
({
type: 'UPDATE_SITUATION',
fieldName,
value
} as const)
export const updateUnit = (targetUnit: string) =>
({
type: 'UPDATE_TARGET_UNIT',
targetUnit
} as const)
export const goBackToSimulation = (): ThunkResult<void> => (
_,
getState,
{ history }
) => {
const url = getState().simulation?.url
url && history.push(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: DottedName | null = null) =>
({
type: 'EXPLAIN_VARIABLE',
variableName
} as const)