1
0
Fork 0
mirror of https://github.com/betagouv/mon-entreprise synced 2025-02-09 04:05:01 +00:00
mon-entreprise/source/reducers/inFranceAppReducer.ts
Maxime Quandalle 7e2a4085a7 Poursuite de la migration TypeScript
* Utilisation de la version stable de TypeScript 3.7

* Début de migration du State Redux. Plutôt que de redéfinir les types
  en doublon par rapport aux actions et reducers, on utilise les valeurs
  retournées par ces fonctions comme source pour les types globaux.

* Modification de tsconfig pour meilleur typage dans VS Code

* Meilleur typage de l'environnement : suppression de @types/node qui
  était trop large (contient tout l'environnement serveur), et
  remplacement par @types/webpack-env. Par ailleurs typage des variables
  d'environnement utilisées.

* Début de migration de l'économie collaborative

* Migration de nombreux composants UI

* Mise à jour de dépendances pour récupérer un meilleur typage

* Ajout d'un hook pour configurer les simulateurs

* Suppression du higher-order component "withSitePaths", on utilise
  systématiquement le hook useContext.

L'essentiel de l'application est maintenant migré, reste le moteur !
2019-11-11 11:33:38 +01:00

169 lines
4.2 KiB
TypeScript

import { Action as CreationChecklistAction } from 'Actions/companyCreationChecklistActions'
import { Action as HiringChecklist } from 'Actions/hiringChecklistAction'
import { omit } from 'ramda'
import { combineReducers } from 'redux'
import { LegalStatus } from 'Selectors/companyStatusSelectors'
import {
Action as CompanyStatusAction,
LegalStatusRequirements
} from 'Types/companyTypes'
type Action = CompanyStatusAction | CreationChecklistAction | HiringChecklist
function companyLegalStatus(
state: LegalStatusRequirements = {},
action: Action
): LegalStatusRequirements {
switch (action.type) {
case 'COMPANY_IS_SOLE_PROPRIETORSHIP':
return { ...state, soleProprietorship: action.isSoleProprietorship }
case 'DEFINE_DIRECTOR_STATUS':
return { ...state, directorStatus: action.status }
case 'COMPANY_HAS_MULTIPLE_ASSOCIATES':
return { ...state, multipleAssociates: action.multipleAssociates }
case 'COMPANY_IS_MICROENTERPRISE':
return { ...state, autoEntrepreneur: action.autoEntrepreneur }
case 'SPECIFY_DIRECTORS_SHARE':
return { ...state, minorityDirector: action.minorityDirector }
case 'RESET_COMPANY_STATUS_CHOICE':
return action.answersToReset ? omit(action.answersToReset, state) : {}
}
return state
}
function hiringChecklist(
state: { [key: string]: boolean } = {},
action: Action
) {
switch (action.type) {
case 'CHECK_HIRING_ITEM':
return {
...state,
[action.name]: action.checked
}
case 'INITIALIZE_HIRING_CHECKLIST':
return Object.keys(state).length
? state
: action.checklistItems.reduce(
(checklist, item) => ({ ...checklist, [item]: false }),
{}
)
default:
return state
}
}
function companyCreationChecklist(
state: { [key: string]: boolean } = {},
action: Action
) {
switch (action.type) {
case 'CHECK_COMPANY_CREATION_ITEM':
return {
...state,
[action.name]: action.checked
}
case 'INITIALIZE_COMPANY_CREATION_CHECKLIST':
return Object.keys(state).length
? state
: action.checklistItems.reduce(
(checklist, item) => ({ ...checklist, [item]: false }),
{}
)
case 'RESET_COMPANY_STATUS_CHOICE':
return {}
default:
return state
}
}
function companyStatusChoice(
state: LegalStatus = null,
action: Action
): LegalStatus {
if (action.type === 'RESET_COMPANY_STATUS_CHOICE') {
return null
}
if (action.type !== 'INITIALIZE_COMPANY_CREATION_CHECKLIST') {
return state
}
return action.statusName
}
const infereLegalStatusFromCategorieJuridique = (
catégorieJuridique: string
) => {
/*
Nous utilisons le code entreprise pour connaitre le statut juridique
(voir https://www.insee.fr/fr/information/2028129)
En revanche, impossible de différencier EI et auto-entreprise
https://www.sirene.fr/sirene/public/question.action?idQuestion=2933
*/
if (catégorieJuridique === '1000') {
return 'EI'
}
if (catégorieJuridique === '5498') {
return 'EURL'
}
if (catégorieJuridique.match(/^54..$/)) {
return 'SARL'
}
if (catégorieJuridique.match(/^55..$/)) {
return 'SA'
}
if (catégorieJuridique === '5720') {
return 'SASU'
}
if (catégorieJuridique.match(/^57..$/)) {
return 'SAS'
}
return 'NON_IMPLÉMENTÉ'
}
export type Company = {
siren: string
catégorieJuridique?: string
statutJuridique?: string
isAutoEntrepreneur?: boolean
isDirigeantMajoritaire?: boolean
}
function existingCompany(state: Company = null, action): Company {
if (!action.type.startsWith('EXISTING_COMPANY::')) {
return state
}
if (action.type.endsWith('RESET')) {
return null
}
if (action.type.endsWith('SET_SIREN')) {
return { siren: action.siren }
}
if (state && action.type.endsWith('SET_CATEGORIE_JURIDIQUE')) {
const statutJuridique = infereLegalStatusFromCategorieJuridique(
action.catégorieJuridique
)
return {
siren: state.siren,
statutJuridique
}
}
if (state && action.type.endsWith('SPECIFY_AUTO_ENTREPRENEUR')) {
return { ...state, isAutoEntrepreneur: action.isAutoEntrepreneur }
}
if (state && action.type.endsWith('SPECIFY_DIRIGEANT_MAJORITAIRE')) {
return { ...state, isDirigeantMajoritaire: action.isDirigeantMajoritaire }
}
return state
}
export default combineReducers({
companyLegalStatus,
companyStatusChoice,
companyCreationChecklist,
existingCompany,
hiringChecklist
})