Skip to content

Quantity input change #1485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions components/Cart/CartContents.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
:key="product.key"
:product="product"
@remove="handleRemoveProduct"
@update-quantity="handleUpdateQuantity"
/>
</section>
<CommonButton v-if="showCheckoutButton" link-to="/checkout" center-button>
Expand Down Expand Up @@ -73,6 +74,20 @@ onMounted(async () => {
isLoading.value = false;
}
});

/**
* Handles updating the quantity of a cart item.
*
* @param {{ key: string, quantity: number }} payload
*/
const handleUpdateQuantity = async ({ key, quantity }) => {
try {
await cart.updateCartItemQuantity(key, quantity);
} catch (error) {
console.error("Error updating cart item quantity:", error);
// Optionally, add user notification here
}
};
</script>

<style scoped>
Expand Down
30 changes: 27 additions & 3 deletions components/Cart/CartItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
<div class="item">
<span class="block mt-2 font-extrabold">Quantity: <br /></span>
<span class="item-content">
{{ product.quantity }}
<CommonInput
:model-value="localQuantity"
:min="1"
:loading="isUpdating"
@update:modelValue="onQuantityChange"
/>
</span>
</div>
<div class="item">
Expand All @@ -46,10 +51,19 @@
* @emits CartItem#remove - Emitted when the remove button is clicked.
*/

import { ref } from "vue";
import { ref, watch } from "vue";
import { formatPrice } from "@/utils/functions";

const isRemoving = ref(false);
const isUpdating = ref(false);
const localQuantity = ref(props.product.quantity);

Check warning

Code scanning / CodeQL

Variable not declared before use Warning

Variable 'props' is used before its
declaration
.

Copilot Autofix

AI 7 days ago

To fix the issue, the declaration of props should be moved above its first usage on line 59. This ensures that the variable is declared before it is used, improving code readability and adhering to best practices. Specifically, the defineProps call for props should be relocated to the top of the <script setup> block, before any other code that references props.


Suggested changeset 1
components/Cart/CartItem.vue

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/components/Cart/CartItem.vue b/components/Cart/CartItem.vue
--- a/components/Cart/CartItem.vue
+++ b/components/Cart/CartItem.vue
@@ -56,2 +56,9 @@
 
+const props = defineProps({
+  product: {
+    type: Object,
+    required: true,
+  },
+});
+
 const isRemoving = ref(false);
@@ -67,9 +74,2 @@
 
-const props = defineProps({
-  product: {
-    type: Object,
-    required: true,
-  },
-});
-
 const emit = defineEmits(["remove", "update-quantity"]);
EOF
@@ -56,2 +56,9 @@

const props = defineProps({
product: {
type: Object,
required: true,
},
});

const isRemoving = ref(false);
@@ -67,9 +74,2 @@

const props = defineProps({
product: {
type: Object,
required: true,
},
});

const emit = defineEmits(["remove", "update-quantity"]);
Copilot is powered by AI and may make mistakes. Always verify output.

Check failure

Code scanning / CodeQL

Property access on null or undefined Error

The base expression of this property access is always undefined.

Copilot Autofix

AI 7 days ago

To fix the issue, the code should directly access the product prop defined by defineProps instead of attempting to access props.product. In Vue's <script setup> syntax, props are directly accessible as variables without needing to reference a props object. This change ensures that the code adheres to Vue's conventions and avoids runtime errors.

The following changes will be made:

  1. Replace all instances of props.product with product.
  2. Ensure that the product prop is correctly accessed throughout the script.

Suggested changeset 1
components/Cart/CartItem.vue

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/components/Cart/CartItem.vue b/components/Cart/CartItem.vue
--- a/components/Cart/CartItem.vue
+++ b/components/Cart/CartItem.vue
@@ -58,6 +58,6 @@
 const isUpdating = ref(false);
-const localQuantity = ref(props.product.quantity);
+const localQuantity = ref(product.quantity);
 
 watch(
-  () => props.product.quantity,
+  () => product.quantity,
   (newVal) => {
@@ -81,3 +81,3 @@
   isRemoving.value = true;
-  emit("remove", props.product.key);
+  emit("remove", product.key);
 };
@@ -85,5 +85,5 @@
 const onQuantityChange = (newQuantity) => {
-  if (newQuantity === props.product.quantity || isUpdating.value) return;
+  if (newQuantity === product.quantity || isUpdating.value) return;
   isUpdating.value = true;
-  emit("update-quantity", { key: props.product.key, quantity: newQuantity });
+  emit("update-quantity", { key: product.key, quantity: newQuantity });
   // UI disables controls while updating; parent resets isUpdating via prop or reactivity
EOF
@@ -58,6 +58,6 @@
const isUpdating = ref(false);
const localQuantity = ref(props.product.quantity);
const localQuantity = ref(product.quantity);

watch(
() => props.product.quantity,
() => product.quantity,
(newVal) => {
@@ -81,3 +81,3 @@
isRemoving.value = true;
emit("remove", props.product.key);
emit("remove", product.key);
};
@@ -85,5 +85,5 @@
const onQuantityChange = (newQuantity) => {
if (newQuantity === props.product.quantity || isUpdating.value) return;
if (newQuantity === product.quantity || isUpdating.value) return;
isUpdating.value = true;
emit("update-quantity", { key: props.product.key, quantity: newQuantity });
emit("update-quantity", { key: product.key, quantity: newQuantity });
// UI disables controls while updating; parent resets isUpdating via prop or reactivity
Copilot is powered by AI and may make mistakes. Always verify output.

watch(
() => props.product.quantity,
(newVal) => {
localQuantity.value = newVal;
}
);

const props = defineProps({
product: {
Expand All @@ -58,7 +72,7 @@
},
});

const emit = defineEmits(["remove"]);
const emit = defineEmits(["remove", "update-quantity"]);

/**
* Emits a "remove" event with the product's key as the payload.
Expand All @@ -67,6 +81,16 @@
isRemoving.value = true;
emit("remove", props.product.key);
};

const onQuantityChange = (newQuantity) => {
if (newQuantity === props.product.quantity || isUpdating.value) return;
isUpdating.value = true;
emit("update-quantity", { key: props.product.key, quantity: newQuantity });
// UI disables controls while updating; parent resets isUpdating via prop or reactivity
setTimeout(() => {
isUpdating.value = false;
}, 1000);
};
</script>

<style scoped>
Expand Down
68 changes: 68 additions & 0 deletions components/common/CommonInput.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<template>
<div class="flex items-center space-x-2">
<button
class="px-2 py-1 bg-gray-200 rounded disabled:opacity-50"
:disabled="loading || value <= min"
@click="updateValue(value - 1)"
aria-label="Decrease quantity"
>
-
</button>
<input
type="number"
:value="value"
:min="min"
:max="max"
class="w-12 text-center border rounded"
:disabled="loading"
@input="onInput"
/>
<button
class="px-2 py-1 bg-gray-200 rounded disabled:opacity-50"
:disabled="loading || (max !== null && value >= max)"
@click="updateValue(value + 1)"
aria-label="Increase quantity"
>
+
</button>
</div>
</template>

<script setup>
import { computed } from "vue";

const props = defineProps({
modelValue: {
type: Number,
required: true,
},
min: {
type: Number,
default: 1,
},
max: {
type: Number,
default: null,
},
loading: {
type: Boolean,
default: false,
},
});

const emit = defineEmits(["update:modelValue"]);

const value = computed(() => props.modelValue);

function updateValue(newValue) {
if (props.loading) return;
if (props.max !== null && newValue > props.max) return;
if (newValue < props.min) return;
emit("update:modelValue", newValue);
}

function onInput(e) {
const newValue = Number(e.target.value);
updateValue(newValue);
}
</script>