-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLandingLoginView.tsx
121 lines (105 loc) · 3.74 KB
/
LandingLoginView.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import React, { useState } from 'react';
import DataswiftTm from '../../assets/icons/dataswift_tm.svg';
import { Input } from 'hmi';
import { pdaLookupWithEmail } from '../../services/HattersService';
import { PdaLookupResponse } from '../../types/Hatters';
import { config } from '../../app.config';
import { Link, useHistory } from 'react-router-dom';
import { newUserAccessToken } from '../../api/hatAPI';
import { loginWithToken } from '../../features/authentication/authenticationSlice';
import { HatClientService } from '../../services/HatClientService';
import Cookies from 'js-cookie';
import { useDispatch } from 'react-redux';
import FormatMessage from "../../features/messages/FormatMessage";
const LandingLoginView: React.FC = () => {
const history = useHistory();
const dispatch = useDispatch();
const [error, setError] = useState('');
const [email, setEmail] = useState('');
const [currentStep, setCurrentStep] = useState<1 | 2>(1);
const [response, setResponse] = useState<PdaLookupResponse | null>(null);
const [password, setPassword] = useState('');
const onNext = async () => {
try {
const res = await pdaLookupWithEmail(email);
if (res.parsedBody) {
setCurrentStep(2);
setResponse(res.parsedBody);
}
} catch (e) {
console.log(e);
setError('We can’t find an account that matches this information.');
}
};
const onLogin = async () => {
try {
if (!response?.hatName) return;
const res = await newUserAccessToken(response.hatName + '.' + response.hatCluster, response.hatName, password);
if (res.parsedBody) {
dispatch(loginWithToken(res.parsedBody.accessToken));
HatClientService.getInstance(res.parsedBody.accessToken);
const secure = window.location.protocol === 'https:';
Cookies.set('token', res.parsedBody.accessToken, { expires: 3, secure: secure, sameSite: 'strict' });
history.replace('/feed');
}
} catch (e) {
console.log(e);
}
};
const onSignup = async () => {
window.location.assign(`${config.links.pdaSignup}&email=${email}`);
};
return (
<div className="landing-login">
<img src={DataswiftTm} className="landing-dataswift-tm" alt="Dataswift" />
<h2>Log in to your account</h2>
{currentStep === 1 && (
<>
<Input
type="email"
id="email"
name="email"
autoComplete="email"
placeholder="Enter Email"
value={email}
onChange={(e) => {
setEmail(e.target.value);
setError('');
}}
hasError={!!error}
errorMessage={error}
/>
<button className="ds-hmi-btn ds-hmi-btn-primary" onClick={onNext}>
Next
</button>
</>
)}
{currentStep === 2 && (
<>
<div className="landing-login-email-text">{email}</div>
<Input
type="password"
id="password"
name="password"
autoComplete="password"
placeholder="Enter Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<button className="ds-hmi-btn ds-hmi-btn-primary" onClick={onLogin}>
Login
</button>
<Link className={'auth-login-btn-link'} to={'/auth/recover-password'}>
<FormatMessage id={'ds.auth.login.forgotPassword'} />
</Link>
</>
)}
<hr />
<div className="landing-dont-have-account">Don't have an account?</div>
<button className="ds-hmi-btn ds-hmi-btn-primary landing-btn-secondary" onClick={onSignup}>
Signup
</button>
</div>
);
};
export default LandingLoginView;