JezK
Edit File: sc-cart-form.entry.js.map
{"file":"sc-cart-form.entry.js","mappings":";;;;;;;;;;;;;;;;AASA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;8EA8EiC,UAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qNA8DH,UAAE;;;;;;;","names":[],"sources":["src/components/controllers/cart/sc-cart-form/sc-cart-form.tsx"],"sourcesContent":["import { Component, h, Prop, State } from '@stencil/core';\nimport { __ } from '@wordpress/i18n';\n\nimport { convertLineItemsToLineItemData } from '../../../../functions/line-items';\nimport { createOrUpdateCheckout } from '../../../../services/session';\nimport { state as checkoutState } from '@store/checkout';\nimport uiStore from '@store/ui';\nimport { Checkout, LineItemData } from '../../../../types';\nimport { updateFormState } from '@store/form/mutations';\nconst query = {\n expand: [\n 'line_items',\n 'line_item.price',\n 'line_item.component_line_items',\n 'price.product',\n 'customer',\n 'customer.shipping_address',\n 'payment_intent',\n 'discount',\n 'discount.promotion',\n 'discount.coupon',\n 'shipping_address',\n 'tax_identifier',\n ],\n};\n\n@Component({\n tag: 'sc-cart-form',\n styles: 'sc-cart-form { display: inline-block }',\n shadow: false,\n})\nexport class ScCartForm {\n private form: HTMLScFormElement;\n\n /** The quantity */\n @Prop({ mutable: true }) quantity: number = 1;\n\n /** The price id to add. */\n @Prop() priceId: string;\n\n /** The variant id to add. */\n @Prop() variantId: string;\n\n /** Are we in test or live mode. */\n @Prop() mode: 'test' | 'live' = 'live';\n\n /** The form id to use for the cart. */\n @Prop({ reflect: true }) formId: string;\n\n /** Is it busy */\n @State() busy: boolean;\n @State() error: string;\n\n /** Find a line item with this price. */\n getLineItem() {\n const lineItem = (checkoutState?.checkout?.line_items?.data || []).find(item => {\n if (this.variantId) {\n return item.variant?.id === this.variantId && item.price?.id === this.priceId;\n }\n return item.price?.id === this.priceId;\n });\n if (!lineItem?.id) {\n return false;\n }\n return {\n id: lineItem?.id,\n price_id: lineItem?.price?.id,\n quantity: lineItem?.quantity,\n } as LineItemData;\n }\n\n /** Add the item to cart. */\n async addToCart() {\n const { price } = await this.form.getFormJson();\n try {\n updateFormState('FETCH');\n // if it's ad_hoc, update the amount. Otherwise increment the quantity.\n checkoutState.checkout = await this.addOrUpdateLineItem({\n ...(!!price ? { ad_hoc_amount: parseInt(price as string) || null } : {}),\n ...(!!this.variantId ? { variant_id: (this.variantId as string) || null } : {}),\n });\n updateFormState('RESOLVE');\n // store the checkout in localstorage and open the cart\n uiStore.set('cart', { ...uiStore.state.cart, ...{ open: true } });\n } catch (e) {\n updateFormState('REJECT');\n console.error(e);\n this.error = e?.message || __('Something went wrong', 'surecart');\n }\n }\n\n async addOrUpdateLineItem(data: any = {}) {\n // get the current line item from the price id.\n let lineItem = this.getLineItem() as LineItemData;\n\n // convert line items response to line items post.\n let existingData = convertLineItemsToLineItemData(checkoutState?.checkout?.line_items || []);\n\n // Line item does not exist. Add it.\n return (await createOrUpdateCheckout({\n id: checkoutState?.checkout?.id,\n data: {\n live_mode: this.mode === 'live',\n line_items: [\n ...(existingData || []).map((item: LineItemData) => {\n // if the price ids match (we have already a line item)\n const priceOrVariantMatches = this.variantId ? item.price_id === this.priceId && item.variant_id === this.variantId : item.price_id === this.priceId;\n\n if (priceOrVariantMatches) {\n return {\n ...item,\n ...(!!data?.ad_hoc_amount ? { ad_hoc_amount: data?.ad_hoc_amount } : {}),\n ...(!!data?.variant_id ? { variant_id: data?.variant_id } : {}),\n quantity: !item?.ad_hoc_amount ? item?.quantity + 1 : 1, // only increase quantity if not ad_hoc.\n };\n }\n // return item.\n return item;\n }),\n // add a line item if one does not exist.\n ...(!lineItem\n ? [\n {\n price_id: this.priceId,\n variant_id: this.variantId,\n ...(!!data?.ad_hoc_amount ? { ad_hoc_amount: data?.ad_hoc_amount } : {}),\n quantity: 1,\n },\n ]\n : []),\n ],\n },\n query: {\n ...query,\n form_id: this.formId,\n },\n })) as Checkout;\n }\n\n render() {\n return (\n <sc-form\n ref={el => (this.form = el as HTMLScFormElement)}\n onScSubmit={() => {\n this.addToCart();\n }}\n >\n {this.error && (\n <sc-alert open={!!this.error} type=\"danger\">\n <span slot=\"title\">{__('Error', 'surecart')}</span>\n {this.error}\n </sc-alert>\n )}\n <slot />\n </sc-form>\n );\n }\n}\n"],"version":3}