();
+
+ useEffect(() => {
+ setContents(
+ state.cart.contents.map((product: Product) => )
+ )
+ }, [state, setContents]);
+
+ useEffect(() => {
+ console.log(contents);
+ }, [contents]);
+
return (
- <>
- >
+
+ {
+ state.user.firstName ?
+
+ Hello, {state.user.firstName}!
+ :
+ Your cart is empty! Please log in to build your own.
+ }
+
+
+
)
}
diff --git a/client/src/components/Cart/CartItem.tsx b/client/src/components/Cart/CartItem.tsx
index f3bbfd3..9074b01 100644
--- a/client/src/components/Cart/CartItem.tsx
+++ b/client/src/components/Cart/CartItem.tsx
@@ -1,7 +1,9 @@
-function CartItem() {
+function CartItem({ product }: any) {
return (
- <>
- >
+
+
{product.name}
+
{product.price}
+
)
}
diff --git a/client/src/components/Navbar.tsx b/client/src/components/Navbar.tsx
index f2a5128..4ef6b33 100644
--- a/client/src/components/Navbar.tsx
+++ b/client/src/components/Navbar.tsx
@@ -42,7 +42,13 @@ function NavBar() {
- {loggedIn ? : }
+ {loggedIn ?
+ <>
+
+
+ >
+ :
+ }
)
}
diff --git a/client/src/components/Products/ProductCard.tsx b/client/src/components/Products/ProductCard.tsx
index cc5df56..45388f9 100644
--- a/client/src/components/Products/ProductCard.tsx
+++ b/client/src/components/Products/ProductCard.tsx
@@ -1,9 +1,11 @@
+import { useContext } from "react";
import { useNavigate } from "react-router-dom";
-import Route from 'react-router-dom';
-import ProductPage from "./ProductPage";
+import { addToCart } from "../../util/helpers";
+import { AppContext } from "../../store/store";
export default function ProductCard({ productData }: any) {
const { name, category, description, price, id } = productData;
+ const [state, dispatch] = useContext(AppContext);
const navigate = useNavigate();
return (
@@ -15,7 +17,14 @@ export default function ProductCard({ productData }: any) {
Price: {`$${price}` || "Free, apparently!"}
-
+
+ {
+ state.user.headers && state.user.headers.authenticated ?
+
+ :
+
+ }
+
)
diff --git a/client/src/components/Products/ProductPage.tsx b/client/src/components/Products/ProductPage.tsx
index 2ff8633..f222d41 100644
--- a/client/src/components/Products/ProductPage.tsx
+++ b/client/src/components/Products/ProductPage.tsx
@@ -20,7 +20,7 @@ export default function ProductPage() {
(a photo here)
{info.description}
- Price: {info.price}
+ Price: ${info.price}
: <>>
)
diff --git a/client/src/components/User/Register.tsx b/client/src/components/User/Register.tsx
index 1dd89c8..d8f0348 100644
--- a/client/src/components/User/Register.tsx
+++ b/client/src/components/User/Register.tsx
@@ -1,4 +1,5 @@
import { useEffect, useReducer, useState } from "react";
+import { useNavigate } from "react-router-dom";
import { initialState, reducer } from "../../store/store";
import { ActionType, emptySessionHeader } from "../../store/store_types";
import { userInfo } from '../../types/main';
@@ -16,6 +17,7 @@ function Register() {
headers: emptySessionHeader
}
+ const navigate = useNavigate();
const [state, dispatch] = useReducer(reducer, initialState);
const [userInput, setUserInput] = useState(formInitialState);
const [warningText, setWarningText] = useState('initial');
@@ -58,6 +60,7 @@ function Register() {
if (register.ok) {
setUserInput(formInitialState);
+ navigate('/');
} else {
console.log('Something went wrong');
}
diff --git a/client/src/store/store.ts b/client/src/store/store.ts
index c88c42e..eea3d94 100644
--- a/client/src/store/store.ts
+++ b/client/src/store/store.ts
@@ -28,6 +28,18 @@ export const reducer = (state: appState, action: userAction) => {
...state,
user: payload
}
+ case ActionType.ADDTOCART:
+ let updatedContents = state.cart.contents;
+ console.log(action.payload);
+ updatedContents.push(action.payload);
+
+ return {
+ ...state,
+ cart: {
+ ...state.cart,
+ contents: updatedContents
+ }
+ }
default:
return state;
}
diff --git a/client/src/store/store_types.ts b/client/src/store/store_types.ts
index 90db9b6..c1899dd 100644
--- a/client/src/store/store_types.ts
+++ b/client/src/store/store_types.ts
@@ -8,7 +8,8 @@ export enum ActionType {
REGISTERNEW,
UPDATEONE,
SEARCH,
- USERLOGIN
+ USERLOGIN,
+ ADDTOCART
}
export interface userAction {
diff --git a/client/src/types/main.d.ts b/client/src/types/main.d.ts
index 0648493..30ce9dc 100644
--- a/client/src/types/main.d.ts
+++ b/client/src/types/main.d.ts
@@ -7,7 +7,7 @@ export type userInfo = {
email: string
id?: number
- // NOTE: userInfo.name is deprecated
+ // NOTE: userInfo.name => displayName?
name?: string
password: string
verifyPassword?: string
diff --git a/client/src/util/helpers.ts b/client/src/util/helpers.ts
new file mode 100644
index 0000000..3481f3a
--- /dev/null
+++ b/client/src/util/helpers.ts
@@ -0,0 +1,7 @@
+import React from "react";
+import { ActionType, userAction } from "../store/store_types";
+import { Product } from "../types/main";
+
+export const addToCart = (productData: Product, dispatch: React.Dispatch): any => {
+ dispatch({ type: ActionType.ADDTOCART, payload: productData });
+}
\ No newline at end of file
diff --git a/swagger.yml b/swagger.yml
new file mode 100644
index 0000000..e69de29