-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(IDPay): [IDPAY-30] Create XState machine for IDPay Onboarding f…
…low (#4151) * Create XState machine for IDPay Onboarding flow * Add xstate types generation * State machine refactoring * Add provider * Refactor to test the provider * Add IDPay envs * Remove API test token
- Loading branch information
1 parent
f35e73d
commit eea9ad4
Showing
21 changed files
with
656 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
locales/locales.ts | ||
ts/utils/__tests__/xss.test.ts | ||
definitions/* | ||
**/*.typegen.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React from "react"; | ||
import { createStackNavigator } from "@react-navigation/stack"; | ||
import InitiativeDetailsScreen, { | ||
InitiativeDetailsScreenRouteParams | ||
} from "../screens/InitiativeDetailScreen"; | ||
import { IDPayOnboardingMachineProvider } from "../xstate/provider"; | ||
|
||
export const IDPayOnboardingRoutes = { | ||
IDPAY_ONBOARDING_MAIN: "IDPAY_ONBOARDING_MAIN", | ||
IDPAY_ONBOARDING_INITIATIVE_DETAILS: "IDPAY_ONBOARDING_INITIATIVE_DETAILS" | ||
} as const; | ||
|
||
export type IDPayOnboardingParamsList = { | ||
[IDPayOnboardingRoutes.IDPAY_ONBOARDING_INITIATIVE_DETAILS]: InitiativeDetailsScreenRouteParams; | ||
}; | ||
|
||
const Stack = createStackNavigator<IDPayOnboardingParamsList>(); | ||
|
||
export const IDPayOnboardingNavigator = () => ( | ||
<IDPayOnboardingMachineProvider> | ||
<Stack.Navigator | ||
initialRouteName={ | ||
IDPayOnboardingRoutes.IDPAY_ONBOARDING_INITIATIVE_DETAILS | ||
} | ||
> | ||
<Stack.Screen | ||
name={IDPayOnboardingRoutes.IDPAY_ONBOARDING_INITIATIVE_DETAILS} | ||
component={InitiativeDetailsScreen} | ||
/> | ||
</Stack.Navigator> | ||
</IDPayOnboardingMachineProvider> | ||
); |
64 changes: 64 additions & 0 deletions
64
ts/features/idpay/onboarding/screens/InitiativeDetailScreen.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React from "react"; | ||
import { RouteProp, useRoute } from "@react-navigation/native"; | ||
import { Text } from "react-native"; | ||
import { SafeAreaView } from "react-native-safe-area-context"; | ||
import { useActor } from "@xstate/react"; | ||
import { IDPayOnboardingParamsList } from "../navigation/navigator"; | ||
import { useOnboardingMachineService } from "../xstate/provider"; | ||
|
||
type InitiativeDetailsScreenRouteParams = { | ||
serviceId: string; | ||
}; | ||
|
||
const InitiativeDetailsScreen = () => { | ||
const onboardingMachineService = useOnboardingMachineService(); | ||
|
||
const [state, send] = useActor(onboardingMachineService); | ||
|
||
const route = | ||
useRoute< | ||
RouteProp< | ||
IDPayOnboardingParamsList, | ||
"IDPAY_ONBOARDING_INITIATIVE_DETAILS" | ||
> | ||
>(); | ||
|
||
const { serviceId } = route.params; | ||
|
||
React.useEffect(() => { | ||
send({ | ||
type: "SELECT_INITIATIVE", | ||
serviceId | ||
}); | ||
}, [send, serviceId]); | ||
|
||
const content = React.useMemo(() => { | ||
if ( | ||
state.matches("WAITING_INITIATIVE_SELECTION") || | ||
state.matches("LOADING_INITIATIVE") | ||
) { | ||
return <Text>Loading...</Text>; | ||
} | ||
|
||
if ( | ||
state.matches("DISPLAYING_INITIATIVE") && | ||
state.context.initative !== undefined | ||
) { | ||
return <Text>Initiative ID: {state.context.initative.initiativeId}</Text>; | ||
} | ||
|
||
return null; | ||
}, [state]); | ||
|
||
return ( | ||
<SafeAreaView> | ||
<Text>ServiceID: {serviceId}</Text> | ||
<Text>State: {state.value}</Text> | ||
{content} | ||
</SafeAreaView> | ||
); | ||
}; | ||
|
||
export type { InitiativeDetailsScreenRouteParams }; | ||
|
||
export default InitiativeDetailsScreen; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { assign, createMachine } from "xstate"; | ||
import { InitiativeDto } from "../../../../../definitions/idpay/onboarding/InitiativeDto"; | ||
import { LOADING_TAG } from "../../../../utils/xstate"; | ||
|
||
// Context types | ||
export type Context = { | ||
serviceId?: string; | ||
initative?: InitiativeDto; | ||
}; | ||
|
||
// Events types | ||
type E_SELECT_INITIATIVE = { | ||
type: "SELECT_INITIATIVE"; | ||
serviceId: string; | ||
}; | ||
|
||
type Events = E_SELECT_INITIATIVE; | ||
|
||
// Services types | ||
type Services = { | ||
loadInitiative: { | ||
data: InitiativeDto; | ||
}; | ||
}; | ||
|
||
const createIDPayOnboardingMachine = () => | ||
/** @xstate-layout N4IgpgJg5mDOIC5QEkAiAFAggTQPoHkA5AIX0wCVVlCBxAOgHVNkAVam3a15TNgNQCiuAMoCAMgIDCbIgGJRE6Z0LdeyQQG0ADAF1EoAA4B7WAEsALqaMA7fSACeiAEwAWLQBoQAD2cB2AMx0vgCs-v6+TgCMAJxO-gAcTvEAvsmeaFh4RKQUVLR0YmR5HFxsaoKyEDZgdKbWAG5GANY1ADZGAIYQyNYWph2W9WDaekggxmaWNnaOCP7B8UGR8aFaAGzBLtFaEZ4+CE6+i+Errkdah-4uLqlpINZGEHB2GTgEJGSU7IzMbLTKqn4QgUUhkhDsEz60zG+w8DmcblS6Qwb2yn2KBSK7ABZSBEJMUNsMMQcNm80WvmWqw2Wx2TiRIFeWQ+uW+VGE6DEOGxpR4eLGkKmRNAsM8s2CWhcdHivjW0Uia3iK3CawZTPeOS+tHxkyswu8iAAtC54mLEJFgpE6Fp-GsXKEzjsbbdkkA */ | ||
createMachine( | ||
{ | ||
context: {}, | ||
tsTypes: {} as import("./machine.typegen").Typegen0, | ||
schema: { | ||
context: {} as Context, | ||
events: {} as Events, | ||
services: {} as Services | ||
}, | ||
predictableActionArguments: true, | ||
id: "IDPAY_ONBOARDING", | ||
initial: "WAITING_INITIATIVE_SELECTION", | ||
states: { | ||
WAITING_INITIATIVE_SELECTION: { | ||
tags: [LOADING_TAG], | ||
on: { | ||
SELECT_INITIATIVE: { | ||
target: "LOADING_INITIATIVE", | ||
actions: "selectInitiative" | ||
} | ||
} | ||
}, | ||
LOADING_INITIATIVE: { | ||
tags: [LOADING_TAG], | ||
invoke: { | ||
src: "loadInitiative", | ||
id: "loadInitiative", | ||
onDone: [ | ||
{ | ||
target: "DISPLAYING_INITIATIVE", | ||
actions: "loadInitiativeSuccess" | ||
} | ||
] | ||
} | ||
}, | ||
DISPLAYING_INITIATIVE: { | ||
type: "final" | ||
} | ||
} | ||
}, | ||
{ | ||
actions: { | ||
selectInitiative: assign((_, event) => ({ | ||
serviceId: event.serviceId | ||
})), | ||
loadInitiativeSuccess: assign((_, event) => ({ | ||
initative: event.data | ||
})) | ||
} | ||
} | ||
); | ||
|
||
type IDPayOnboardingMachineType = ReturnType< | ||
typeof createIDPayOnboardingMachine | ||
>; | ||
|
||
export type { IDPayOnboardingMachineType }; | ||
export { createIDPayOnboardingMachine }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import * as O from "fp-ts/lib/Option"; | ||
import React from "react"; | ||
import { InterpreterFrom } from "xstate"; | ||
import { useInterpret } from "@xstate/react"; | ||
import { useIOSelector } from "../../../../store/hooks"; | ||
import { sessionInfoSelector } from "../../../../store/reducers/authentication"; | ||
import { createOnboardingClient } from "../api/client"; | ||
import { | ||
IDPAY_API_TEST_TOKEN, | ||
IDPAY_API_UAT_BASEURL | ||
} from "../../../../config"; | ||
import { createServicesImplementation } from "./services"; | ||
import { | ||
createIDPayOnboardingMachine, | ||
IDPayOnboardingMachineType | ||
} from "./machine"; | ||
|
||
type OnboardingMachineContext = InterpreterFrom<IDPayOnboardingMachineType>; | ||
|
||
const OnboardingMachineContext = React.createContext<OnboardingMachineContext>( | ||
{} as OnboardingMachineContext | ||
); | ||
|
||
type Props = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
const IDPayOnboardingMachineProvider = (props: Props) => { | ||
const { children } = props; | ||
|
||
const sessionInfo = useIOSelector(sessionInfoSelector); | ||
|
||
if (O.isNone(sessionInfo)) { | ||
throw new Error("Session info is undefined"); | ||
} | ||
|
||
const token = | ||
IDPAY_API_TEST_TOKEN !== undefined | ||
? IDPAY_API_TEST_TOKEN | ||
: sessionInfo.value.bpdToken; | ||
|
||
const onboardingClient = createOnboardingClient(IDPAY_API_UAT_BASEURL, token); | ||
|
||
const machine = createIDPayOnboardingMachine(); | ||
|
||
const services = createServicesImplementation(onboardingClient); | ||
|
||
const machineService = useInterpret(machine, { | ||
services | ||
}); | ||
|
||
return ( | ||
<OnboardingMachineContext.Provider value={machineService}> | ||
{children} | ||
</OnboardingMachineContext.Provider> | ||
); | ||
}; | ||
|
||
const useOnboardingMachineService = () => | ||
React.useContext(OnboardingMachineContext); | ||
|
||
export { IDPayOnboardingMachineProvider, useOnboardingMachineService }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { pipe } from "fp-ts/lib/function"; | ||
import * as E from "fp-ts/lib/Either"; | ||
import { InitiativeDto } from "../../../../../definitions/idpay/onboarding/InitiativeDto"; | ||
import { OnboardingClient } from "../api/client"; | ||
import { Context } from "./machine"; | ||
|
||
const createServicesImplementation = (onboardingClient: OnboardingClient) => { | ||
const loadInitiative = async (context: Context) => { | ||
if (context.serviceId === undefined) { | ||
return Promise.reject("serviceId is undefined"); | ||
} | ||
|
||
const response = await onboardingClient.getInitiativeId({ | ||
serviceId: context.serviceId | ||
}); | ||
|
||
const data: Promise<InitiativeDto> = pipe( | ||
response, | ||
E.fold( | ||
_ => Promise.reject("error loading initiative"), | ||
_ => { | ||
if (_.status !== 200) { | ||
return Promise.reject("error loading initiative"); | ||
} | ||
return Promise.resolve(_.value); | ||
} | ||
) | ||
); | ||
|
||
return data; | ||
}; | ||
|
||
return { | ||
loadInitiative | ||
}; | ||
}; | ||
|
||
export { createServicesImplementation }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.