Skip to content

Commit c1e5590

Browse files
committed
feat: udpate demo page flow
1 parent fb73b75 commit c1e5590

9 files changed

Lines changed: 84 additions & 24 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axiosClient from '@api/axios-client';
2-
import { AuthTypeMap, AuthResponse } from '@types';
2+
import { AuthTypeMap, DemoAuthResponse } from '@types';
33

44
export interface ExchangeDemoTokenPayload {
55
code: string;
@@ -9,8 +9,8 @@ export interface ExchangeDemoTokenPayload {
99

1010
export const exchangeDemoToken = async (
1111
payload: ExchangeDemoTokenPayload
12-
): Promise<AuthResponse> => {
12+
): Promise<DemoAuthResponse> => {
1313
return axiosClient
14-
.post<AuthResponse>('/v1/exchange_demo_token', payload)
14+
.post<DemoAuthResponse>('/v1/exchange_demo_token', payload)
1515
.then(response => response.data);
1616
};

ui/dashboard/src/@api/organization/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './organization-archive';
44
export * from './organization-unarchive';
55
export * from './organization-updater';
66
export * from './organization-details-fetcher';
7+
export * from './organization-demo-creator';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import axiosClient from '@api/axios-client';
2+
import { Organization } from '@types';
3+
4+
export interface OrganizationDemoCreatorPayload {
5+
name: string;
6+
urlCode: string;
7+
description?: string;
8+
ownerEmail: string;
9+
}
10+
11+
export interface OrganizationDemoResponse {
12+
organization: Organization;
13+
}
14+
15+
export const organizationDemoCreator = async (
16+
params?: OrganizationDemoCreatorPayload
17+
): Promise<OrganizationDemoResponse> => {
18+
return axiosClient
19+
.post<OrganizationDemoResponse>(
20+
'/v1/environment/create_demo_organization',
21+
params
22+
)
23+
.then(response => response.data);
24+
};

ui/dashboard/src/@types/auth.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export interface AuthResponse {
4141
token: AuthToken;
4242
}
4343

44+
export interface DemoAuthResponse {
45+
demoCreationToken: AuthToken;
46+
}
47+
4448
export interface UserInfoForm {
4549
firstName: string;
4650
lastName: string;

ui/dashboard/src/auth/auth-demo-callback.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { PAGE_PATH_DEMO_SITE } from 'constants/routing';
66
import { getCookieState } from 'cookie';
77
import { useSubmit, useToast } from 'hooks';
88
import queryString from 'query-string';
9+
import { setDemoTokenStorage } from 'storage/demo-token';
910
import { AppLoading } from 'app';
1011
import { useAuth } from './auth-context';
1112

@@ -20,8 +21,9 @@ export const AuthDemoCallbackPage: FC = memo(() => {
2021
async (payload: ExchangeTokenPayload) => {
2122
try {
2223
const response = await exchangeDemoToken(payload);
23-
if (response.token) {
24-
navigate(`${PAGE_PATH_DEMO_SITE}?token=${response.token}`);
24+
if (response.demoCreationToken) {
25+
setDemoTokenStorage(response.demoCreationToken);
26+
navigate(PAGE_PATH_DEMO_SITE);
2527
}
2628
} catch (error) {
2729
setIsGoogleAuthError(true);

ui/dashboard/src/hooks/use-toast.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,20 @@ export const useToast = () => {
4444
);
4545

4646
const errorNotify = (error?: unknown, message?: string) => {
47-
const { message: _message, status } = (error as AxiosError) || {};
47+
const {
48+
message: _message,
49+
status,
50+
response
51+
} = (error as AxiosError<{ message?: string }>) || {};
4852
return notify({
4953
messageType: 'error',
5054
message:
5155
status === 409
5256
? t('same-data-exists')
53-
: message || _message || t('something-went-wrong')
57+
: message ||
58+
response?.data?.message ||
59+
_message ||
60+
t('something-went-wrong')
5461
});
5562
};
5663
return {

ui/dashboard/src/pages/demo/demo-form.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const formSchema = ({ requiredMessage, translation }: FormSchemaProps) =>
3434
});
3535

3636
const DemoForm = ({ isDemoSiteEnabled }: { isDemoSiteEnabled?: boolean }) => {
37-
const { t } = useTranslation(['common', 'form', 'message']);
37+
const { t } = useTranslation(['common', 'form', 'auth', 'message']);
3838

3939
const form = useForm({
4040
resolver: yupResolver(useFormSchema(formSchema)),
@@ -68,7 +68,7 @@ const DemoForm = ({ isDemoSiteEnabled }: { isDemoSiteEnabled?: boolean }) => {
6868
name="organizationName"
6969
render={({ field }) => (
7070
<Form.Item>
71-
<Form.Label required>{t('organization-name')}</Form.Label>
71+
<Form.Label required>{t('auth:organization-name')}</Form.Label>
7272
<Form.Control>
7373
<Input
7474
placeholder={`${t('form:placeholder-name')}`}
@@ -113,7 +113,7 @@ const DemoForm = ({ isDemoSiteEnabled }: { isDemoSiteEnabled?: boolean }) => {
113113
<Form.Item>
114114
<Form.Control>
115115
<Checkbox
116-
title={t('demo-agree-terms')}
116+
title={t('auth:demo-agree-terms')}
117117
checked={field.value}
118118
onCheckedChange={field.onChange}
119119
/>

ui/dashboard/src/pages/demo/index.tsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { useEffect, useState } from 'react';
2-
import { useLocation } from 'react-router-dom';
31
import { authenticationUrl } from '@api/auth';
42
import { useQueryDemoSiteStatus } from '@queries/demo-site-status';
53
import { urls } from 'configs';
64
import { setCookieState } from 'cookie';
75
import { useSubmit } from 'hooks';
86
import { useTranslation } from 'i18n';
9-
import queryString from 'query-string';
7+
import { getDemoTokenStorage } from 'storage/demo-token';
108
import { cn } from 'utils/style';
119
import { IconGoogle } from '@icons';
1210
import AuthWrapper from 'pages/signin/elements/auth-wrapper';
@@ -17,14 +15,12 @@ import DemoForm from './demo-form';
1715

1816
const AccessDemoPage = () => {
1917
const { t } = useTranslation(['common', 'auth', 'message']);
20-
const location = useLocation();
21-
const query = location.search;
2218

23-
const [userToken, setUserToken] = useState('');
19+
const authToken = getDemoTokenStorage();
2420
const { data: demoSiteStatusData, isLoading } = useQueryDemoSiteStatus();
2521

2622
const isDemoSiteEnabled = demoSiteStatusData?.isDemoSiteEnabled;
27-
const isAuthenticated = Boolean(userToken);
23+
const isAuthenticated = Boolean(authToken);
2824

2925
const { onSubmit: onGoogleLoginHandler, submitting } = useSubmit(() => {
3026
const state = `${Date.now()}`;
@@ -41,13 +37,6 @@ const AccessDemoPage = () => {
4137
});
4238
});
4339

44-
useEffect(() => {
45-
const { token } = queryString.parse(query);
46-
if (token && typeof token === 'string') {
47-
setUserToken(token);
48-
}
49-
}, [query]);
50-
5140
return (
5241
<AuthWrapper>
5342
{isLoading ? (
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { type Nullable } from 'option-t/nullable';
2+
import { AuthToken } from '@types';
3+
4+
const KEY = 'demo_token';
5+
6+
export const getDemoTokenStorage = (): Nullable<AuthToken> => {
7+
try {
8+
const tokenStr = window.localStorage.getItem(KEY);
9+
if (tokenStr) {
10+
const token = JSON.parse(tokenStr);
11+
return token;
12+
}
13+
} catch (error) {
14+
console.error(error);
15+
}
16+
return null;
17+
};
18+
19+
export const setDemoTokenStorage = (token: AuthToken): void => {
20+
try {
21+
window.localStorage.setItem(KEY, JSON.stringify(token));
22+
} catch (error) {
23+
console.error(error);
24+
}
25+
};
26+
27+
export const clearDemoTokenStorage = (): void => {
28+
try {
29+
window.localStorage.removeItem(KEY);
30+
} catch (error) {
31+
console.error(error);
32+
}
33+
};

0 commit comments

Comments
 (0)