From 4f94a412a86e1cf57f9dc9ba1bc4a3fe9d330215 Mon Sep 17 00:00:00 2001 From: michacurri Date: Sun, 3 Jan 2021 08:36:43 -0500 Subject: [PATCH 1/2] on master - oops --- api/controllers/profileController.js | 1 - server.js | 4 +--- src/components/admin/WorkorderCreate.js | 2 +- src/components/root/WorkorderDisplay.js | 7 ++++++- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/api/controllers/profileController.js b/api/controllers/profileController.js index 8ead7e2..8869808 100644 --- a/api/controllers/profileController.js +++ b/api/controllers/profileController.js @@ -29,7 +29,6 @@ module.exports = { const profileRes = await Profile.findOne({ email }) .populate("workorders") .exec(); - // console.log(`"profileRes.workorders": ${profileRes.workorders}`); return profileRes; } catch (err) { throw err; diff --git a/server.js b/server.js index 35ff2bd..1748f3b 100644 --- a/server.js +++ b/server.js @@ -7,16 +7,14 @@ const cookieParser = require("cookie-parser"); const mongoose = require("mongoose"); const DB_URI = process.env.DB_URI || "mongodb://localhost:27017/aero"; -// const DB_URI = "mongodb://localhost:27017/aero"; const PORT = process.env.PORT || 5000; -// const PORT = 5000; app.use(cookieParser()); app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.use( cors({ - // origin: `https://aero-workorder-management.herokuapp.com`, + origin: "http://localhost:3000/" || "https://aero-workorder-management.herokuapp.com", credentials: true, exposedHeaders: ["Set-Cookie"], allowedHeaders: ["Content-Type", "Authorization"], diff --git a/src/components/admin/WorkorderCreate.js b/src/components/admin/WorkorderCreate.js index 266aef6..b9166f8 100644 --- a/src/components/admin/WorkorderCreate.js +++ b/src/components/admin/WorkorderCreate.js @@ -21,7 +21,7 @@ const WorkorderAdd = ({ currentProfile }) => { body: JSON.stringify({ brand, model, colour }), }); if (response.ok) { - // props.onAdd(); + // onNewWorkorder(response); console.log(response); } else { console.log("Error saving record"); diff --git a/src/components/root/WorkorderDisplay.js b/src/components/root/WorkorderDisplay.js index 35c18e1..064c070 100644 --- a/src/components/root/WorkorderDisplay.js +++ b/src/components/root/WorkorderDisplay.js @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useEffect } from "react"; import { makeStyles } from "@material-ui/core/styles"; import { Table, @@ -19,6 +19,11 @@ const useStyles = makeStyles({ export default function WorkorderDisplay({ currentProfile }) { const classes = useStyles(); const { workorders } = currentProfile; + + useEffect(() => { + console.log('refresh'); + }, [workorders]) + return ( From 087fd98d29d043c8be2a8a09ffa1e2f845d5c45f Mon Sep 17 00:00:00 2001 From: michacurri Date: Tue, 5 Jan 2021 14:54:04 -0500 Subject: [PATCH 2/2] large structural changes to put useContext in MainContentSection and update workorders on state change --- api/middleware/verifyToken.js | 1 + api/routes/profileRoutes.js | 2 +- api/routes/workorderRoutes.js | 2 +- src/backend/authorization/AuthContainer.js | 110 ++++++++++++++----- src/backend/authorization/AuthLoginSignup.js | 84 -------------- src/components/admin/WorkorderCreate.js | 5 +- src/components/root/MainContentSection.js | 41 ++++++- src/components/root/Profile.js | 17 +-- src/components/root/ProfileCreate.js | 4 +- src/components/root/Workorder.js | 15 ++- src/components/root/WorkorderDisplay.js | 1 + 11 files changed, 141 insertions(+), 141 deletions(-) delete mode 100644 src/backend/authorization/AuthLoginSignup.js diff --git a/api/middleware/verifyToken.js b/api/middleware/verifyToken.js index 2a6f541..400779e 100644 --- a/api/middleware/verifyToken.js +++ b/api/middleware/verifyToken.js @@ -1,6 +1,7 @@ const { verifyToken } = require("../tokens/tokenService"); exports.verifyToken = async (req, res, next) => { + console.log(req.cookies); const { cookies } = req; try { if (!cookies || !cookies.token) { diff --git a/api/routes/profileRoutes.js b/api/routes/profileRoutes.js index f126680..b32cb07 100644 --- a/api/routes/profileRoutes.js +++ b/api/routes/profileRoutes.js @@ -93,7 +93,7 @@ router .use(verifyToken) .route("/this-profile") .get(async (req, res) => { - console.log(`/this-profile: ${req.profile.id}`); + // console.log(`/this-profile: ${req.profile.id}`); try { const profile = await findProfileById(req.profile.id); res.json({ data: profile }); diff --git a/api/routes/workorderRoutes.js b/api/routes/workorderRoutes.js index 5fe65a3..b3b6613 100644 --- a/api/routes/workorderRoutes.js +++ b/api/routes/workorderRoutes.js @@ -26,7 +26,7 @@ router.get("/", async (req, res) => { // router.post("/create/:userId", async (req, res) => { router - // .use(verifyToken) + .use(verifyToken) .route("/create/:userId") .post(async (req, res) => { const { brand, model, colour } = req.body; diff --git a/src/backend/authorization/AuthContainer.js b/src/backend/authorization/AuthContainer.js index 3e0ff8d..45817ae 100644 --- a/src/backend/authorization/AuthContainer.js +++ b/src/backend/authorization/AuthContainer.js @@ -1,11 +1,23 @@ -import React, { useContext, useCallback } from "react"; -import { UserContext } from "./UserContext"; -import { ImpersonatorContext } from "./ImpersonatorContext"; -import AuthLoginSignup from "./AuthLoginSignup"; +import React, { Fragment, useContext, useCallback, useState } from "react"; +import { Link, Switch, Route, Redirect } from "react-router-dom"; +import ProfileCreate from "../../components/root/ProfileCreate"; +import Field from "../../components/root/Field"; +// import { UserContext } from "./UserContext"; +// import { ImpersonatorContext } from "./ImpersonatorContext"; +// import AuthLoginSignup from "./AuthLoginSignup"; -const AuthContainer = () => { - const [currentUser, setCurrentUser] = useContext(UserContext); - const [impersonator, setImpersonator] = useContext(ImpersonatorContext); +const headers = { + Accept: "application/json", + "Content-Type": "application/json", +}; + +const AuthContainer = ({loadUserProfile}) => { + // const [currentUser, setCurrentUser] = useContext(UserContext); + const [email, setEmail] = useState(""); + const [password, setPassword] = useState(""); + const [error, setError] = useState(""); + const [link, setLink] = useState("signup"); + // const [impersonator, setImpersonator] = useContext(ImpersonatorContext); //* const loadImpersonator = useCallback(() => { // setTimeout(() => { @@ -13,29 +25,71 @@ const AuthContainer = () => { // }, [3000]); // }, [setImpersonator]); - const loadUserProfile = useCallback( - async function () { - try { - const response = await fetch("/api/profile/this-profile", { - headers: { - credentials: "include", - }, - }); - const json = await response.json(); - if (!response.ok) { - throw new Error(json.message); - } - setCurrentUser(json.data); - } catch (err) { - console.log(err); - setCurrentUser(undefined); + const changeLink = () => { + if (link === "signup") { + setLink("login"); + } else { + setLink("signup"); + } + }; + + + const handleSubmit = async (e) => { + e.preventDefault(); + try { + const response = await fetch("/api/profile/login", { + method: "POST", + headers: headers, + body: JSON.stringify({ email, password }), + }); + const data = await response.json(); + if (!response.ok) { + throw new Error(data.message); } - }, - [setCurrentUser] - ); + loadUserProfile(); + } catch (err) { + setError(err.message); + } + }; + return ( - + + + + +
+
+ { + setEmail(e.target.value); + }} + /> + { + setPassword(e.target.value); + }} + /> + + +
+
+ + + +
+ {/* prettier-ignore */} + +
); }; diff --git a/src/backend/authorization/AuthLoginSignup.js b/src/backend/authorization/AuthLoginSignup.js deleted file mode 100644 index 5e648f8..0000000 --- a/src/backend/authorization/AuthLoginSignup.js +++ /dev/null @@ -1,84 +0,0 @@ -import React, { Fragment, useState } from "react"; -import { Link, Switch, Route, Redirect } from "react-router-dom"; -import ProfileCreate from "../../components/root/ProfileCreate"; -import Field from "../../components/root/Field"; - -const headers = { - Accept: "application/json", - "Content-Type": "application/json", -}; - -function AuthLoginSignup({ loadUserProfile }) { - const [email, setEmail] = useState(""); - const [password, setPassword] = useState(""); - const [error, setError] = useState(""); - const [link, setLink] = useState("signup"); - - const changeLink = () => { - if (link === "signup") { - setLink("login"); - } else { - setLink("signup"); - } - }; - - const handleSubmit = async (e) => { - e.preventDefault(); - try { - const response = await fetch("/api/profile/login", { - method: "POST", - headers: headers, - body: JSON.stringify({ email, password }), - }); - const data = await response.json(); - if (!response.ok) { - throw new Error(data.message); - } - loadUserProfile(); - } catch (err) { - setError(err.message); - } - }; - - return ( - - - - -
-
- { - setEmail(e.target.value); - }} - /> - { - setPassword(e.target.value); - }} - /> - - -
-
- - - -
- {/* prettier-ignore */} - -
- ); -} - -export default AuthLoginSignup; diff --git a/src/components/admin/WorkorderCreate.js b/src/components/admin/WorkorderCreate.js index b9166f8..2ca3a79 100644 --- a/src/components/admin/WorkorderCreate.js +++ b/src/components/admin/WorkorderCreate.js @@ -1,7 +1,7 @@ import React, { Fragment, useState } from "react"; import WorkorderEditor from "../root/WorkorderEditor"; -const WorkorderAdd = ({ currentProfile }) => { +const WorkorderAdd = ({ currentProfile, loadUserProfile }) => { const [workorder, setWorkorder] = useState(); const updateWorkorderField = (e) => { @@ -21,8 +21,7 @@ const WorkorderAdd = ({ currentProfile }) => { body: JSON.stringify({ brand, model, colour }), }); if (response.ok) { - // onNewWorkorder(response); - console.log(response); + loadUserProfile() } else { console.log("Error saving record"); } diff --git a/src/components/root/MainContentSection.js b/src/components/root/MainContentSection.js index 44a403f..6bc8065 100644 --- a/src/components/root/MainContentSection.js +++ b/src/components/root/MainContentSection.js @@ -1,5 +1,6 @@ -import React, { useContext, Fragment } from "react"; +import React, { useContext, Fragment, useCallback } from "react"; import { UserContext } from "../../backend/authorization/UserContext"; +import { ImpersonatorContext } from "../../backend/authorization/ImpersonatorContext"; import { Redirect, Route, Switch } from "react-router-dom"; import Home from "./Home"; import SidebarNav from "./SidebarNav"; @@ -10,7 +11,37 @@ import AdvertHero from "./AdvertHero"; import AuthContainer from "../../backend/authorization/AuthContainer"; function MainContentSection({ loginClick }) { - const [currentProfile] = useContext(UserContext); + const [admin, setAdmin] = useContext(ImpersonatorContext); + const [currentProfile, setCurrentProfile] = useContext(UserContext); + + + + const loadUserProfile = useCallback( + async function () { + try { + const response = await fetch("/api/profile/this-profile", { + headers: { + credentials: "include", + }, + }); + const json = await response.json(); + if (!response.ok) { + throw new Error(json.message); + } + setCurrentProfile(json.data); + } catch (err) { + console.log(err); + setCurrentProfile(undefined); + } + }, + [setCurrentProfile] + ); + + // function loadUserProfile() { + + // const prevWorkorders = currentProfile.workorders; + // console.log(`prev: ${prevWorkorders} & new: ${workorders}`); + // } let content; if (loginClick) { @@ -26,8 +57,8 @@ function MainContentSection({ loginClick }) { {/* prettier-ignore */} } /> - } /> - } /> + } /> + } /> } /> @@ -35,7 +66,7 @@ function MainContentSection({ loginClick }) { ); } else { - content = ; + content = ; } } else { content = ( diff --git a/src/components/root/Profile.js b/src/components/root/Profile.js index 77f998e..fea6a5b 100644 --- a/src/components/root/Profile.js +++ b/src/components/root/Profile.js @@ -1,18 +1,14 @@ -import React, { useContext, useState, Fragment } from "react"; +import React, { useState, Fragment } from "react"; import ProfileDisplay from "./ProfileDisplay"; -import ProfileCreate from './ProfileCreate'; -import ProfileSearch from './ProfilesSearch' -import { ImpersonatorContext } from "../../backend/authorization/ImpersonatorContext"; -import { UserContext } from "../../backend/authorization/UserContext"; +import ProfileCreate from "./ProfileCreate"; +import ProfileSearch from "./ProfilesSearch"; // _______ Profile // Return two buttons that control whether to Sign In or Sign Up or add // On Login check if currentProfile exists in DB // TODO - IF user is ADMIN show everything -const Profile = () => { - const [admin] = useContext(ImpersonatorContext); - const [currentProfile, setCurrentProfile] = useContext(UserContext); +const Profile = ({ admin, currentProfile, setCurrentProfile }) => { const [searchOrAdd, setSearchOrAdd] = useState(null); const searchProfiles = () => { @@ -38,10 +34,7 @@ const Profile = () => { ) : (
- +
)} diff --git a/src/components/root/ProfileCreate.js b/src/components/root/ProfileCreate.js index d77d95d..88cdbfa 100644 --- a/src/components/root/ProfileCreate.js +++ b/src/components/root/ProfileCreate.js @@ -2,6 +2,8 @@ import React, { useState } from "react"; import ContactEditor from "./ContactEditor"; import { useHistory } from "react-router-dom"; +const headers = { "Content-Type": "application/json" }; + function ProfileCreate({ changeLink }) { const [contact, setContact] = useState(); const history = useHistory(); @@ -22,7 +24,7 @@ function ProfileCreate({ changeLink }) { try { const response = await fetch("/api/profile/create", { method: "POST", - headers: { "Content-Type": "application/json" }, + headers: headers, body: JSON.stringify(contact), }); const data = await response.json(); diff --git a/src/components/root/Workorder.js b/src/components/root/Workorder.js index 2fa9462..3ae3eb7 100644 --- a/src/components/root/Workorder.js +++ b/src/components/root/Workorder.js @@ -1,12 +1,12 @@ import React, { useContext, Fragment, useState } from "react"; -import { UserContext } from "../../backend/authorization/UserContext"; -import { ImpersonatorContext } from "../../backend/authorization/ImpersonatorContext"; +// import { UserContext } from "../../backend/authorization/UserContext"; +// import { ImpersonatorContext } from "../../backend/authorization/ImpersonatorContext"; import WorkorderDisplay from "./WorkorderDisplay"; import WorkorderCreate from "../admin/WorkorderCreate"; -const Workorder = () => { - const [currentProfile] = useContext(UserContext); - const [admin] = useContext(ImpersonatorContext); +const Workorder = ({ currentProfile, loadUserProfile, admin }) => { + // const [currentProfile] = useContext(UserContext); + // const [admin] = useContext(ImpersonatorContext); const [workView, setWorkView] = useState("display"); function workorderClick() { @@ -29,7 +29,10 @@ const Workorder = () => { ) : ( - + )} diff --git a/src/components/root/WorkorderDisplay.js b/src/components/root/WorkorderDisplay.js index 064c070..3e2213f 100644 --- a/src/components/root/WorkorderDisplay.js +++ b/src/components/root/WorkorderDisplay.js @@ -17,6 +17,7 @@ const useStyles = makeStyles({ }); export default function WorkorderDisplay({ currentProfile }) { + console.log(currentProfile); const classes = useStyles(); const { workorders } = currentProfile;