Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reworked exit from app for iframe scenarios #2095

Merged
merged 4 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/kyb-app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LoadingScreen } from '@/common/components/molecules/LoadingScreen';
import { APP_LANGUAGE_QUERY_KEY } from '@/common/consts/consts';
import { CustomerProviderFallback } from '@/components/molecules/CustomerProviderFallback';
import { AppLoadingContainer } from '@/components/organisms/AppLoadingContainer';
import { CustomerProvider } from '@/components/providers/CustomerProvider';
Expand All @@ -11,7 +12,7 @@ import * as Sentry from '@sentry/react';
import { RouterProvider } from 'react-router-dom';

export const App = () => {
const language = new URLSearchParams(window.location.search).get('lng') || 'en';
const language = new URLSearchParams(window.location.search).get(APP_LANGUAGE_QUERY_KEY) || 'en';

const dependancyQueries = [
useCustomerQuery(),
Expand Down
1 change: 1 addition & 0 deletions apps/kyb-app/src/common/consts/consts.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const ARRAY_VALUE_INDEX_PLACEHOLDER = `{INDEX}`;
export const APP_LANGUAGE_QUERY_KEY = 'lng';
9 changes: 4 additions & 5 deletions apps/kyb-app/src/components/layouts/AppShell/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { usePageResolverContext } from '@/components/organisms/DynamicUI/PageRes
import { useStateManagerContext } from '@/components/organisms/DynamicUI/StateManager/components/StateProvider';
import { useDynamicUIContext } from '@/components/organisms/DynamicUI/hooks/useDynamicUIContext';
import { useCustomer } from '@/components/providers/CustomerProvider';
import { useFlowTracking } from '@/hooks/useFlowTracking';
import { useAppExit } from '@/hooks/useAppExit/useAppExit';
import { ctw } from '@ballerine/ui';

export const Navigation = () => {
Expand All @@ -15,7 +15,7 @@ export const Navigation = () => {
const { stateApi } = useStateManagerContext();
const { currentPage } = usePageResolverContext();
const { customer } = useCustomer();
const { trackExit } = useFlowTracking();
const exitFromApp = useAppExit();

const isFirstStep = currentPage?.number === 1;
const isDisabled = state.isLoading;
Expand All @@ -26,10 +26,9 @@ export const Navigation = () => {
return;
}

trackExit();
window.location.href = customer?.websiteUrl!;
exitFromApp();
return;
}, [stateApi]);
}, [stateApi, exitFromApp]);

return (
<button
Expand Down
7 changes: 6 additions & 1 deletion apps/kyb-app/src/domains/collection-flow/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,14 @@ export interface UIPage {
pageValidation?: Rule[];
}

export interface UISchemaConfig {
kybOnExitAction?: 'send-event' | 'redirect-to-customer-portal';
supportedLanguages: string[];
}

export interface UISchema {
id: string;
config: Record<string, unknown>;
config: UISchemaConfig;
uiSchema: {
elements: UIPage[];
};
Expand Down
29 changes: 29 additions & 0 deletions apps/kyb-app/src/hooks/useAppExit/useAppExit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useCustomerQuery } from '@/hooks/useCustomerQuery';
import { useFlowTracking } from '@/hooks/useFlowTracking';
import { useLanguage } from '@/hooks/useLanguage';
import { useUISchemasQuery } from '@/hooks/useUISchemasQuery';
import { useCallback } from 'react';

export const useAppExit = () => {
const appLanguage = useLanguage();
const { data: uiSchema } = useUISchemasQuery(appLanguage);
const { trackExit } = useFlowTracking();
const { customer } = useCustomerQuery();

const { kybOnExitAction = 'send-event' } = uiSchema?.config || {};

const exit = useCallback(() => {
if (kybOnExitAction === 'send-event') {
trackExit();
return;
}

if (kybOnExitAction === 'redirect-to-customer-portal') {
if (customer?.websiteUrl) {
location.href = customer?.websiteUrl;
}
}
}, [trackExit, customer]);

return exit;
};
7 changes: 6 additions & 1 deletion apps/kyb-app/src/hooks/useFlowTracking/useFlowTracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import { useCallback } from 'react';

export const useFlowTracking = () => {
const trackExit = useCallback(() => {
window.parent.postMessage('ballerine.collection-flow.back-button-pressed', '*');
const event = 'ballerine.collection-flow.back-button-pressed';
console.log(`Sending event: ${event}`);
window.parent.postMessage(event, '*');
}, []);

const trackFinish = useCallback(() => {
const event = 'ballerine.collection-flow.finish-button-pressed';
console.log(`Sending event: ${event}`);

window.parent.postMessage('ballerine.collection-flow.finish-button-pressed', '*');
}, []);

Expand Down
1 change: 1 addition & 0 deletions apps/kyb-app/src/hooks/useLanguage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useLanguage';
12 changes: 12 additions & 0 deletions apps/kyb-app/src/hooks/useLanguage/useLanguage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { APP_LANGUAGE_QUERY_KEY } from '@/common/consts/consts';
import { useSearchParams } from 'react-router-dom';

const defaultQuery = {
lng: 'en',
};

export const useLanguage = () => {
const [location] = useSearchParams(defaultQuery);

return location.get(APP_LANGUAGE_QUERY_KEY) || defaultQuery.lng;
};
2 changes: 1 addition & 1 deletion apps/kyb-app/src/pages/CollectionFlow/CollectionFlow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export const CollectionFlow = withSessionProtected(() => {
<AppShell.Sidebar>
<div className="flex h-full flex-col">
<div className="flex h-full flex-1 flex-col">
<div className="flex flex-row justify-between whitespace-nowrap gap-2 pb-10">
<div className="flex flex-row justify-between gap-2 whitespace-nowrap pb-10">
<AppShell.Navigation />
<div>
<AppShell.LanguagePicker />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import DOMPurify from 'dompurify';
import { useTranslation } from 'react-i18next';

import { useCustomer } from '@/components/providers/CustomerProvider';
import { useFlowTracking } from '@/hooks/useFlowTracking';
import { useAppExit } from '@/hooks/useAppExit/useAppExit';
import { withSessionProtected } from '@/hooks/useSessionQuery/hocs/withSessionProtected';
import { Button, Card } from '@ballerine/ui';

export const Approved = withSessionProtected(() => {
const { t } = useTranslation();
const { customer } = useCustomer();
const { trackExit } = useFlowTracking();
const exitFromApp = useAppExit();

return (
<div className="flex h-full items-center justify-center">
Expand All @@ -29,15 +29,9 @@ export const Approved = withSessionProtected(() => {
{t('approved.content', { companyName: customer?.displayName })}
</p>
</div>
{customer?.displayName && customer?.websiteUrl && (
{customer?.displayName && (
<div className="flex justify-center">
<Button
variant="secondary"
onClick={() => {
trackExit();
location.href = customer.websiteUrl;
}}
>
<Button variant="secondary" onClick={exitFromApp}>
{t('backToPortal', { companyName: customer.displayName })}
</Button>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import DOMPurify from 'dompurify';
import { useTranslation } from 'react-i18next';

import { useCustomer } from '@/components/providers/CustomerProvider';
import { useFlowTracking } from '@/hooks/useFlowTracking';
import { useAppExit } from '@/hooks/useAppExit/useAppExit';
import { withSessionProtected } from '@/hooks/useSessionQuery/hocs/withSessionProtected';
import { Button, Card } from '@ballerine/ui';

export const Success = withSessionProtected(() => {
const { t } = useTranslation();
const { customer } = useCustomer();

const { trackExit } = useFlowTracking();
const exitFromApp = useAppExit();

return (
<div className="flex h-full items-center justify-center">
Expand All @@ -29,15 +29,9 @@ export const Success = withSessionProtected(() => {
{t('success.content')}
</h2>
</div>
{customer?.displayName && customer?.websiteUrl && (
{customer?.displayName && (
<div className="flex justify-center">
<Button
variant="secondary"
onClick={() => {
trackExit();
location.href = customer.websiteUrl;
}}
>
<Button variant="secondary" onClick={exitFromApp}>
{t('backToPortal', { companyName: customer.displayName })}
</Button>
</div>
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 @@ -53,6 +53,7 @@ export const ConfigSchema = z
.boolean()
.optional()
.describe('Indicates if workflow could be created in backoffice'),
kybOnExitAction: z.enum(['send-event', 'redirect-to-customer-portal']).optional(),
})
.strict()
.optional();
Expand Down
Loading