Skip to content

Commit

Permalink
feat: implemented redirects handling (#2715)
Browse files Browse the repository at this point in the history
* feat: implemented redirects handling

* fix: removed debugger & added loading screen to failed
  • Loading branch information
chesterkmr authored Sep 18, 2024
1 parent 26d22ad commit d763a65
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import posthog from 'posthog-js';

export const fetchUser = async (): Promise<TUser> => {
const user = await request.get('collection-flow/user').json<TUser>();

if (user) {
try {
posthog.identify(user.id, {
Expand All @@ -21,6 +22,7 @@ export const fetchUser = async (): Promise<TUser> => {
console.error('Error identifying user in PostHog:', error);
}
}

return user;
};

Expand Down
12 changes: 10 additions & 2 deletions apps/kyb-app/src/domains/collection-flow/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export interface Document {
revisionReason?: string;
rejectionReason?: string;
};
pages?: { ballerineFileId: string }[];
pages?: Array<{ ballerineFileId: string }>;
}

export interface UBO {
Expand Down Expand Up @@ -126,7 +126,7 @@ export interface UIPage {
name: string;
number: number;
stateName: string;
elements: UIElement<AnyObject>[];
elements: Array<UIElement<AnyObject>>;
actions: Action[];
pageValidation?: Rule[];
}
Expand All @@ -136,6 +136,13 @@ export interface UISchemaConfig {
supportedLanguages: string[];
}

export interface UIOptions {
redirectUrls?: {
success?: string;
failure?: string;
};
}

export interface UISchema {
id: string;
config: UISchemaConfig;
Expand All @@ -147,6 +154,7 @@ export interface UISchema {
definition: AnyObject;
extensions: AnyObject;
};
uiOptions?: UIOptions;
}

export * from './ui-schema.types';
1 change: 1 addition & 0 deletions apps/kyb-app/src/hooks/useUIOptionsRedirect/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useUIOptionsRedirect';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useLanguage } from '@/hooks/useLanguage';
import { useUISchemasQuery } from '@/hooks/useUISchemasQuery';
import { useEffect } from 'react';

export const useUIOptionsRedirect = (state: 'success' | 'failure') => {
const { data } = useUISchemasQuery(useLanguage());

useEffect(() => {
if (data?.uiOptions?.redirectUrls?.[state]) {
location.href = data.uiOptions.redirectUrls?.[state] as string;
}
}, [data, state]);
};
24 changes: 18 additions & 6 deletions apps/kyb-app/src/pages/CollectionFlow/CollectionFlow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import { useFlowContextQuery } from '@/hooks/useFlowContextQuery';
import { useLanguageParam } from '@/hooks/useLanguageParam/useLanguageParam';
import { withSessionProtected } from '@/hooks/useSessionQuery/hocs/withSessionProtected';
import { useUISchemasQuery } from '@/hooks/useUISchemasQuery';
import { LoadingScreen } from '@/pages/CollectionFlow/components/atoms/LoadingScreen';
import { Approved } from '@/pages/CollectionFlow/components/pages/Approved';
import { Failed } from '@/pages/CollectionFlow/components/pages/Failed';
import { Rejected } from '@/pages/CollectionFlow/components/pages/Rejected';
import { Success } from '@/pages/CollectionFlow/components/pages/Success';
import { AnyObject } from '@ballerine/ui';
Expand Down Expand Up @@ -78,6 +80,9 @@ export const useCompleteLastStep = () => {
}, [elements, refetch, state, stateApi]);
};

const isSuccess = (state: string) => state === 'success' || state === 'finish';
const isFailed = (state: string) => state === 'failed';

export const CollectionFlow = withSessionProtected(() => {
const { language } = useLanguageParam();
const { data: schema } = useUISchemasQuery(language);
Expand All @@ -102,6 +107,7 @@ export const CollectionFlow = withSessionProtected(() => {
filteredNonEmptyErrors?.[0]?.stateName ||
context?.flowConfig?.appState ||
elements?.at(0)?.stateName;

if (!appState) return null;

return {
Expand Down Expand Up @@ -131,6 +137,7 @@ export const CollectionFlow = withSessionProtected(() => {
}, [customer?.logoImageUri]);

if (initialContext?.flowConfig?.appState === 'approved') return <Approved />;

if (initialContext?.flowConfig?.appState == 'rejected') return <Rejected />;

return definition && context ? (
Expand All @@ -142,10 +149,15 @@ export const CollectionFlow = withSessionProtected(() => {
extensions={schema?.definition.extensions}
definition={definition as State}
>
{({ state, stateApi }) =>
state === 'finish' ? (
<Success />
) : (
{({ state, stateApi }) => {
// Temp state, has to be resolved to success or failure by plugins
if (state === 'done') return <LoadingScreen />;

if (isSuccess(state)) return <Success />;

if (isFailed(state)) return <Failed />;

return (
<DynamicUI.PageResolver state={state} pages={elements ?? []}>
{({ currentPage }) => {
return currentPage ? (
Expand Down Expand Up @@ -244,8 +256,8 @@ export const CollectionFlow = withSessionProtected(() => {
) : null;
}}
</DynamicUI.PageResolver>
)
}
);
}}
</DynamicUI.StateManager>
</DynamicUI>
) : null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { LoadingScreen } from '@/common/components/molecules/LoadingScreen';
import { useUIOptionsRedirect } from '@/hooks/useUIOptionsRedirect';

export const Failed = () => {
useUIOptionsRedirect('failure');

return <LoadingScreen />;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Failed';
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useTranslation } from 'react-i18next';
import { useCustomer } from '@/components/providers/CustomerProvider';
import { useAppExit } from '@/hooks/useAppExit/useAppExit';
import { withSessionProtected } from '@/hooks/useSessionQuery/hocs/withSessionProtected';
import { useUIOptionsRedirect } from '@/hooks/useUIOptionsRedirect';
import { Button, Card } from '@ballerine/ui';

export const Success = withSessionProtected(() => {
Expand All @@ -12,6 +13,8 @@ export const Success = withSessionProtected(() => {

const { exit, isExitAvailable } = useAppExit();

useUIOptionsRedirect('success');

return (
<div className="flex h-full items-center justify-center">
<Card className="w-full max-w-[646px] p-12">
Expand Down
2 changes: 1 addition & 1 deletion services/workflows-service/prisma/data-migrations
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class CollectionFlowService {
return {
id: workflowDefinition.id,
config: workflowDefinition.config,
uiOptions: uiDefintion.uiOptions,
uiSchema: {
// @ts-expect-error - error from Prisma types fix
elements: this.traverseUiSchema(
Expand Down

0 comments on commit d763a65

Please sign in to comment.