import { LoopReducer , Loop } from 'redux-loop' ; import { IForm , BirthSection , DeathSection } from '@client/forms' ; import * as offlineActions from '@client/offline/actions' ; import { deserializeForm } from '@client/forms/mappings/deserializer' ; import { messages } from '@client/i18n/messages/views/review' ; export type IRegisterFormState = { state : 'LOADING' ; registerForm : null ; } | { state : 'READY' ; registerForm : { birth : IForm ; death : IForm ; } ; } ; export const initialState = { state : 'LOADING' , registerForm : null } ; const GET_REGISTER_FORM = 'REGISTER_FORM/GET_REGISTER_FORM' ; type GetRegisterFormAction = { type : typeof GET_REGISTER_FORM ; } ; type Action = GetRegisterFormAction ; export const registerFormReducer = ( state = initialState , action ) : IRegisterFormState | Loop < IRegisterFormState , Action > => { switch ( action . type ) { case offlineActions . READY : case offlineActions . DEFINITIONS_LOADED : const birth = deserializeForm ( action . payload . forms . registerForm . birth ) ; const death = deserializeForm ( action . payload . forms . registerForm . death ) ; const preview = { viewType : 'preview' as const , name : messages . previewName , title : messages . previewTitle , groups : [ { id : 'preview-view-group' , fields : [ ] } ] } ; return { ... state , state : 'READY' , registerForm : { birth : { ... birth , sections : [ ... birth . sections , { ... preview , id : BirthSection . Preview } ] } , death : { ... death , sections : [ ... death . sections , { ... preview , id : DeathSection . Preview } ] } } } ; default : return state ; } } ;