|
| 1 | +import React, { createContext, useContext, ReactElement } from 'react'; |
| 2 | +import { useMachine } from '@xstate/react'; |
| 3 | +import inHouseAdminMachine, { |
| 4 | + InHouseAdminContext as IHAC, |
| 5 | +} from './inHouseAdminMachine'; |
| 6 | +import { State, EventData } from 'xstate'; |
| 7 | +import { SpaceProps } from '@tpr/core'; |
| 8 | +import { i18n as i18nDefaults, InHouseAdminI18nProps } from './i18n'; |
| 9 | +import { useI18n } from '../hooks/use-i18n'; |
| 10 | +import { splitObjectIntoTwo } from '../../../utils'; |
| 11 | + |
| 12 | +export const InHouseAdminContext = createContext<InHouseAdminContextProps>({ |
| 13 | + current: {}, |
| 14 | + send: (_, __) => ({}), |
| 15 | + onCorrect: () => {}, |
| 16 | + onRemove: Promise.resolve, |
| 17 | + onSaveContacts: Promise.resolve, |
| 18 | + onSaveAddress: Promise.resolve, |
| 19 | + onSaveName: Promise.resolve, |
| 20 | + i18n: i18nDefaults, |
| 21 | + addressAPI: { |
| 22 | + get: (endpoint) => Promise.resolve(endpoint), |
| 23 | + limit: 50, |
| 24 | + }, |
| 25 | +}); |
| 26 | + |
| 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 | + |
| 34 | +type RenderProps = (_props: InHouseAdminContextProps) => ReactElement; |
| 35 | + |
| 36 | +export interface InHouseAdminContextProps |
| 37 | + extends Omit<InHouseAdminProviderProps, 'inHouseAdmin'> { |
| 38 | + send: ( |
| 39 | + event: any, |
| 40 | + payload?: EventData, |
| 41 | + ) => Partial<State<IHAC, any, any, any>>; |
| 42 | + current: Partial<State<IHAC, any, any, any>>; |
| 43 | +} |
| 44 | + |
| 45 | +export type RecursivePartial<T> = { |
| 46 | + [P in keyof T]?: RecursivePartial<T[P]>; |
| 47 | +}; |
| 48 | + |
| 49 | +interface InHouseAdmin { |
| 50 | + id: string; |
| 51 | + schemeRoleId: string | number; |
| 52 | + title: string; |
| 53 | + firstname: string; |
| 54 | + lastname: string; |
| 55 | + 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 { |
| 75 | + addressLine1: string; |
| 76 | + addressLine2: string; |
| 77 | + addressLine3: string; |
| 78 | + postTown: string; |
| 79 | + county: string; |
| 80 | + postCode: string; |
| 81 | + country: string; |
| 82 | +} |
| 83 | + |
| 84 | +export interface InHouseAdminProviderProps { |
| 85 | + complete?: boolean; |
| 86 | + onCorrect?: (...args: any[]) => void; |
| 87 | + onRemove?: (...args: any[]) => Promise<any>; |
| 88 | + onSaveContacts?: (...args: any[]) => Promise<any>; |
| 89 | + onSaveAddress?: (...args: any[]) => Promise<any>; |
| 90 | + onSaveName?: (...args: any[]) => Promise<any>; |
| 91 | + testId?: string; |
| 92 | + /** inHouseAdmin props from the API */ |
| 93 | + inHouseAdmin: Partial<InHouseAdminProps>; |
| 94 | + children?: RenderProps | ReactElement; |
| 95 | + addressAPI: AddressAPIType; |
| 96 | + /** overwrite any text that you need */ |
| 97 | + i18n?: RecursivePartial<InHouseAdminI18nProps>; |
| 98 | + /** cfg space props */ |
| 99 | + cfg?: SpaceProps; |
| 100 | +} |
| 101 | + |
| 102 | +const addressFields = [ |
| 103 | + 'addressLine1', |
| 104 | + 'addressLine2', |
| 105 | + 'addressLine3', |
| 106 | + 'postTown', |
| 107 | + 'county', |
| 108 | + 'country', |
| 109 | + 'postCode', |
| 110 | + 'countryId', |
| 111 | +]; |
| 112 | + |
| 113 | +export const InHouseAdminProvider = ({ |
| 114 | + complete, |
| 115 | + inHouseAdmin, |
| 116 | + children, |
| 117 | + i18n: i18nOverrides = {}, |
| 118 | + ...rest |
| 119 | +}: InHouseAdminProviderProps) => { |
| 120 | + const i18n = useI18n(i18nDefaults, i18nOverrides); |
| 121 | + const [modifiedAdmin, adminAddress] = splitObjectIntoTwo( |
| 122 | + inHouseAdmin, |
| 123 | + addressFields, |
| 124 | + ); |
| 125 | + const [current, send] = useMachine(inHouseAdminMachine, { |
| 126 | + context: { |
| 127 | + complete, |
| 128 | + inHouseAdmin: { |
| 129 | + ...modifiedAdmin, |
| 130 | + address: adminAddress, |
| 131 | + }, |
| 132 | + }, |
| 133 | + }); |
| 134 | + |
| 135 | + const ui = |
| 136 | + typeof children === 'function' |
| 137 | + ? children({ current, send, i18n, ...rest }) |
| 138 | + : children; |
| 139 | + return ( |
| 140 | + <InHouseAdminContext.Provider value={{ current, send, i18n, ...rest }}> |
| 141 | + {ui} |
| 142 | + </InHouseAdminContext.Provider> |
| 143 | + ); |
| 144 | +}; |
| 145 | + |
| 146 | +export const useInHouseAdminContext = (): InHouseAdminContextProps => { |
| 147 | + const inHouseAdminUtils = useContext(InHouseAdminContext); |
| 148 | + if (!inHouseAdminUtils) { |
| 149 | + throw new Error( |
| 150 | + `InHouseAdmin compound components cannot be rendered outside the InHouseAdminProvider component`, |
| 151 | + ); |
| 152 | + } |
| 153 | + return inHouseAdminUtils; |
| 154 | +}; |
0 commit comments