@@ -7,16 +7,30 @@ import { State, EventData } from 'xstate';
77import { SpaceProps } from '@tpr/core' ;
88import { i18n as i18nDefaults , InHouseAdminI18nProps } from './i18n' ;
99import { useI18n } from '../hooks/use-i18n' ;
10+ import { splitObjectIntoTwo } from '../../../utils' ;
1011
1112export const InHouseAdminContext = createContext < InHouseAdminContextProps > ( {
1213 current : { } ,
1314 send : ( _ , __ ) => ( { } ) ,
1415 onCorrect : ( ) => { } ,
1516 onRemove : Promise . resolve ,
16- onSaveType : Promise . resolve ,
17+ onSaveContacts : Promise . resolve ,
18+ onSaveAddress : Promise . resolve ,
19+ onSaveName : Promise . resolve ,
1720 i18n : i18nDefaults ,
21+ addressAPI : {
22+ get : ( endpoint ) => Promise . resolve ( endpoint ) ,
23+ limit : 50 ,
24+ } ,
1825} ) ;
1926
27+ type AddressAPIType = {
28+ /** API instance with auth to get a list of addresses */
29+ get : ( endpoint : string ) => Promise < any > ;
30+ /** limit of items to display per search */
31+ limit : number ;
32+ } ;
33+
2034type RenderProps = ( _props : InHouseAdminContextProps ) => ReactElement ;
2135
2236export interface InHouseAdminContextProps
@@ -32,40 +46,70 @@ export type RecursivePartial<T> = {
3246 [ P in keyof T ] ?: RecursivePartial < T [ P ] > ;
3347} ;
3448
35- export type InHouseAdminProps = {
49+ interface InHouseAdmin {
3650 id : string ;
37- schemeRoleId : string ;
51+ schemeRoleId : string | number ;
3852 title : string ;
3953 firstname : string ;
4054 lastname : string ;
4155 effectiveDate : string ;
56+ countryId : string ;
57+ telephoneNumber : string ;
58+ emailAddress : string ;
59+ addressAPI : AddressAPIType ;
60+ }
61+
62+ export interface InHouseAdminWithContactsProps extends InHouseAdmin {
63+ address : Partial < {
64+ addressLine1 : string ;
65+ addressLine2 : string ;
66+ addressLine3 : string ;
67+ postTown : string ;
68+ county : string ;
69+ postCode : string ;
70+ country : string ;
71+ } > ;
72+ }
73+
74+ export interface InHouseAdminProps extends InHouseAdmin {
4275 addressLine1 : string ;
4376 addressLine2 : string ;
4477 addressLine3 : string ;
4578 postTown : string ;
4679 county : string ;
47- postcode : string ;
48- countryId : string ;
49- telephoneNumber : string ;
50- emailAddress : string ;
51- [ key : string ] : any ;
52- } ;
80+ postCode : string ;
81+ country : string ;
82+ }
5383
5484export interface InHouseAdminProviderProps {
5585 complete ?: boolean ;
5686 onCorrect ?: ( ...args : any [ ] ) => void ;
5787 onRemove ?: ( ...args : any [ ] ) => Promise < any > ;
58- onSaveType ?: ( ...args : any [ ] ) => Promise < any > ;
88+ onSaveContacts ?: ( ...args : any [ ] ) => Promise < any > ;
89+ onSaveAddress ?: ( ...args : any [ ] ) => Promise < any > ;
90+ onSaveName ?: ( ...args : any [ ] ) => Promise < any > ;
5991 testId ?: string ;
6092 /** inHouseAdmin props from the API */
6193 inHouseAdmin : Partial < InHouseAdminProps > ;
6294 children ?: RenderProps | ReactElement ;
95+ addressAPI : AddressAPIType ;
6396 /** overwrite any text that you need */
6497 i18n ?: RecursivePartial < InHouseAdminI18nProps > ;
6598 /** cfg space props */
6699 cfg ?: SpaceProps ;
67100}
68101
102+ const addressFields = [
103+ 'addressLine1' ,
104+ 'addressLine2' ,
105+ 'addressLine3' ,
106+ 'postTown' ,
107+ 'county' ,
108+ 'country' ,
109+ 'postCode' ,
110+ 'countryId' ,
111+ ] ;
112+
69113export const InHouseAdminProvider = ( {
70114 complete,
71115 inHouseAdmin,
@@ -74,10 +118,17 @@ export const InHouseAdminProvider = ({
74118 ...rest
75119} : InHouseAdminProviderProps ) => {
76120 const i18n = useI18n ( i18nDefaults , i18nOverrides ) ;
121+ const [ modifiedAdmin , adminAddress ] = splitObjectIntoTwo (
122+ inHouseAdmin ,
123+ addressFields ,
124+ ) ;
77125 const [ current , send ] = useMachine ( inHouseAdminMachine , {
78126 context : {
79127 complete,
80- inHouseAdmin,
128+ inHouseAdmin : {
129+ ...modifiedAdmin ,
130+ address : adminAddress ,
131+ } ,
81132 } ,
82133 } ) ;
83134
0 commit comments