11import React , { createContext , useContext , ReactElement } from 'react' ;
22import { useMachine } from '@xstate/react' ;
3- import trusteeMachine , { TrusteeContext as TC } from './trusteeMachine' ;
3+ import trusteeMachine , {
4+ TrusteeContext as TC ,
5+ TrusteeProps ,
6+ } from './trusteeMachine' ;
47import { State , EventData } from 'xstate' ;
58import { SpaceProps } from '@tpr/core' ;
69
710export const TrusteeContext = createContext < TrusteeContextProps > ( {
11+ complete : false ,
12+ testId : '' ,
13+ cfg : { } ,
814 current : { } ,
915 send : ( _ , __ ) => ( { } ) ,
1016 onCorrect : ( ) => { } ,
1117 onRemove : ( ) => new Promise ( ( res ) => res ( ) ) ,
12- onSave : ( ) => new Promise ( ( res ) => res ( ) ) ,
1318 addressAPI : { get : ( ) => new Promise ( ( res ) => res ( ) ) } ,
1419} ) ;
1520
@@ -37,19 +42,28 @@ export interface TrusteeInput {
3742 [ key : string ] : any ;
3843}
3944
40- export interface TrusteeContextProps extends Omit < TrusteeProps , 'trustee' > {
45+ export interface TrusteeContextProps {
46+ complete ?: boolean ;
47+ testId ?: string ;
48+ children ?: RenderProps | ReactElement ;
49+ cfg ?: SpaceProps ;
50+ onRemove : ( ...args : any [ ] ) => Promise < any > ;
51+ onCorrect : ( ...args : any [ ] ) => void ;
4152 send : ( event : any , payload ?: EventData ) => Partial < State < TC , any , any , any > > ;
53+ addressAPI : any ;
4254 current : Partial < State < TC , any , any , any > > ;
4355}
4456
45- export interface TrusteeProps {
57+ export interface TrusteeCardProps {
58+ trustee : TrusteeInput ;
4659 complete ?: boolean ;
47- onCorrect ?: ( ...args : any [ ] ) => void ;
48- onRemove ?: ( ...args : any [ ] ) => Promise < any > ;
49- onSave ?: ( trustee : Partial < TrusteeInput > ) => Promise < any > ;
60+ onCorrect : ( ...args : any [ ] ) => void ;
61+ onRemove : ( ...args : any [ ] ) => Promise < any > ;
62+ onDetailsSave : ( values : any , trustee : TrusteeProps ) => Promise < any > ;
63+ onContactSave : ( values : any , trustee : TrusteeProps ) => Promise < any > ;
64+ onAddressSave : ( values : any , trustee : TrusteeProps ) => Promise < any > ;
5065 addressAPI : any ;
5166 testId ?: string ;
52- trustee : TrusteeInput ;
5367 children ?: RenderProps | ReactElement ;
5468 cfg ?: SpaceProps ;
5569}
@@ -58,9 +72,11 @@ export const TrusteeProvider = ({
5872 trustee,
5973 complete,
6074 children,
61- onSave,
75+ onDetailsSave,
76+ onContactSave,
77+ onAddressSave,
6278 ...rest
63- } : TrusteeProps ) => {
79+ } : TrusteeCardProps ) => {
6480 const {
6581 addressLine1,
6682 addressLine2,
@@ -89,19 +105,47 @@ export const TrusteeProvider = ({
89105 } ,
90106 } ,
91107 services : {
92- saveData : ( { trustee } , _ ) => {
93- const { address, ...details } = trustee ;
94- return onSave ( { ...details , ...address } ) ;
108+ onDetailsSave : ( { trustee } ) => {
109+ const {
110+ schemeRoleId,
111+ title,
112+ forename,
113+ surname,
114+ trusteeType,
115+ isProfessionalTrustee,
116+ } = trustee ;
117+ return onDetailsSave (
118+ {
119+ schemeRoleId,
120+ title,
121+ forename,
122+ surname,
123+ trusteeType,
124+ isProfessionalTrustee,
125+ } ,
126+ trustee ,
127+ ) ;
128+ } ,
129+ onContactSave : ( { trustee } ) => {
130+ const { schemeRoleId, telephoneNumber, emailAddress } = trustee ;
131+ return onContactSave (
132+ { schemeRoleId, telephoneNumber, emailAddress } ,
133+ trustee ,
134+ ) ;
135+ } ,
136+ onAddressSave : ( { trustee } ) => {
137+ const { schemeRoleId, address } = trustee ;
138+ return onAddressSave ( { schemeRoleId, ...address } , trustee ) ;
95139 } ,
96140 } ,
97141 } ) ;
98142
99143 const ui =
100144 typeof children === 'function'
101- ? children ( { current, send, onSave , ...rest } )
145+ ? children ( { current, send, ...rest } )
102146 : children ;
103147 return (
104- < TrusteeContext . Provider value = { { current, send, onSave , ...rest } } >
148+ < TrusteeContext . Provider value = { { current, send, ...rest } } >
105149 { ui }
106150 </ TrusteeContext . Provider >
107151 ) ;
0 commit comments