From 1d0a4b82ec9dbb8dc9ade91d54f7327624410331 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 11 Sep 2018 10:03:37 +0200 Subject: [PATCH] Default Value added to first Input on the form --- client/src/auth/Auth.js | 10 ++-- client/src/components/LoggedInUser.js | 6 +-- client/src/components/NavBar.js | 2 +- client/src/components/UserUpdate.js | 67 ++++++++++++++++++++------- 4 files changed, 59 insertions(+), 26 deletions(-) diff --git a/client/src/auth/Auth.js b/client/src/auth/Auth.js index 7a2ad9abb..44488c4ef 100644 --- a/client/src/auth/Auth.js +++ b/client/src/auth/Auth.js @@ -102,7 +102,7 @@ class Auth { this.signinOrCreateAccount({ ...data }) this.cb(data) console.log(authResult.idTokenPayload) - setItem('auth.user', JSON.stringify(authResult.idTokenPayload)) + setItem('authUser', JSON.stringify(authResult.idTokenPayload)) if (window.location.href.includes(`callback`)) { window.location.href = '/' } @@ -126,7 +126,7 @@ class Auth { }) console.log('signinOrCreateAccount auth: ', await data) - setItem('auth.user', JSON.stringify(data.data.authenticate)) + setItem('authUser', JSON.stringify(data.data.authenticate)) if (window.location.href.includes(`callback`)) { window.location.href = '/' } else { @@ -143,7 +143,7 @@ class Auth { removeItem('access_token') removeItem('id_token') removeItem('expires_at') - removeItem('auth.user') + removeItem('authUser') removeItem('auth0.ssodata') window.localStorage.clear() deleteCookie('ajs_anonymous_id') @@ -156,7 +156,7 @@ class Auth { async isAuthenticated () { // check session and run Auth0 SS0 - const getUser = await getItem('auth.user') + const getUser = await getItem('authUser') let user = JSON.parse(getUser) if (validObject(user, 'exp') && new Date().getTime() < (user.exp * 1000)) { @@ -168,7 +168,7 @@ class Auth { const checkSSO = await this.checkSession() .then(result => { console.log('handleAuth.checksessions: ', result) - setItem('auth.user', result.idTokenPayload.sub) + setItem('authUser', result.idTokenPayload.sub) // user confirmed, log into client this.setSession(result) return result.idTokenPayload diff --git a/client/src/components/LoggedInUser.js b/client/src/components/LoggedInUser.js index aaca368cc..7fff914ee 100644 --- a/client/src/components/LoggedInUser.js +++ b/client/src/components/LoggedInUser.js @@ -43,12 +43,10 @@ class LoggedInUser extends Component { } } - componentDidMount() { - - } - render() { + localStorage.setItem('loggedInUser', JSON.stringify(this.props.data.userByAuthId)); + return (
{this.displayLoggedInUser()} diff --git a/client/src/components/NavBar.js b/client/src/components/NavBar.js index ce36144af..6e79ec4e6 100644 --- a/client/src/components/NavBar.js +++ b/client/src/components/NavBar.js @@ -50,7 +50,7 @@ class NavBar extends Component { componentDidMount() { - const userStored = localStorage.getItem('auth.user'); + const userStored = localStorage.getItem('authUser'); if (userStored !== null && userStored !== undefined && userStored !== 'undefined') { const user = JSON.parse(userStored); diff --git a/client/src/components/UserUpdate.js b/client/src/components/UserUpdate.js index 7e1e8a7c9..dcc43b4f1 100644 --- a/client/src/components/UserUpdate.js +++ b/client/src/components/UserUpdate.js @@ -6,19 +6,25 @@ import {getRolesQuery, updateUserMutation, getUsersQuery} from '../queries/queri class UserUpdate extends Component { constructor(props){ - super(props); - this.state = { - id: '5b90fc33ae71ee3798b2317d', // TODO this Id is the one that should be available to all components after the validation that the user is authenticated. - firstName: '', - middleName: '', - lastName: '', - email: '', - emailVerified: 0, - isDeveloper: 0, - isTrader: 0, - isDataAnalyst: 0, - roleId: '1' - }; + + super(props); + + this.defaultValuesSet = false; + + this.state = { + id: '5b90fc33ae71ee3798b2317d', // TODO this Id is the one that should be available to all components after the validation that the user is authenticated. + firstName: '', + middleName: '', + lastName: '', + email: '', + emailVerified: 0, + isDeveloper: 0, + isTrader: 0, + isDataAnalyst: 0, + roleId: '1' + }; + + console.log("constructor"); } displayRoles(){ @@ -33,7 +39,7 @@ class UserUpdate extends Component { } submitForm(e){ - console.log(this.state); + console.log("submitting this>", this.state); e.preventDefault(); this.props.updateUserMutation({ variables: { @@ -114,15 +120,44 @@ class UserUpdate extends Component { this.setState({ roleId:e.target.value }) } + componentDidMount(){ +console.log("component did mount"); + this.setState({}); + } + + componentWillUnmount(){ +console.log("component will UNmount"); + } + + componentDidUpdate () + { + console.log("component did update"); + if (this.defaultValuesSet === false) + { + let user = JSON.parse(localStorage.getItem("loggedInUser")); + this.defaultValuesSet= true; + this.refs.firstName.value = user.alias; + console.log("setting defaults"); + } + } + render(){ - console.log("user at render", this.props); +console.log("render"); + return(
+

Complete your profile with the following optional information. Providing your real name might help other users trust you more.

+
- this.setState({ firstName:e.target.value})}/> + this.setState({ firstName:e.target.value})}> +