Skip to content

Commit

Permalink
✨ delete user cart
Browse files Browse the repository at this point in the history
  • Loading branch information
neilitalia committed Oct 17, 2021
1 parent dcbef0f commit 46315d9
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 15 deletions.
22 changes: 14 additions & 8 deletions client/src/components/ListingCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,33 @@
<i class="bx bx-show"></i>
<span class="span">{{ listing.views }}</span>
</vs-button>
<vs-tooltip v-if="inCart">
<vs-tooltip v-if="inProfile">
<vs-button class="btn-chat" primary icon>
<i class="bx bx-edit-alt"></i>
</vs-button>
<template #tooltip> Edit Listing </template>
</vs-tooltip>
<vs-tooltip v-if="inProfile">
<vs-button
class="btn-chat"
warn
danger
icon
@click="removeFromCart({ cartId: cart.id, listingId: listing.id })"
@click="setListingIdToDelete(listing.id), toggleDeleteDialog()"
>
<i class="bx bx-trash"></i>
</vs-button>
<template #tooltip> Remove from cart </template>
<template #tooltip> Delete Listing </template>
</vs-tooltip>
<vs-tooltip v-if="inProfile">
<vs-tooltip v-if="inCart">
<vs-button
class="btn-chat"
danger
warn
icon
@click="setListingIdToDelete(listing.id), toggleDeleteDialog()"
@click="removeFromCart({ cartId: cart.id, listingId: listing.id })"
>
<i class="bx bx-trash"></i>
</vs-button>
<template #tooltip> Delete Listing </template>
<template #tooltip> Remove from cart </template>
</vs-tooltip>
<vs-button-group v-if="inCart">
<vs-button
Expand Down
12 changes: 8 additions & 4 deletions client/src/pages/Cart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</div>
<div v-if="!cart.cart_listing.length">
<h2>Your {{ cart.name }} cart is lonely :( No items yet</h2>
<vs-button border class="mr-20" @click="checkOut(cart.id)">
<vs-button border class="mr-20" @click="deleteCart(cart.id)">
<i class="bx bx-trash-alt"></i>&nbsp;Delete list
</vs-button>
</div>
Expand All @@ -42,7 +42,7 @@
<vs-button border class="mr-20" @click="checkOut(cart.id)">
<i class="bx bx-cart"></i>&nbsp;Checkout
</vs-button>
<vs-button border class="mr-20" @click="checkOut(cart.id)">
<vs-button border class="mr-20" @click="deleteCart(cart.id)">
<i class="bx bx-trash-alt"></i>&nbsp;Delete list
</vs-button>
<p>Subtotal: ${{ getSubTotal(cart.cart_listing) }}</p>
Expand Down Expand Up @@ -84,8 +84,12 @@ export default {
},
methods: {
...mapActions("navigation", ["setActivePage"]),
...mapActions("stripe", ["checkout"]),
...mapActions("cart", ["getUserCartItems", "toggleNewCartDialog"]),
...mapActions("stripe", ["checkOut"]),
...mapActions("cart", [
"getUserCartItems",
"toggleNewCartDialog",
"deleteCart",
]),
getSubTotal(items) {
const rawSubTotal = items.reduce((acc, item) => {
return acc + item.price * item.cart_info.quantity;
Expand Down
10 changes: 10 additions & 0 deletions client/src/services/CartServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ export const GetUserCartItems = async (userId) => {
return res
}

export const DeleteCart = async (cartId) => {
const res = await Client.delete(`/cart/delete/${cartId}`)
.then((res) =>{
return res
}).catch((err)=>{
return err.response
})
return res
}

export const AddItemToUserCart = async (data) => {
const res = await Client.post('/cartlisting/add', data)
.then((res) =>{
Expand Down
14 changes: 13 additions & 1 deletion client/src/store/modules/cart.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GetUserCartItems, AddItemToUserCart, GetUserCartsList, RemoveItemFromUserCart, IncrementCartItem, DecrementCartItem, CreateNewCartList } from "../../services/CartServices";
import { GetUserCartItems, AddItemToUserCart, GetUserCartsList, RemoveItemFromUserCart, IncrementCartItem, DecrementCartItem, CreateNewCartList, DeleteCart } from "../../services/CartServices";

const state = () => ({
userCarts: [],
Expand Down Expand Up @@ -110,6 +110,8 @@ const actions = {
const res = await IncrementCartItem(req)
if(res.status === 200){
commit('setCartStatus','Incremented')
} else {
commit('setCartStatus','Failed')
}
},
async decrementCartItem({commit},payload){
Expand All @@ -120,6 +122,16 @@ const actions = {
const res = await DecrementCartItem(req)
if(res.status === 200){
commit('setCartStatus','Decremented')
} else {
commit('setCartStatus','Failed')
}
},
async deleteCart({commit}, payload){
const res = await DeleteCart(payload)
if(res.status === 200){
commit('setCartStatus','Cart Deleted')
} else {
commit('setCartStatus','Failed')
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/CartController.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const DeleteCart = async (req, res) => {
const deletedCartListings = CartListing.destroy({ where: { cart_id: id} })
const cart = await Cart.findOne({where: { id: id }})
const deletedCart = await cart.destroy({returning: true})
return res.send({msg: "Deleted Cart", deleted: [deletedCart, deletedCartListings]})
return res.send({msg: "Deleted Cart", payload: { cart_id: id }, deleted: [deletedCart, deletedCartListings]})
} catch (error) {
return res.status(500).send(error.message)
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/ListingController.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const DeleteListing = async (req, res) => {
where: { id: id }
})
const deleted = await listing.destroy()
return res.send({ msg: "Deleted listing", deleted: [deletedCartsListing, deletedImage, deleted ]})
return res.send({ msg: "Deleted listing", payload: { listing_id: id }, deleted: [deletedCartsListing, deletedImage, deleted ]})
} catch (error) {
return res.status(500).send({ error: error })
}
Expand Down

0 comments on commit 46315d9

Please sign in to comment.