Skip to content

Commit 7c83597

Browse files
committed
feat: update demo page flow
1 parent c1e5590 commit 7c83597

4 files changed

Lines changed: 65 additions & 13 deletions

File tree

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,43 @@
1-
import axiosClient from '@api/axios-client';
1+
import axios, { AxiosInstance } from 'axios';
2+
import { urls } from 'configs';
3+
import { getDemoTokenStorage } from 'storage/demo-token';
24
import { Organization } from '@types';
35

46
export interface OrganizationDemoCreatorPayload {
57
name: string;
68
urlCode: string;
79
description?: string;
8-
ownerEmail: string;
10+
ownerEmail?: string;
911
}
1012

1113
export interface OrganizationDemoResponse {
1214
organization: Organization;
1315
}
1416

17+
const axiosClient: AxiosInstance = axios.create({
18+
baseURL: urls.WEB_API_ENDPOINT
19+
});
20+
21+
axiosClient.interceptors.request.use(
22+
config => {
23+
const authToken = getDemoTokenStorage();
24+
if (authToken) {
25+
config.headers['Authorization'] = `Bearer ${authToken.accessToken}`;
26+
}
27+
return config;
28+
},
29+
error => {
30+
return Promise.reject(error);
31+
}
32+
);
33+
1534
export const organizationDemoCreator = async (
16-
params?: OrganizationDemoCreatorPayload
35+
payload?: OrganizationDemoCreatorPayload
1736
): Promise<OrganizationDemoResponse> => {
1837
return axiosClient
1938
.post<OrganizationDemoResponse>(
2039
'/v1/environment/create_demo_organization',
21-
params
40+
payload
2241
)
2342
.then(response => response.data);
2443
};

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { useAuth } from './auth-context';
1212

1313
export const AuthDemoCallbackPage: FC = memo(() => {
1414
const { errorNotify } = useToast();
15-
const { setIsGoogleAuthError, setIsInitialLoading } = useAuth();
15+
const { setIsGoogleAuthError } = useAuth();
1616
const navigate = useNavigate();
1717
const location = useLocation();
1818
const query = location.search;
@@ -27,7 +27,6 @@ export const AuthDemoCallbackPage: FC = memo(() => {
2727
}
2828
} catch (error) {
2929
setIsGoogleAuthError(true);
30-
setIsInitialLoading(false);
3130
errorNotify(error);
3231
navigate(PAGE_PATH_DEMO_SITE);
3332
}
@@ -37,7 +36,6 @@ export const AuthDemoCallbackPage: FC = memo(() => {
3736
useEffect(() => {
3837
const { code, state } = queryString.parse(query);
3938
const cookieState = getCookieState();
40-
setIsInitialLoading(true);
4139

4240
if (!!code && typeof code === 'string' && state === cookieState) {
4341
onGoogleDemoHandler({

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { FormProvider, SubmitHandler, useForm } from 'react-hook-form';
2+
import { useNavigate } from 'react-router-dom';
3+
import { organizationDemoCreator } from '@api/organization';
24
import { yupResolver } from '@hookform/resolvers/yup';
5+
import { AxiosError } from 'axios';
6+
import { PAGE_PATH_ROOT } from 'constants/routing';
7+
import { useToast } from 'hooks';
38
import useFormSchema, { FormSchemaProps } from 'hooks/use-form-schema';
49
import { useTranslation } from 'i18n';
10+
import { clearDemoTokenStorage } from 'storage/demo-token';
511
import * as yup from 'yup';
612
import { onGenerateSlug } from 'utils/converts';
713
import Button from 'components/button';
@@ -33,7 +39,16 @@ const formSchema = ({ requiredMessage, translation }: FormSchemaProps) =>
3339
.required(requiredMessage)
3440
});
3541

36-
const DemoForm = ({ isDemoSiteEnabled }: { isDemoSiteEnabled?: boolean }) => {
42+
const DemoForm = ({
43+
isDemoSiteEnabled,
44+
onDemoAuthenticated
45+
}: {
46+
isDemoSiteEnabled?: boolean;
47+
onDemoAuthenticated: (v: boolean) => void;
48+
}) => {
49+
const { errorNotify, notify } = useToast();
50+
const navigate = useNavigate();
51+
3752
const { t } = useTranslation(['common', 'form', 'auth', 'message']);
3853

3954
const form = useForm({
@@ -52,11 +67,27 @@ const DemoForm = ({ isDemoSiteEnabled }: { isDemoSiteEnabled?: boolean }) => {
5267

5368
const onSubmit: SubmitHandler<AccessDemoForm> = async values => {
5469
try {
55-
console.log(values);
70+
const response = await organizationDemoCreator({
71+
name: values.organizationName,
72+
urlCode: values.organizationUrlCode
73+
});
74+
75+
if (response?.organization) {
76+
notify({
77+
message: t('message:collection-action-success', {
78+
collection: t('organization'),
79+
action: t('created')
80+
})
81+
});
82+
clearDemoTokenStorage();
83+
navigate(PAGE_PATH_ROOT);
84+
}
5685
} catch (error) {
57-
if (error) {
58-
console.log(error);
86+
if ((error as AxiosError).status === 401) {
87+
onDemoAuthenticated(false);
88+
clearDemoTokenStorage();
5989
}
90+
errorNotify(error);
6091
}
6192
};
6293

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useState } from 'react';
12
import { authenticationUrl } from '@api/auth';
23
import { useQueryDemoSiteStatus } from '@queries/demo-site-status';
34
import { urls } from 'configs';
@@ -19,8 +20,8 @@ const AccessDemoPage = () => {
1920
const authToken = getDemoTokenStorage();
2021
const { data: demoSiteStatusData, isLoading } = useQueryDemoSiteStatus();
2122

23+
const [isAuthenticated, setIsAuthenticated] = useState(Boolean(authToken));
2224
const isDemoSiteEnabled = demoSiteStatusData?.isDemoSiteEnabled;
23-
const isAuthenticated = Boolean(authToken);
2425

2526
const { onSubmit: onGoogleLoginHandler, submitting } = useSubmit(() => {
2627
const state = `${Date.now()}`;
@@ -73,7 +74,10 @@ const AccessDemoPage = () => {
7374
{`Sign in With Google`}
7475
</Button>
7576
) : (
76-
<DemoForm isDemoSiteEnabled={isDemoSiteEnabled} />
77+
<DemoForm
78+
isDemoSiteEnabled={isDemoSiteEnabled}
79+
onDemoAuthenticated={setIsAuthenticated}
80+
/>
7781
)}
7882
</>
7983
)}

0 commit comments

Comments
 (0)