Skip to content

Commit b1ed534

Browse files
committed
Automatically select value in select with watch
1 parent 3c24d35 commit b1ed534

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

components/Products/ProductsSingleProduct.vue

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
* @prop {string} id - The ID of the product to display.
4949
* @prop {string} slug - The slug of the product to display.
5050
*/
51+
52+
import { ref, watch } from 'vue';
53+
5154
import GET_SINGLE_PRODUCT_QUERY from "@/apollo/queries/GET_SINGLE_PRODUCT_QUERY.gql";
5255
import ADD_TO_CART_MUTATION from "@/apollo/mutations/ADD_TO_CART_MUTATION.gql";
5356
@@ -62,7 +65,7 @@ const isLoading = useState("isLoading", () => false);
6265
6366
const cart = useCart();
6467
65-
const selectedVariation = ref(null)
68+
const selectedVariation = ref(); // TODO Use selectedVariation v-model and implement this functionality to support multiple variants
6669
6770
const props = defineProps({
6871
id: { type: String, required: true },
@@ -72,6 +75,16 @@ const props = defineProps({
7275
const variables = { id: props.id, slug: props.slug };
7376
const { data } = await useAsyncQuery(GET_SINGLE_PRODUCT_QUERY, variables);
7477
78+
watch(
79+
() => data.value,
80+
(dataValue) => {
81+
if (dataValue && dataValue.product.variations.nodes.length > 0) {
82+
selectedVariation.value = dataValue.product.variations.nodes[0].databaseId;
83+
}
84+
},
85+
{ immediate: true }
86+
);
87+
7588
/**
7689
* Adds a product to the cart by calling the addToCart mutation with the given product.
7790
*

0 commit comments

Comments
 (0)