import * as React from "react"; import { get, head } from "lodash"; import ReactJson from "react-json-view"; import numeral from "numeral"; import { Table } from ".."; import { V1ObjectWrapper, convertProperties } from "../V1ObjectWrapper"; import { PluginProps, OrderSummaryProps, OrderSummaryTypes, OrderSummaryItemTypes, } from "./types"; import { ComponentTypes } from "../types"; export const OrderSummary: React.FC = ({ cart = [], style = {}, properties = {}, cellStyle, footerCellStyle, type = OrderSummaryTypes.STANDARD, showHeader = true, showCart = true, showSubtotal = true, showTotal = true, }) => { const total = cart.reduce((acc, cv) => { console.log({ acc, cv }); return acc + cv.value; }, 0); // Psuedocode for calculating values // All products added up // - Discounts ($) // - Discount (%) // - Promo Codes ($) // - Promo Codes (%) // + Shipping // + Sales Tax // const subTotal = cart.filter(itm => itm.itemType === OrderSummaryItemTypes.PRODUCT).reduce((acc, cv) => acc + (cv.value as number), 0) // const dollarBasedDiscounts = cart.filter(itm => itm.itemType === OrderSummaryItemTypes.DISCOUNT && (itm.value as string).indexOf('%') === -1).reduce((acc, cv) => acc + numeral(cv.value), 0) // const percentBasedDiscounts = get(head(cart.filter(itm => itm.itemType === OrderSummaryItemTypes.DISCOUNT && (itm.value as string).indexOf('%') > -1)), 'value', 1) // const shippingTotals = cart.filter(itm => itm.itemType === OrderSummaryItemTypes.SHIPPING).reduce((acc, cv) => acc + numeral(cv.value), 0) // const salesTax = get(head(cart.filter(itm => itm.itemType === OrderSummaryItemTypes.SALESTAX && (itm.value as string).indexOf('%') > -1)), 'value', 1) return (
{/* */} [ itm.title, numeral(itm.value).format("$0,0.00"), ]) : []), ...(showSubtotal ? [["Subtotal", numeral(total).format("$0,0.00")]] : []), ]} footer={[ ...(showTotal ? [`Due Today:`, numeral(total).format("$0,0.00")] : []), ]} /> ); }; const OrderSummaryPlugin: React.FC = ({ settings }) => { const properties = convertProperties(settings.properties); return ( ); }; export const onComponentRender = (hook, payload, actions) => { if ( hook.id === "webcomponent" && payload.type === ComponentTypes.ORDERSUMMARY ) { return [OrderSummaryPlugin]; } }; export default OrderSummary;