aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Form/SignInForm.tsx21
-rw-r--r--src/index.tsx12
2 files changed, 18 insertions, 15 deletions
diff --git a/src/Form/SignInForm.tsx b/src/Form/SignInForm.tsx
index 40352fe..efe85ed 100644
--- a/src/Form/SignInForm.tsx
+++ b/src/Form/SignInForm.tsx
@@ -26,24 +26,17 @@ const SignInForm: React.FC<PropTypes> = ({ setUser }) => {
const classes = useStyles();
const inputRef = useRef<HTMLInputElement>();
- const getUserProfile = name => {
- get(`/users?name=${name}`).then(response => {
- setUser(response.data[0]);
- });
- };
-
const onClick = () => {
- const value = inputRef.current?.value;
- if (value) {
- localStorage.setItem('user', value);
- getUserProfile(value);
+ const username = inputRef.current?.value;
+ if (username) {
+ get(`/users?name=${username}`).then(response => {
+ const user = response.data[0];
+ setUser(user);
+ localStorage.setItem('userId', user._id);
+ });
}
};
- if (localStorage.getItem('user') !== null) {
- getUserProfile(localStorage.getItem('user'));
- }
-
return (
<form className={classes.root} noValidate autoComplete="off">
<h1>Sign In</h1>
diff --git a/src/index.tsx b/src/index.tsx
index 1362544..0855038 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -1,4 +1,4 @@
-import React, { useState } from 'react';
+import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';
import {
createMuiTheme,
@@ -15,6 +15,7 @@ import ProfileInfo from './ProfileInfo/ProfileInfo';
import SignInForm from './Form/SignInForm';
import { User } from './types';
+import { get } from './requests';
const theme = createMuiTheme({
palette: {
@@ -37,6 +38,15 @@ const App: React.FC = () => {
const [user, setUser] = React.useState<User | undefined>();
const classes = useStyles();
+ useEffect(() => {
+ const userId = localStorage.getItem('userId');
+ if (userId) {
+ get(`/users/${userId}`).then(response => {
+ setUser(response.data);
+ });
+ }
+ }, []);
+
return (
<ThemeProvider theme={theme}>
<CssBaseline />