From 12fb40fd9c3246d1300e6b4f8bbe75abab83563f Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 6 Sep 2018 12:10:24 +0200 Subject: [PATCH] Individual Form to INSERT and UPDATE a user profile, all working --- client/src/components/AddUser.js | 130 +-------------------- client/src/components/Profile.js | 2 + client/src/components/UpdateUser.js | 165 +++++++++++++++++++++++++++ client/src/components/UserList.js | 9 +- client/src/components/UserProfile.js | 4 +- client/src/queries/queries.js | 16 ++- server/models/user.js | 1 + server/schema/schema.js | 21 +--- 8 files changed, 201 insertions(+), 147 deletions(-) create mode 100644 client/src/components/UpdateUser.js diff --git a/client/src/components/AddUser.js b/client/src/components/AddUser.js index 1cb43e56f..0939fe3a8 100644 --- a/client/src/components/AddUser.js +++ b/client/src/components/AddUser.js @@ -1,5 +1,4 @@ import React, { Component } from 'react'; -import ReactDOM from 'react-dom'; import {graphql, compose} from 'react-apollo'; import {getRolesQuery, addUserMutation, getUsersQuery} from '../queries/queries'; @@ -8,24 +7,9 @@ class AddUser extends Component { super(props); this.state = { alias: '', - firstName: '', - lastName: '', - isDeveloper: 0, - isTrader: 0, - isDataAnalyst: 0, - roleId: '1' + authId: '1' }; } - displayRoles(){ - var data = this.props.getRolesQuery; // When there is more than one query binded to a single componente 'data' is replaced by thename of the query given below at the binding operation. - if(data.loading){ - return( ); - } else { - return data.roles.map(role => { - return( ); - }); - } - } submitForm(e){ console.log(this.state); @@ -33,84 +17,17 @@ class AddUser extends Component { this.props.addUserMutation({ variables: { alias: this.state.alias, - firstName: this.state.firstName, - lastName: this.state.lastName, - isDeveloper: this.state.isDeveloper, - isTrader: this.state.isTrader, - isDataAnalyst: this.state.isDataAnalyst, - roleId: this.state.roleId + authId: this.state.authId }, refetchQueries: [{ query: getUsersQuery}] // This allow us to re run whatever queries are necesary after the mutation. }); } - handleCheckBoxes(e){ - let fieldValue; - if (e.target.checked === true) { - fieldValue = 1; - } else { - fieldValue = 0; - - /* When the user unchecks one of the checkboxes, we need to make sure that that Role is not the one selected at the SELECT, - and if it is, then we need to automatically select another one */ - - let select = ReactDOM.findDOMNode(this.refs.select); - switch (e.target.id) { - case "isDeveloper": - if (select.value === "2") { - select.value = "1"; - this.setState({ roleId: "1"}); - } - break; - - case "isTrader": - if (select.value === "3") { - select.value = "1"; - this.setState({ roleId: "1"}); - } - break; - - case "isDataAnalyst": - if (select.value === "4") { - select.value = "1"; - this.setState({ roleId: "1"}); - } - break; - default: - } - } - this.setState({ [e.target.id]: fieldValue}) - } - - handleSelect(e){ - let checkbox; - switch (e.target.value) { - case "2": - checkbox = ReactDOM.findDOMNode(this.refs.id2); - checkbox.checked = true; - this.setState({ isDeveloper: 1}) - break; - case "3": - checkbox = ReactDOM.findDOMNode(this.refs.id3); - checkbox.checked = true; - this.setState({ isTrader: 1}) - break; - case "4": - checkbox = ReactDOM.findDOMNode(this.refs.id4); - checkbox.checked = true; - this.setState({ isDataAnalyst: 1}) - break; - default: - } - - this.setState({ roleId:e.target.value }) - } - render(){ return(
- +

Choose an Alias that will identify you anonimously across the whole Advanced Algos ecosystem. Choose it carefully, since the the Alias can not be changed later.

this.setState({ alias:e.target.value})}/> @@ -118,49 +35,10 @@ class AddUser extends Component {
-
- this.setState({ firstName:e.target.value})}/> - -
-
-
-
- this.setState({ lastName:e.target.value})}/> - -
-
-
-
- - -
-
-
-
- - -
-
-
-
- - -
-
-
- - -
- -
- +
- - ); } } diff --git a/client/src/components/Profile.js b/client/src/components/Profile.js index 9a93782ce..aa68d3c45 100644 --- a/client/src/components/Profile.js +++ b/client/src/components/Profile.js @@ -1,11 +1,13 @@ import React from 'react' import AddUser from './AddUser'; +import UpdateUser from './UpdateUser'; const Profile = () => { return (
+
) } diff --git a/client/src/components/UpdateUser.js b/client/src/components/UpdateUser.js new file mode 100644 index 000000000..d11c87404 --- /dev/null +++ b/client/src/components/UpdateUser.js @@ -0,0 +1,165 @@ +import React, { Component } from 'react'; +import ReactDOM from 'react-dom'; +import {graphql, compose} from 'react-apollo'; +import {getRolesQuery, updateUserMutation, getUsersQuery} from '../queries/queries'; + +class UpdateUser 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: '', + lastName: '', + isDeveloper: 0, + isTrader: 0, + isDataAnalyst: 0, + roleId: '1' + }; + } + displayRoles(){ + var data = this.props.getRolesQuery; // When there is more than one query binded to a single componente 'data' is replaced by thename of the query given below at the binding operation. + if(data.loading){ + return( ); + } else { + return data.roles.map(role => { + return( ); + }); + } + } + + submitForm(e){ + console.log(this.state); + e.preventDefault(); + this.props.updateUserMutation({ + variables: { + id: this.state.id, + firstName: this.state.firstName, + lastName: this.state.lastName, + isDeveloper: this.state.isDeveloper, + isTrader: this.state.isTrader, + isDataAnalyst: this.state.isDataAnalyst, + roleId: this.state.roleId + }, + refetchQueries: [{ query: getUsersQuery}] // This allow us to re run whatever queries are necesary after the mutation. + }); + } + + handleCheckBoxes(e){ + let fieldValue; + if (e.target.checked === true) { + fieldValue = 1; + } else { + fieldValue = 0; + + /* When the user unchecks one of the checkboxes, we need to make sure that that Role is not the one selected at the SELECT, + and if it is, then we need to automatically select another one */ + + let select = ReactDOM.findDOMNode(this.refs.select); + switch (e.target.id) { + case "isDeveloper": + if (select.value === "2") { + select.value = "1"; + this.setState({ roleId: "1"}); + } + break; + + case "isTrader": + if (select.value === "3") { + select.value = "1"; + this.setState({ roleId: "1"}); + } + break; + + case "isDataAnalyst": + if (select.value === "4") { + select.value = "1"; + this.setState({ roleId: "1"}); + } + break; + default: + } + } + this.setState({ [e.target.id]: fieldValue}) + } + + handleSelect(e){ + let checkbox; + switch (e.target.value) { + case "2": + checkbox = ReactDOM.findDOMNode(this.refs.id2); + checkbox.checked = true; + this.setState({ isDeveloper: 1}) + break; + case "3": + checkbox = ReactDOM.findDOMNode(this.refs.id3); + checkbox.checked = true; + this.setState({ isTrader: 1}) + break; + case "4": + checkbox = ReactDOM.findDOMNode(this.refs.id4); + checkbox.checked = true; + this.setState({ isDataAnalyst: 1}) + break; + default: + } + + this.setState({ roleId:e.target.value }) + } + + 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({ lastName:e.target.value})}/> + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+ + +
+ +
+ +
+
+
+ + + ); + } +} + +export default compose( + graphql(getRolesQuery, {name: "getRolesQuery"}), + graphql(updateUserMutation, {name: "updateUserMutation"}) +) (UpdateUser); // This technique binds more than one query to a single component. diff --git a/client/src/components/UserList.js b/client/src/components/UserList.js index a9c54bc72..5bba0e310 100644 --- a/client/src/components/UserList.js +++ b/client/src/components/UserList.js @@ -20,15 +20,20 @@ class UserList extends Component { } else { return data.users.map(user => { return ( -
  • {this.setState({ selected: user.id})}} >{user.alias}
  • +
  • { + this.setState({ selected: user.id}); + } + } >{user.alias}
  • ) }); } } render() { + return ( +
    -