994255cd3a666372fb33a289f48354c099b5c9c2
[clamp.git] / ui-react / src / components / app / login / BasicAuthLogin.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23 import React from 'react';
24 import styled from 'styled-components';
25 import LoopService from '../../backend_communication/LoopService';
26
27 const LoginHeaderStyle = styled.span`
28   font-size: 20px;
29   font-weight: bold;
30   padding-left: 10px;
31         color: ${props => props.theme.loopViewerHeaderFontColor};
32 `
33 const LoginDivStyle = styled.div`
34   font-size: 12px;
35         background-color: ${props => props.theme.loopViewerHeaderBackgroundColor};
36         padding: 10px 10px;
37         color: ${props => props.theme.loopViewerHeaderFontColor};
38 `
39 const LoginSubmitButtonStyle = styled.button`
40   font-size: 12px;
41         padding: 5px 10px;
42         color: ${props => props.theme.loopViewerHeaderFontColor};
43   border: 2px solid;
44   border-radius: 8px;
45 `
46 const LoginTextInputStyle = styled.input`
47   padding: 10px 10px;
48   margin-left: 20px;
49   border: 1px solid #ced4da;
50   border-radius: 3px;
51         color: ${props => props.theme.loopViewerHeaderFontColor};
52 `
53
54 export default class BasicAuthLogin extends React.Component {
55     constructor(props) {
56         super(props);
57         this.handleSubmit = this.handleSubmit.bind(this);
58         this.handleChange = this.handleChange.bind(this);
59         console.log('BasicAuthLogin');
60         this.state = {
61             username: '',
62             password: '',
63             submitted: 'false'
64         };
65     }
66
67     handleChange(e) {
68         const { name, value } = e.target;
69         this.setState({ [name]: value });
70     }
71
72     handleSubmit(e) {
73             e.preventDefault();
74             this.setState({ submitted: true });
75             const { username, password } = this.state;
76             LoopService.login(username, password)
77             .then(
78               user => {
79                   const { from } = { from: { pathname: "/" } };
80                   this.props.history.push(from);
81               },
82               error => {
83                   const { from } = { from: { pathname: "/loginFailed" } };
84                   this.props.history.push(from);
85                   console.log ("Basic login failed");
86               }
87         );
88     }
89
90     render() {
91       const { username, password, submitted} = this.state;
92         return (
93             <div>
94                 <LoginHeaderStyle>Login</LoginHeaderStyle>
95                 <form name="form" onSubmit={this.handleSubmit}>
96                     <LoginDivStyle className={(submitted && !username ? ' has-error' : '')}>
97                         <label htmlFor="username">Username</label>
98                         <LoginTextInputStyle name="username" value={username} onChange={this.handleChange} />
99                         {submitted && !username &&
100                             <div className="help-block">Username is required</div>
101                         }
102                     </LoginDivStyle>
103                     <LoginDivStyle className={(submitted && !password ? ' has-error' : '')}>
104                         <label htmlFor="password">Password</label>
105                         <LoginTextInputStyle type="password" name="password" value={password} onChange={this.handleChange} />
106                         {submitted && !password &&
107                             <div className="help-block">Password is required</div>
108                         }
109                     </LoginDivStyle>
110                     <LoginDivStyle>
111                         <LoginSubmitButtonStyle className="btn btn-primary">Login</LoginSubmitButtonStyle>
112                     </LoginDivStyle>
113                 </form>
114             </div>
115         );
116     }
117 }