forked from webclinic017/passiv-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemoLoginPage.tsx
66 lines (59 loc) · 1.7 KB
/
DemoLoginPage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import React, { useState } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { loginSucceeded, logout } from '../actions';
import { postData } from '../api';
import { selectLoggedIn, selectIsDemo } from '../selectors';
import { H1 } from '../styled/GlobalElements';
import DemoNotesContent from '../components/DemoNotesContent';
import ShadowBox from '../styled/ShadowBox';
import { Button } from '../styled/Button';
import { push } from 'connected-react-router';
type Props = {
location: any;
};
const DemoLoginPage = ({ location }: Props) => {
const loggedIn = useSelector(selectLoggedIn);
const isDemo = useSelector(selectIsDemo);
const dispatch = useDispatch();
const [triggered, setTriggered] = useState(false);
const [submitting, setSubmitting] = useState(false);
const demoLogin = () => {
setTriggered(true);
setSubmitting(true);
postData('/api/v1/auth/demo/', {})
.then(response => {
setSubmitting(false);
dispatch(loginSucceeded(response));
})
.catch(error => {
setSubmitting(false);
});
};
if (loggedIn) {
if (isDemo != null && !isDemo && !triggered) {
dispatch(logout());
} else {
return (
<ShadowBox background="#04a287">
<DemoNotesContent />
<Button
onClick={() => {
dispatch(push('/app/dashboard'));
}}
>
Continue
</Button>
</ShadowBox>
);
}
}
if (!triggered) {
demoLogin();
}
if (submitting) {
return <H1>Initializing your demo account...</H1>;
} else {
return <H1>Oops, we weren't able to get you a demo account!</H1>;
}
};
export default DemoLoginPage;