File tree Expand file tree Collapse file tree 1 file changed +14
-1
lines changed Expand file tree Collapse file tree 1 file changed +14
-1
lines changed Original file line number Diff line number Diff line change 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+
5154import GET_SINGLE_PRODUCT_QUERY from " @/apollo/queries/GET_SINGLE_PRODUCT_QUERY.gql" ;
5255import ADD_TO_CART_MUTATION from " @/apollo/mutations/ADD_TO_CART_MUTATION.gql" ;
5356
@@ -62,7 +65,7 @@ const isLoading = useState("isLoading", () => false);
6265
6366const 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
6770const props = defineProps ({
6871 id: { type: String , required: true },
@@ -72,6 +75,16 @@ const props = defineProps({
7275const variables = { id: props .id , slug: props .slug };
7376const { 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 *
You can’t perform that action at this time.
0 commit comments