Skip to content

Commit 2a3232d

Browse files
committed
Used AJAX for the add to cart.
1 parent 55e86c4 commit 2a3232d

File tree

10 files changed

+161
-1495
lines changed

10 files changed

+161
-1495
lines changed

core/templates/core/index.html

+15-20
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,33 @@
1919
<h2 class="text-lg font-bold text-[#050a30] truncate">{{ item.name }}</h2>
2020
<div class="flex justify-between items-center mt-3">
2121
<!-- Add to Cart Button -->
22-
<button class="px-4 py-2 bg-[#cae8ff] text-[#050a30] font-semibold rounded-md hover:bg-[#b6e0ff] focus:outline-none">
23-
<a href="{% url 'add_to_cart' item.id %}">
24-
Add to Cart
25-
</a>
22+
<button class="add-to-cart-btn px-4 py-2 bg-[#cae8ff] text-[#050a30] font-semibold rounded-md hover:bg-[#b6e0ff] focus:outline-none" data-item-id="{{ item.id }}">
23+
Add to Cart
2624
</button>
2725

2826
<!-- Add Wishlist Button -->
2927
{% if request.user.is_authenticated %}
30-
{% if item.id not in user_wishlist %}
31-
<a href="{% url 'toggle_wishlist' item.id %}">
3228
<button
33-
class="px-2 py-2 bg-[#050a30] text-white rounded-full hover:bg-blue-600 focus:outline-none"
34-
title="Add to Wishlist">
29+
class="add-to-wishlist-btn px-2 py-2 bg-[#050a30] text-white rounded-full hover:bg-blue-600 focus:outline-none"
30+
title="{% if item.id in user_wishlist %}Remove from Wishlist{% else %}Add to Wishlist{% endif %}"
31+
data-item-id="{{ item.id }}">
3532

33+
{% if item.id in user_wishlist %}
34+
<!-- Filled heart if already in wishlist -->
35+
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="#f00" viewBox="0 0 24 24" stroke="currentColor">
36+
<path d="M12.1 21.35c-.33.11-.71.11-1.04 0C7.07 19.75 2 15.42 2 10.34A6.34 6.34 0 016.34 4c2.09 0 4.22 1.03 5.66 2.55C13.44 5.03 15.57 4 17.66 4A6.34 6.34 0 0122 10.34c0 5.08-5.07 9.41-8.94 10.99z"></path>
37+
</svg>
38+
{% else %}
39+
<!-- Empty heart if not in wishlist -->
3640
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
3741
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
3842
d="M4.318 6.318a4.5 4.5 0 016.364 0L12 7.636l1.318-1.318a4.5 4.5 0 116.364 6.364L12 20.364l-7.682-7.682a4.5 4.5 0 010-6.364z" />
3943
</svg>
44+
{% endif %}
4045
</button>
41-
</a>
42-
{% else %}
43-
<a href="{% url 'toggle_wishlist' item.id %}">
44-
<button
45-
class="px-2 py-2 bg-[#050a30] text-white rounded-full hover:bg-blue-600 focus:outline-none"
46-
title="Remove from Wishlist">
47-
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="#f00" viewBox="0 0 24 24" stroke="currentColor">
48-
<path d="M12.1 21.35c-.33.11-.71.11-1.04 0C7.07 19.75 2 15.42 2 10.34A6.34 6.34 0 016.34 4c2.09 0 4.22 1.03 5.66 2.55C13.44 5.03 15.57 4 17.66 4A6.34 6.34 0 0122 10.34c0 5.08-5.07 9.41-8.94 10.99z"></path>
49-
</svg>
50-
</button>
51-
</a>
52-
{% endif %}
46+
{% else %}
5347
{% endif %}
48+
5449
</div>
5550
</div>
5651
</div>

core/templates/core/main.html

+10-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
<link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap" rel="stylesheet">
1010
<link rel="stylesheet" href="{% static 'css/tailwind_output.css' %}">
1111
<link rel="icon" type="image/jpg" href="{% static 'images/logo.png' %}">
12+
13+
<meta name="csrf-token" content="{{ csrf_token }}">
14+
1215
<title>Convenience Delivered, Every Time | QuickDash</title>
1316
</head>
1417
<body class="bg-[#cae8ff] text-black relative">
@@ -80,13 +83,15 @@
8083

8184
<a href="{% url 'profile' %}" class="hidden lg:block px-4 py-2 text-gray-700 hover:scale-105 hover:shadow-md cursor-pointer transition-all duration-200 ease-in-out">Profile</a>
8285
{% if request.user.is_authenticated %}
83-
<a href="{% url 'logout' %}" class="hidden lg:block px-4 py-2 text-gray-700 hover:scale-105 hover:shadow-md cursor-pointer transition-all duration-200 ease-in-out">Logout</a>
86+
<a href="{% url 'logout' %}" class="block px-4 py-2 text-gray-700 hover:scale-105 hover:shadow-md cursor-pointer transition-all duration-200 ease-in-out">Logout</a>
8487

8588
{% else %}
86-
<a href="{% url 'login' %}" class="hidden lg:block px-4 py-2 text-gray-700 hover:scale-105 hover:shadow-md cursor-pointer transition-all duration-200 ease-in-out">Login</a>
89+
<a href="{% url 'login' %}" class="block px-4 py-2 text-gray-700 hover:scale-105 hover:shadow-md cursor-pointer transition-all duration-200 ease-in-out">Login</a>
8790
{% endif %}
8891
</div>
8992
</div>
93+
94+
9095
</header>
9196

9297
<!-- Floating Contact Icon -->
@@ -104,7 +109,7 @@
104109
<a href="{% url 'cart' %}" class="flex flex-col items-center space-y-1 relative">
105110
<img src="{% static 'images/cart.svg' %}" alt="Cart" class="h-6 w-6">
106111
{% if request.user.is_authenticated %}
107-
<span class="absolute -top-3.5 left-4 bg-red-500 text-white text-xs rounded-full w-5 h-5 flex items-center justify-center">{{ total_items }}</span>
112+
<span id="cart-count" class="absolute -top-3.5 left-4 bg-red-500 text-white text-xs rounded-full w-5 h-5 flex items-center justify-center">{{ total_items }}</span>
108113
{% endif %}
109114
<span class="text-xs">Cart</span>
110115
</a>
@@ -127,6 +132,8 @@
127132

128133

129134
</body>
135+
<script src="{% static 'js/cart.js' %}"></script>
136+
<script src="{% static 'js/wishlist.js' %}"></script>
130137
<script>
131138
const searchBar = document.getElementById("search-bar");
132139

core/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def homePage(request):
1414
items = Item.objects.filter(
1515
Q(name__icontains=query) |
1616
Q(description__icontains=query)
17-
)
17+
).order_by('-date_added')
1818

1919

2020
if restaurant_filter:

db.sqlite3

0 Bytes
Binary file not shown.

orders/urls.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44

55
urlpatterns = [
6-
path('add-to-cart/<int:item_id>/', views.add_to_cart, name='add_to_cart'),
6+
path('ajax/add-to-cart', views.ajax_add_to_cart, name='ajax_add_to_cart'),
7+
78
path('cart/', views.cartPage, name='cart'),
89

910
path('cart/remove/<int:item_id>/', views.remove_from_cart, name='remove_from_cart'),
@@ -15,7 +16,8 @@
1516
path('add-restaurant/', views.add_restaurant, name='add_restaurant'),
1617

1718
path('wishlist/', views.wishlist, name='wishlist'),
18-
path('wishlist/toggle/<int:item_id>/', views.toggle_wishlist, name='toggle_wishlist'),
19+
20+
path('ajax/add-to-wishlist/', views.ajax_add_to_wishlist, name='ajax_add_to_wishlist'),
1921

2022
path('checkout/', views.checkout, name='checkout'),
2123

orders/views.py

+36-8
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,24 @@
1111
from django.db.models import Sum
1212

1313

14-
1514
@login_required(login_url='login')
16-
def add_to_cart(request, item_id):
17-
item = get_object_or_404(Item, id=item_id)
18-
cart_item, created = CartItem.objects.get_or_create(user=request.user, item=item)
19-
if not created:
20-
cart_item.quantity += 1
21-
cart_item.save()
22-
return redirect('home')
15+
def ajax_add_to_cart(request):
16+
if request.method == 'POST':
17+
item_id = request.POST.get('item_id')
18+
item = get_object_or_404(Item, id=item_id)
19+
20+
# Get or create the cart item
21+
cart_item, created = CartItem.objects.get_or_create(user=request.user, item=item)
22+
23+
if not created:
24+
cart_item.quantity += 1
25+
cart_item.save()
26+
27+
# Get updated cart total
28+
total_items = sum(item.quantity for item in CartItem.objects.filter(user=request.user))
29+
30+
return JsonResponse({'status': 'success', 'total_items': total_items})
31+
return JsonResponse({'status': 'error', 'message': 'Invalid Request'})
2332

2433
@login_required(login_url='login')
2534
def cartPage(request):
@@ -109,6 +118,25 @@ def wishlist(request):
109118
wishlist_items = WishListItem.objects.filter(user=request.user).select_related('item')
110119
return render(request, 'orders/wishlist.html', {'wishlist_items': wishlist_items})
111120

121+
@login_required(login_url='login')
122+
def ajax_add_to_wishlist(request):
123+
if request.method == 'POST':
124+
item_id = request.POST.get('item_id')
125+
item = get_object_or_404(Item, id=item_id)
126+
127+
# Check if the item is already in the wishlist
128+
wishlist_item, created = WishListItem.objects.get_or_create(user=request.user, item=item)
129+
130+
if not created:
131+
wishlist_item.delete()
132+
status = 'removed'
133+
else:
134+
wishlist_item.save()
135+
status = 'added'
136+
137+
return JsonResponse({'status': status})
138+
return JsonResponse({'status': 'error', 'message': 'Invalid Request'})
139+
112140
@login_required(login_url='login')
113141
def checkout(request):
114142
cart_items = get_user_cart(request.user)

static/css/styles.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@tailwind base
2+
@tailwind components
3+
@tailwind utilities

0 commit comments

Comments
 (0)