11<script setup lang="ts">
2- import type { ProductVariantFieldsFragment } from ' #shopify/storefront'
2+ import type { ProductFieldsFragment , ProductVariantFieldsFragment } from ' #shopify/storefront'
33import type { FormSubmitEvent } from ' #ui/types'
44
5+ const props = defineProps <{
6+ product? : ProductFieldsFragment
7+ }>()
8+
59const selectedVariant = defineModel <ProductVariantFieldsFragment >()
610
11+ const emit = defineEmits <{
12+ submit: [FormSubmitEvent <typeof state >]
13+ }>()
14+
715const { language, country } = useLocalization ()
816const { locale } = useI18n ()
917const { add } = useCart ()
@@ -15,7 +23,7 @@ const state = reactive({
1523 selectedOptions: selectedVariant .value ?.selectedOptions ,
1624})
1725
18- const { data : product } = await useStorefrontData (` product-options-${locale .value }-${handle .value } ` , ` #graphql
26+ const { data } = await useStorefrontData (` product-options-${locale .value }-${handle .value } ` , ` #graphql
1927 query FetchProductOptions($handle: String, $language: LanguageCode, $country: CountryCode, $selectedOptions: [SelectedOptionInput!])
2028 @inContext(language: $language, country: $country) {
2129 product(handle: $handle) {
@@ -47,14 +55,20 @@ const { data: product } = await useStorefrontData(`product-options-${locale.valu
4755
4856const loading = ref (false )
4957
50- watch (() => product .value ?.selectedOrFirstAvailableVariant , variant => selectedVariant .value = variant ?? undefined )
58+ const product = computed (() => data .value ?? props .product )
59+
60+ watch (() => data .value ?.selectedOrFirstAvailableVariant , variant => selectedVariant .value = variant ?? undefined )
5161
5262const onSubmit = async (event : FormSubmitEvent <typeof state >) => {
5363 if (! selectedVariant .value ) return
5464
5565 loading .value = true
5666
57- await add (selectedVariant .value .id , event .data .quantity ).then (() => loading .value = false )
67+ await add (selectedVariant .value .id , event .data .quantity ).finally (() => {
68+ loading .value = false
69+
70+ emit (' submit' , event )
71+ })
5872}
5973 </script >
6074
0 commit comments