fixing bugs where rendering overwrites local state. user profiles
This commit is contained in:
@@ -7,6 +7,7 @@ import LandingPage from './components/LandingPage';
|
|||||||
import Products from './components/Products/Products';
|
import Products from './components/Products/Products';
|
||||||
import LoginForm from './components/User/LoginForm';
|
import LoginForm from './components/User/LoginForm';
|
||||||
import Register from './components/User/Register';
|
import Register from './components/User/Register';
|
||||||
|
import UserProfile from './components/User/UserProfile';
|
||||||
|
|
||||||
import './App.scss'
|
import './App.scss'
|
||||||
|
|
||||||
@@ -21,6 +22,7 @@ function App() {
|
|||||||
<Routes>
|
<Routes>
|
||||||
<Route path="/" element={<LandingPage/>} />
|
<Route path="/" element={<LandingPage/>} />
|
||||||
<Route path="/login" element={<LoginForm/>} />
|
<Route path="/login" element={<LoginForm/>} />
|
||||||
|
<Route path="/users/:userID" element={<UserProfile profile={state.user.id} />} />
|
||||||
<Route path="/register" element={<Register/>} />
|
<Route path="/register" element={<Register/>} />
|
||||||
<Route path="/products/" element={<Products />} />
|
<Route path="/products/" element={<Products />} />
|
||||||
</Routes>
|
</Routes>
|
||||||
|
|||||||
@@ -5,11 +5,10 @@ import { useNavigate } from 'react-router-dom';
|
|||||||
|
|
||||||
function NavBar() {
|
function NavBar() {
|
||||||
const [loggedIn, setLoggedIn] = useState(false);
|
const [loggedIn, setLoggedIn] = useState(false);
|
||||||
const [userID, setUserID] = useState(null);
|
const [profText, setProfText] = useState(null);
|
||||||
const [searchInput, setSearchInput] = useState('');
|
const [searchInput, setSearchInput] = useState('');
|
||||||
|
|
||||||
const [state, dispatch] = useContext(AppContext);
|
const [state, dispatch] = useContext(AppContext);
|
||||||
// const { searchTerm, user, cart } = useContext(AppContext);
|
|
||||||
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
@@ -20,24 +19,30 @@ function NavBar() {
|
|||||||
navigate(`/products?query=${searchInput}`);
|
navigate(`/products?query=${searchInput}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// const forceRender = () => {
|
useEffect(() => {
|
||||||
// console.log(searchTerm);
|
if (state === initialState) return;
|
||||||
// console.log(user);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// useEffect(() => {
|
console.log(state.user);
|
||||||
// console.log('state updated!');
|
|
||||||
// }, [user])
|
if (state.user && state.user.headers.authenticated) {
|
||||||
|
console.log('authenticated!');
|
||||||
|
setProfText(state.user.email);
|
||||||
|
setLoggedIn(true);
|
||||||
|
} else if (!state.user.authenticated) {
|
||||||
|
setLoggedIn(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
}, [state]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<nav>
|
<nav>
|
||||||
<a href="/">Logo</a>
|
<button onClick={() => navigate("/")}>Logo</button>
|
||||||
<div className="searchbar">
|
<div className="searchbar">
|
||||||
<input type="text" placeholder="Search products" onChange={(e) => setSearchInput(e.target.value)}/>
|
<input type="text" placeholder="Search products" onChange={(e) => setSearchInput(e.target.value)}/>
|
||||||
<button onClick={handleSearch}>Search</button>
|
<button onClick={handleSearch}>Search</button>
|
||||||
<button onClick={() => console.log(state)}>Render</button>
|
<button onClick={() => console.log(state)}>Render</button>
|
||||||
</div>
|
</div>
|
||||||
{loggedIn ? <a href={`/users/${userID}`}>Profile info</a> : <a href="/login">Log In</a>}
|
{loggedIn ? <button onClick={() => navigate(`/users/${state.user.id}`)}>{profText}</button> : <button onClick={() => navigate("/login")}>Log In</button>}
|
||||||
</nav>
|
</nav>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,13 +25,12 @@ function LoginForm() {
|
|||||||
const json = await response?.json();
|
const json = await response?.json();
|
||||||
|
|
||||||
if (json) {
|
if (json) {
|
||||||
console.log(json);
|
const { session, userProfile } = json;
|
||||||
console.log(json.user);
|
|
||||||
|
|
||||||
let thisUser: userInfo = {
|
let thisUser: userInfo = {
|
||||||
email: json.user.email,
|
id: userProfile.id,
|
||||||
password: json.user.password,
|
email: userProfile.email,
|
||||||
headers: json
|
password: userProfile.password,
|
||||||
|
headers: session
|
||||||
}
|
}
|
||||||
|
|
||||||
setToDispatch(thisUser);
|
setToDispatch(thisUser);
|
||||||
@@ -39,7 +38,7 @@ function LoginForm() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log('thing?');
|
if (!toDispatch) return;
|
||||||
dispatch({ type: ActionType.USERLOGIN, payload: toDispatch });
|
dispatch({ type: ActionType.USERLOGIN, payload: toDispatch });
|
||||||
}, [toDispatch]);
|
}, [toDispatch]);
|
||||||
|
|
||||||
|
|||||||
33
client/src/components/User/UserProfile.tsx
Normal file
33
client/src/components/User/UserProfile.tsx
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { useContext, useEffect } from "react"
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import { AppContext } from "../../store/store"
|
||||||
|
|
||||||
|
import Page from "../../util/Page";
|
||||||
|
|
||||||
|
export default function UserProfile(profile: any): JSX.Element {
|
||||||
|
const [state, dispatch] = useContext(AppContext);
|
||||||
|
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
// useEffect(() => {
|
||||||
|
// if (!state.user.headers.authenticated) {
|
||||||
|
// console.log('bad');
|
||||||
|
// }
|
||||||
|
// }, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Page>
|
||||||
|
<h1>User Profile</h1>
|
||||||
|
<h2>Thanks for supporting us{`, ${state.user.name}!` || '!'}</h2>
|
||||||
|
<h2>{state.user.id || 'Profile not found'}</h2>
|
||||||
|
<h3>{state.user.email}</h3>
|
||||||
|
|
||||||
|
<div className="profile-options">
|
||||||
|
<button>Order History</button>
|
||||||
|
<button>Open Orders</button>
|
||||||
|
<button>Edit Profile</button>
|
||||||
|
<button>Profile Settings</button>
|
||||||
|
</div>
|
||||||
|
</Page>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -35,5 +35,5 @@ export const handleLogin = async (email: string, password: string) => {
|
|||||||
body: JSON.stringify({ email: email, password: password })
|
body: JSON.stringify({ email: email, password: password })
|
||||||
});
|
});
|
||||||
|
|
||||||
if (serverCall.ok) return serverCall;
|
return serverCall;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,12 @@ loginRouter.route('/').post(async (req, res) => {
|
|||||||
req.session.authenticated = true;
|
req.session.authenticated = true;
|
||||||
req.session.user = { email: email, password: password }
|
req.session.user = { email: email, password: password }
|
||||||
|
|
||||||
res.send(req.session);
|
let fullUserProfile = await newClient.query("SELECT * FROM users WHERE email = ($1)", [email]);
|
||||||
|
|
||||||
|
res.send({
|
||||||
|
session: req.session,
|
||||||
|
userProfile: fullUserProfile.rows[0]
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
|
|||||||
Reference in New Issue
Block a user