From 803de79557c7c01e7737efe6576042d96cc16424 Mon Sep 17 00:00:00 2001 From: Kimmo Puputti Date: Tue, 2 Jan 2018 15:46:35 +0200 Subject: [PATCH] Handle unit type in a general way --- .../BookingBreakdown/BookingBreakdown.js | 62 +++++++++++-------- .../BookingBreakdown.test.js.snap | 16 ++--- 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/src/components/BookingBreakdown/BookingBreakdown.js b/src/components/BookingBreakdown/BookingBreakdown.js index 4f67672d..1936a4d4 100644 --- a/src/components/BookingBreakdown/BookingBreakdown.js +++ b/src/components/BookingBreakdown/BookingBreakdown.js @@ -25,16 +25,16 @@ const isValidCommission = commissionLineItem => { }; const UnitPriceItem = props => { - const { transaction, intl } = props; - const nightPurchase = transaction.attributes.lineItems.find( - item => item.code === 'line-item/night' && !item.reversal + const { transaction, unitType, intl } = props; + const unitPurchase = transaction.attributes.lineItems.find( + item => item.code === unitType && !item.reversal ); - const formattedUnitPrice = formatMoney(intl, nightPurchase.unitPrice); + const formattedUnitPrice = formatMoney(intl, unitPurchase.unitPrice); return (
- + {formattedUnitPrice}
@@ -43,11 +43,12 @@ const UnitPriceItem = props => { UnitPriceItem.propTypes = { transaction: propTypes.transaction.isRequired, + unitType: oneOf([propTypes.LINE_ITEM_NIGHT, propTypes.LINE_ITEM_DAY]).isRequired, intl: intlShape.isRequired, }; const UnitsItem = props => { - const { transaction, booking, intl } = props; + const { transaction, booking, unitType, intl } = props; const dateFormatOptions = { weekday: 'short', @@ -71,18 +72,18 @@ const UnitsItem = props => { }} /> ); - const nightPurchase = transaction.attributes.lineItems.find( - item => item.code === 'line-item/night' && !item.reversal + const unitPurchase = transaction.attributes.lineItems.find( + item => item.code === unitType && !item.reversal ); - const nightCount = nightPurchase.quantity.toFixed(); - const nightCountMessage = ( - + const unitCount = unitPurchase.quantity.toFixed(); + const unitCountMessage = ( + ); return (
{bookingPeriod} - {nightCountMessage} + {unitCountMessage}
); }; @@ -94,20 +95,20 @@ UnitsItem.propTypes = { }; const SubTotalItemMaybe = props => { - const { transaction, isProvider, intl } = props; + const { transaction, unitType, isProvider, intl } = props; const refund = transaction.attributes.lineItems.find( - item => item.code === 'line-item/night' && item.reversal + item => item.code === unitType && item.reversal ); - // Show night purchase line total (unit price * quantity) as a subtotal. + // Show unit purchase line total (unit price * quantity) as a subtotal. // PLEASE NOTE that this assumes that the transaction doesn't have other - // line item types than nights (e.g. week, month, year). + // line item types than the defined unit type (e.g. week, month, year). const showSubTotal = isProvider || refund; - const nightPurchase = transaction.attributes.lineItems.find( - item => item.code === 'line-item/night' && !item.reversal + const unitPurchase = transaction.attributes.lineItems.find( + item => item.code === unitType && !item.reversal ); - const formattedSubTotal = formatMoney(intl, nightPurchase.lineTotal); + const formattedSubTotal = formatMoney(intl, unitPurchase.lineTotal); return showSubTotal ? (
@@ -129,10 +130,10 @@ const CommissionItemMaybe = props => { const { transaction, isProvider, intl } = props; const providerCommissionLineItem = transaction.attributes.lineItems.find( - item => item.code === 'line-item/provider-commission' && !item.reversal + item => item.code === propTypes.LINE_ITEM_PROVIDER_COMMISSION && !item.reversal ); const commissionRefund = transaction.attributes.lineItems.find( - item => item.code === 'line-item/provider-commission' && item.reversal + item => item.code === propTypes.LINE_ITEM_PROVIDER_COMMISSION && item.reversal ); // If commission is passed it will be shown as a fee already reduces from the total price @@ -168,10 +169,10 @@ CommissionItemMaybe.propTypes = { }; const RefundItemMaybe = props => { - const { transaction, intl } = props; + const { transaction, unitType, intl } = props; const refund = transaction.attributes.lineItems.find( - item => item.code === 'line-item/night' && item.reversal + item => item.code === unitType && item.reversal ); return refund ? ( @@ -192,6 +193,8 @@ RefundItemMaybe.propTypes = { export const BookingBreakdownComponent = props => { const { rootClassName, className, userRole, transaction, booking, intl } = props; + const unitType = propTypes.LINE_ITEM_NIGHT; + const isProvider = userRole === 'provider'; const classes = classNames(rootClassName || css.root, className); @@ -217,11 +220,16 @@ export const BookingBreakdownComponent = props => { return (
- - - + + + - +
{totalLabel}
diff --git a/src/components/BookingBreakdown/__snapshots__/BookingBreakdown.test.js.snap b/src/components/BookingBreakdown/__snapshots__/BookingBreakdown.test.js.snap index 2af3179a..881fa2f0 100644 --- a/src/components/BookingBreakdown/__snapshots__/BookingBreakdown.test.js.snap +++ b/src/components/BookingBreakdown/__snapshots__/BookingBreakdown.test.js.snap @@ -11,7 +11,7 @@ exports[`BookingBreakdown customer transaction data matches snapshot 1`] = ` className={undefined} > - BookingBreakdown.pricePerNight + BookingBreakdown.pricePerUnit @@ -75,7 +75,7 @@ exports[`BookingBreakdown pretransaction data matches snapshot 1`] = ` className={undefined} > - BookingBreakdown.pricePerNight + BookingBreakdown.pricePerUnit @@ -139,7 +139,7 @@ exports[`BookingBreakdown provider canceled transaction data matches snapshot 1` className={undefined} > - BookingBreakdown.pricePerNight + BookingBreakdown.pricePerUnit @@ -235,7 +235,7 @@ exports[`BookingBreakdown provider transaction data matches snapshot 1`] = ` className={undefined} > - BookingBreakdown.pricePerNight + BookingBreakdown.pricePerUnit