From 138f9437ed36ea920335f59354ef7d012aec5cd1 Mon Sep 17 00:00:00 2001 From: soufianeAourinmouche Date: Thu, 30 Jan 2020 12:52:01 +0100 Subject: [PATCH] #2 creating UI for log in --- front-end/package.json | 3 +- front-end/src/API/LoginAPI.js | 0 front-end/src/Actions/UserActions.js | 21 ++++++ front-end/src/Components/Login/Login.jsx | 95 ++++++++++++++++++++++++ front-end/src/Routes/index.js | 3 + front-end/src/Services/UserService.js | 29 ++++++++ 6 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 front-end/src/API/LoginAPI.js create mode 100644 front-end/src/Actions/UserActions.js create mode 100644 front-end/src/Components/Login/Login.jsx create mode 100644 front-end/src/Services/UserService.js diff --git a/front-end/package.json b/front-end/package.json index f058190..f938f45 100644 --- a/front-end/package.json +++ b/front-end/package.json @@ -30,6 +30,7 @@ ] }, "devDependencies": { - "react-router-dom": "^5.1.2" + "react-router-dom": "^5.1.2", + "react-redux": "^4.4.5" } } diff --git a/front-end/src/API/LoginAPI.js b/front-end/src/API/LoginAPI.js new file mode 100644 index 0000000..e69de29 diff --git a/front-end/src/Actions/UserActions.js b/front-end/src/Actions/UserActions.js new file mode 100644 index 0000000..b239cce --- /dev/null +++ b/front-end/src/Actions/UserActions.js @@ -0,0 +1,21 @@ +import { userService } from '../Services/UserService'; + +// export further user actions here, like logout, delete account... +export const userActions = { + login +}; + + +function login(username, password) { +console.log("dans login !"); + return dispatch => { + dispatch(request({ username })); // dispatch request action + console.log("ca n'arrive pas là"); + userService.login(username, password); + }; + + function request(user) { return { type: "LOGIN_REQUEST", user } } + function onSuccess(user) { return { type: "LOGIN_SUCCESS", user } } + function onFailure(error) { return { type: "LOGIN_FAILURE", error } } +} + diff --git a/front-end/src/Components/Login/Login.jsx b/front-end/src/Components/Login/Login.jsx new file mode 100644 index 0000000..24ed91e --- /dev/null +++ b/front-end/src/Components/Login/Login.jsx @@ -0,0 +1,95 @@ +import React from 'react'; + +import { userActions } from '../../Actions/UserActions'; +import { connect } from 'react-redux'; + +class LoginForm extends React.Component { + + constructor(props) { + super(props); + + this.state = { + username: '', + password: '', + submitted: false + }; + + this.handleChange = this.handleChange.bind(this); + this.handleSubmit = this.handleSubmit.bind(this); + } + + handleChange(e) { + const { name, value } = e.target; + this.setState({ [name]: value }); + console.log(`handle change on : {${name} , ${value}}`); + } + + handleSubmit(e) { + e.preventDefault(); + console.log("form submitted"); + + this.setState({ submitted: true }); + let { username, password } = this.state; + + console.log(`submitted state : username=${username}, password=${password}, submitted=${this.state.submitted}`); + + if (username && password) { + //this.props.login(username, password); + userActions.login(username, password); + } + else { + console.log("username and password should not be empty"); + } + } + + componentDidMount() { + if (this.props.username === undefined) { + console.log("username udefined"); + } + else { + console.log(`LoginForm did mount, username : ${this.props.username}`); + } + } + + render() { + const { loggingIn } = this.props; + const { username, password, submitted } = this.state; + return ( + +
+

Login

+
+
+ + +
+
+ + +
+
+ +
+
+
+ + ); + } +} + + +/*function mapState(state) { + const { loggingIn } = state.authentication; + return { loggingIn }; +} + +const actions = { + login: userActions.login, +}; + + +const connectLogin = connect(mapState, actions)(LoginForm); +export { connectLogin as LoginForm }; +*/ + +export default LoginForm; \ No newline at end of file diff --git a/front-end/src/Routes/index.js b/front-end/src/Routes/index.js index 810b018..804be53 100644 --- a/front-end/src/Routes/index.js +++ b/front-end/src/Routes/index.js @@ -5,6 +5,8 @@ import Home from '../Components/Home'; // Import routing components import { BrowserRouter as Router, Switch, Route } from 'react-router-dom'; +import LoginForm from '../Components/Login/Login'; +//import { LoginForm } from '../Components/Login/Login'; export default class Routes extends React.Component{ @@ -13,6 +15,7 @@ export default class Routes extends React.Component{ +
No page found
}>
diff --git a/front-end/src/Services/UserService.js b/front-end/src/Services/UserService.js new file mode 100644 index 0000000..f2a4d7d --- /dev/null +++ b/front-end/src/Services/UserService.js @@ -0,0 +1,29 @@ + +// export further user actions here, like logout, delete account... +export const userService = { + login +}; + +function login(username, password) { + console.log(`Trying to log {${userService}, ${password}}`); + + + // need to check with @Walid if method is POST or GET + const requestOptions = { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ username, password }) + }; + +/* return fetch(`/endPointForLogin`, requestOptions) + .then(handleResponse) + .then(user => { + // create user model with profile data + + return user; + }); +*/ + + return "OK FOR" + +}