Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
ecommerce-maven
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ascend
ecommerce-maven
Commits
6994d175
Commit
6994d175
authored
May 12, 2021
by
Kyle Muldoon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Done with ticket for now
parent
540ad024
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
20528 additions
and
50 deletions
+20528
-50
package-lock.json
ecom-web/package-lock.json
+20463
-24
header-cart.jsx
ecom-web/src/components/Header/header-cart.jsx
+4
-5
CartItem.js
ecom-web/src/components/shopping-cart/CartItem.js
+11
-1
shopping-cart.js
ecom-web/src/components/shopping-cart/shopping-cart.js
+49
-19
user_reducer.js
ecom-web/src/reducers/user_reducer.js
+1
-1
No files found.
ecom-web/package-lock.json
View file @
6994d175
This source diff could not be displayed because it is too large. You can
view the blob
instead.
ecom-web/src/components/Header/header-cart.jsx
View file @
6994d175
import
React
,
{
Component
}
from
'react'
import
'./header.css'
;
import
{
NavLink
}
from
"react-router-dom"
import
Nav
from
"react-bootstrap/Nav"
import
{
LinkContainer
}
from
'react-router-bootstrap'
import
{
NavLink
}
from
'react-router-dom'
import
Nav
from
'react-bootstrap/Nav'
export
default
class
HeaderCart
extends
Component
{
constructor
(
props
)
{
super
(
props
)
...
...
@@ -15,7 +12,9 @@ export default class HeaderCart extends Component {
render
()
{
return
(
<
div
>
<
Nav
.
Link
href=
"/cart"
>
Cart
</
Nav
.
Link
>
<
LinkContainer
to=
"/cart"
>
<
Nav
.
Link
id=
"nav-Cart-link"
>
Cart
</
Nav
.
Link
>
</
LinkContainer
>
</
div
>
)
}
...
...
ecom-web/src/components/shopping-cart/CartItem.js
View file @
6994d175
...
...
@@ -20,7 +20,17 @@ export default function CartItem(props) {
<
div
className
=
"productStock"
>
{
props
.
productInfo
.
availableStock
}
left
in
stock
<
/div
>
<
div
className
=
"quantityControls"
>
<
select
className
=
"productQuantitySelect"
><
option
>
{
props
.
quantity
}
<
/option></
select
>
<
input
className
=
"productQuantitySelect"
type
=
"number"
value
=
{
props
.
quantity
}
min
=
"1"
max
=
"99"
onChange
=
{(
e
)
=>
{
props
.
handleQuantityUpdate
(
props
.
productInfo
.
sku
,
e
.
target
.
value
)}}
/
>
<
button
onClick
=
{()
=>
{
props
.
handleDelete
(
props
.
productInfo
.
sku
)
}}
>
Delete
<
/button
>
<
/div
>
...
...
ecom-web/src/components/shopping-cart/shopping-cart.js
View file @
6994d175
...
...
@@ -3,59 +3,88 @@ import CartItem from './CartItem.js'
import
'./shopping-cart.css'
import
{
useSelector
,
useDispatch
}
from
'react-redux'
import
{
Link
}
from
'react-router-dom'
import
axios
from
'axios'
import
{
updateUserCart
}
from
'./../../actions/cart_actions'
export
default
function
ShoppingCart
()
{
const
[
userSession
,
setUserSession
]
=
useState
(
useSelector
(
state
=>
state
.
user
.
currentUser
))
const
[
allProducts
,
setAllProducts
]
=
useState
(
useSelector
(
state
=>
state
.
market
.
products
))
const
dispatch
=
useDispatch
()
const
userSession
=
useSelector
(
state
=>
state
.
user
.
currentUser
)
const
allProducts
=
useSelector
(
state
=>
state
.
market
.
products
)
const
[
cartRefs
,
setCartRefs
]
=
useState
(
useSelector
(
state
=>
state
.
cart
))
const
[
cartItems
,
setCartItems
]
=
useState
([])
// This effect collects the products that are mapped to the cartRefs in the user's cart
// and assigns the list of producct-supplemented cart items to the cartItems state
////////////////////////////////////////////////////////////////
// Map Product Refs to Products that exist in redux global state
////////////////////////////////////////////////////////////////
useEffect
(()
=>
{
const
productsFromRefs
=
[]
cartRefs
.
map
((
cartRef
)
=>
{
productsFromRefs
.
push
(({
product
:
allProducts
.
filter
((
currProduct
)
=>
(
currProduct
.
sku
===
cartRef
.
productRef
.
sku
))[
0
],
quantity
:
cartRef
.
quantity
}))
productsFromRefs
.
push
((
{
product
:
allProducts
.
filter
((
currProduct
)
=>
(
currProduct
.
sku
===
cartRef
.
productRef
.
sku
))[
0
],
quantity
:
cartRef
.
quantity
}))
})
console
.
log
(
"NEW PRODUCTS FROM REFS YO"
)
console
.
log
(
productsFromRefs
)
setCartItems
(
productsFromRefs
)
},
[
cartRefs
])
// This function deletes an item from the user's cart
////////////////////////////////
// Delete Item from Cart
////////////////////////////////
let
handleDelete
=
(
skuToBeDeleted
)
=>
{
console
.
log
(
"SKU to be deleted: "
+
skuToBeDeleted
)
const
newCartRefs
=
cartRefs
.
filter
((
cartRef
)
=>
!
(
cartRef
.
productRef
.
sku
===
skuToBeDeleted
))
setCartRefs
(
newCartRefs
)
}
let
updateCart
=
async
()
=>
{
const
cartUpdateObj
=
{
userId
:
userSession
.
email
,
cartItems
:
cartRefs
}
////////////////////////////////
// Update Quantity of an item
////////////////////////////////
let
handleQuantityUpdate
=
(
skuToBeUpdated
,
newQuantity
)
=>
{
console
.
log
(
JSON
.
stringify
(
cartUpdateObj
))
const
newCartRefs
=
[]
const
response
=
await
axios
.
put
(
`http://localhost:8080/api/carts/
${
userSession
.
email
}
`
,
cartUpdateObj
)
return
response
cartRefs
.
map
(
(
cartRef
)
=>
{
if
(
cartRef
.
productRef
.
sku
===
skuToBeUpdated
)
{
let
temp
=
cartRef
temp
.
quantity
=
newQuantity
newCartRefs
.
push
(
temp
)
}
else
{
newCartRefs
.
push
(
cartRef
)
}
})
setCartRefs
(
newCartRefs
)
}
////////////////////////////////////////////////////////////////
// Sync Cart with backend and redux global state
////////////////////////////////////////////////////////////////
useEffect
(()
=>
{
console
.
log
(
updateCart
())
const
cartUpdateObj
=
{
userId
:
userSession
.
email
,
cartItems
:
cartRefs
}
dispatch
(
updateUserCart
(
cartUpdateObj
,
cartUpdateObj
.
userId
))
},
[
cartItems
])
return
(
<
div
id
=
"shoppingCartContainer"
>
...
...
@@ -68,6 +97,7 @@ export default function ShoppingCart() {
productInfo
=
{
currItem
.
product
}
quantity
=
{
currItem
.
quantity
}
handleDelete
=
{
handleDelete
}
handleQuantityUpdate
=
{
handleQuantityUpdate
}
key
=
{
currItem
.
product
.
sku
}
/
>
)
...
...
ecom-web/src/reducers/user_reducer.js
View file @
6994d175
...
...
@@ -12,7 +12,7 @@ const userReducer = (state = initialState, action) => {
newState
.
currentUser
=
action
.
user
.
data
;
return
newState
;
case
LOGOUT_USER
:
return
newState
.
currentUser
=
null
;
return
newState
.
currentUser
=
({
currentUser
:
null
})
;
default
:
return
state
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment