Skip to content

Commit

Permalink
Add user details
Browse files Browse the repository at this point in the history
  • Loading branch information
damienteo committed Jul 31, 2019
1 parent 0073c4b commit 33c272b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"lodash.merge": "^4.6.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "3.0.1"
Expand Down
11 changes: 11 additions & 0 deletions src/components/auth.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function authenticate(email, password) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({
name: "New name",
dateOfBirth: "01/01/1991",
email: "[email protected]"
});
}, 2500);
});
}
25 changes: 13 additions & 12 deletions src/components/login.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import React, { useState, useContext } from "react";
import LoadingContext from "./loadingContext";
import { authenticate } from "./auth";
import UserDetailsContext from "./userDetailsContext";

const Login = () => {
const { showLoading, hideLoading } = useContext(LoadingContext);
// const { showLoading, hideLoading } = useContext(LoadingContext);
const { setUserDetails } = useContext(UserDetailsContext);

const [email] = useState("[email protected]");
const [password] = useState("password");

const authenticateUser = () => {
showLoading();
// authenticateUser(email, password).then(userDetails => {
// const {
// name,
// dateOfBirth,
// email,
// secretQuestion,
// secretAnswer
// } = userDetails;
// hideLoading;
// });
// showLoading();
authenticateUser(email, password).then(userDetails => {
const { name, dateOfBirth, email } = userDetails;
// hideLoading;
});
};
return <button onClick={authenticateUser}>Login</button>;
};
Expand Down
30 changes: 30 additions & 0 deletions src/components/userDetailsProvider.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { useState, useContext } from "react";
import merge from "lodash.merge";

import UserDetailsContext from "./userDetailsContext";

const UserDetailsProvider = ({ children }) => {
const setUserDetails = ({ name, dateOfBirth, email }) => {
updateUserDetails(prevState => {
const newState = { ...prevState };
return merge(newState, {
name,
dateOfBirth,
email
});
});
};
const userState = {
name: "",
dateOfBirth: "",
email: ""
};
const [userDetails, updateUserDetails] = useState(userState);
return (
<UserDetailsContext.Provider value={userDetails}>
{children}
</UserDetailsContext.Provider>
);
};

export default UserDetailsProvider;

0 comments on commit 33c272b

Please sign in to comment.