Skip to content

Commit

Permalink
revert discount related changes (ProductOffer.cost)
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermespopolin committed Jul 5, 2023
1 parent 7a8fe5e commit 289a4c5
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ import { motion } from 'framer-motion'
import { useTranslation } from 'next-i18next'
import React, { ReactNode, useState } from 'react'
import { ChevronIcon, Text, theme } from 'ui'
import { ProductOfferFragment } from '@/services/apollo/generated'
import { useFormatter } from '@/utils/useFormatter'

type Props = { defaultOpen: boolean; cost: ProductOfferFragment['cost']; children: ReactNode }
type Props = { defaultOpen: boolean; price: string; children: ReactNode }

export const CartEntryCollapsible = ({ defaultOpen, cost, children }: Props) => {
export const CartEntryCollapsible = ({ defaultOpen, price, children }: Props) => {
const [open, setOpen] = useState(defaultOpen)
const { t } = useTranslation('cart')
const formatter = useFormatter()

const hasDiscountApplied = cost.discount.amount > 0

return (
<Collapsible.Root open={open} onOpenChange={setOpen}>
Expand All @@ -24,12 +19,7 @@ export const CartEntryCollapsible = ({ defaultOpen, cost, children }: Props) =>
<ChevronIcon color={theme.colors.textTertiary} size="1rem" />
</Trigger>
<PriceFlex>
{hasDiscountApplied && (
<Text color="textSecondary" strikethrough={true}>
{formatter.monthlyPrice(cost.gross)}
</Text>
)}
<Text>{formatter.monthlyPrice(cost.net)}</Text>
<Text>{price}</Text>
</PriceFlex>
</DetailsHeader>
<CollapsibleContent forceMount>
Expand Down Expand Up @@ -66,7 +56,6 @@ const PriceFlex = styled.div({
display: 'flex',
alignItems: 'center',
height: '100%',
gap: theme.space.xs,
})

const Trigger = styled(Collapsible.Trigger)({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useEditProductOffer } from '@/components/CartPage/useEditProductOffer'
import { Pillow } from '@/components/Pillow/Pillow'
import { SpaceFlex } from '@/components/SpaceFlex/SpaceFlex'
import { CartFragmentFragment } from '@/services/apollo/generated'
import { useFormatter } from '@/utils/useFormatter'
import { CartEntry } from '../CartInventory.types'
import { RemoveEntryDialog } from '../RemoveEntryDialog'
import { CartEntryCollapsible } from './CartEntryCollapsible'
Expand All @@ -23,6 +24,7 @@ export const CartEntryItem = ({ defaultOpen = false, ...props }: Props) => {
const { shopSessionId, readOnly, onRemove, ...cartEntry } = props
const { title: titleLabel, cost, pillow } = cartEntry
const { t } = useTranslation('cart')
const formatter = useFormatter()

const [editProductOffer, editState] = useEditProductOffer()
const handleConfirmEdit = () => {
Expand All @@ -49,7 +51,7 @@ export const CartEntryItem = ({ defaultOpen = false, ...props }: Props) => {

<Space y={1}>
<Layout.Details>
<CartEntryCollapsible defaultOpen={defaultOpen} cost={cost}>
<CartEntryCollapsible defaultOpen={defaultOpen} price={formatter.monthlyPrice(cost)}>
<DetailsSheet {...cartEntry} />
</CartEntryCollapsible>
</Layout.Details>
Expand Down
12 changes: 1 addition & 11 deletions apps/store/src/components/CartInventory/CartEntryOfferItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ export const CartEntryOfferItem = ({ shopSessionId, product, offer }: CartOfferI

if (!show) return null

const hasDiscountApplied = offer.cost.discount.amount > 0

return (
<Layout.Main>
<Layout.Pillow>
Expand Down Expand Up @@ -97,14 +95,7 @@ export const CartEntryOfferItem = ({ shopSessionId, product, offer }: CartOfferI
</Space>
</Layout.Actions>

<Layout.Price>
{hasDiscountApplied && (
<Text color="textSecondary" strikethrough={true}>
{formatter.monthlyPrice(offer.cost.gross)}
</Text>
)}
{formatter.monthlyPrice(offer.cost.net)}
</Layout.Price>
<Layout.Price>{formatter.monthlyPrice(offer.price)}</Layout.Price>
</Layout.Main>
)
}
Expand Down Expand Up @@ -151,7 +142,6 @@ const Layout = {
gridArea: GRID_AREAS.Price,
display: 'flex',
alignItems: 'center',
gap: theme.space.xs,
}),
Content: styled.div({ gridArea: GRID_AREAS.Content }),
Actions: styled.div({ gridArea: GRID_AREAS.Actions }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const getCartEntry = (item: ShopSession['cart']['entries'][number]): Cart
return {
offerId: item.id,
title: item.variant.product.displayNameFull,
cost: item.cost,
cost: item.price,
startDate: hasCancellation ? undefined : convertToDate(item.startDate),
pillow: {
src: item.variant.product.pillowImage.src,
Expand Down
2 changes: 1 addition & 1 deletion apps/store/src/components/CartInventory/CartInventory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const CartInventory = ({ shopSessionId, cart, readOnly = false }: Props)
key={item.id}
offerId={item.id}
title={item.variant.product.displayNameFull}
cost={item.cost}
cost={item.price}
pillow={{
src: item.variant.product.pillowImage.src,
alt: item.variant.product.pillowImage.alt ?? '',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CartFragmentFragment, ProductOfferFragment } from '@/services/apollo/generated'
import { CartFragmentFragment } from '@/services/apollo/generated'
import { Money } from '@/utils/formatter'

export type CartEntry = {
offerId: string
title: string
cost: ProductOfferFragment['cost']
cost: Money
startDate?: Date | null
pillow: { src: string; alt?: string }
documents: CartFragmentFragment['entries'][number]['variant']['documents']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,6 @@ const cart = {
amount: 125,
currencyCode: CurrencyCode.Sek,
},
cost: {
gross: {
amount: 125,
currencyCode: CurrencyCode.Sek,
},
net: {
amount: 125,
currencyCode: CurrencyCode.Sek,
},
discount: {
amount: 0,
currencyCode: CurrencyCode.Sek,
},
},
cancellation: {
option: ExternalInsuranceCancellationOption.None,
requested: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const DeductibleSelector = ({ offers, selectedOffer, onValueChange }: Pro
if (offer.deductible) {
levels.push({
id: offer.id,
price: offer.cost.net,
price: offer.price,
title: offer.deductible.displayName,
description: offer.deductible.tagline,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ import { SpaceFlex } from '@/components/SpaceFlex/SpaceFlex'
import {
ExternalInsuranceCancellationOption,
ProductOfferFragment,
RedeemedCampaign,
} from '@/services/apollo/generated'
import { PriceIntent } from '@/services/priceIntent/priceIntent.types'
import { ShopSession } from '@/services/shopSession/ShopSession.types'
import { useTracking } from '@/services/Tracking/useTracking'
import { convertToDate } from '@/utils/date'
import { PageLink } from '@/utils/PageLink'
import { useGetDiscountExplanation } from '@/utils/useDiscountExplanation'
import { useFormatter } from '@/utils/useFormatter'
import { CancellationForm, CancellationOption } from './CancellationForm/CancellationForm'
import { ComparisonTableModal } from './ComparisonTableModal'
Expand Down Expand Up @@ -102,12 +100,31 @@ export const OfferPresenter = (props: Props) => {

const [handleUpdateCancellation, updateCancellationInfo] = useUpdateCancellation({ priceIntent })

const discountTooltipProps = useDiscountTooltipProps(
selectedOffer,
shopSession.cart.redeemedCampaigns,
)
const discountTooltipProps = useMemo(() => {
if (!selectedOffer.priceMatch) return null

const company = selectedOffer.priceMatch.externalInsurer.displayName

if (selectedOffer.priceMatch.priceReduction.amount < 1) {
// No price reduction due to incomparable offers
const amount = formatter.monthlyPrice(selectedOffer.priceMatch.externalPrice)
return {
children: t('PRICE_MATCH_BUBBLE_INCOMPARABLE_TITLE', { amount, company }),
subtitle: t('PRICE_MATCH_BUBBLE_INCOMPARABLE_SUBTITLE'),
color: 'gray',
} as const
}

const displayPrice = formatter.monthlyPrice(selectedOffer.cost.net)
const priceReduction = formatter.monthlyPrice(selectedOffer.priceMatch.priceReduction)

return {
children: t('PRICE_MATCH_BUBBLE_SUCCESS_TITLE', { amount: priceReduction }),
subtitle: t('PRICE_MATCH_BUBBLE_SUCCESS_SUBTITLE', { company }),
color: 'green',
} as const
}, [selectedOffer.priceMatch, formatter, t])

const displayPrice = formatter.monthlyPrice(selectedOffer.price)

const cancellationOption = getCancellationOption({
priceIntent,
Expand Down Expand Up @@ -284,56 +301,6 @@ type GetCancellationOptionParams = {
productOffer: ProductOfferFragment
}

const useDiscountTooltipProps = (
selectedOffer: ProductOfferFragment,
redeemedCampaigns?: Array<RedeemedCampaign>,
) => {
const { t } = useTranslation(['purchase-form', 'cart'])
const formatter = useFormatter()
const getDiscountExplanation = useGetDiscountExplanation()

const tooltipProps = useMemo(() => {
if (selectedOffer.priceMatch) {
const company = selectedOffer.priceMatch.externalInsurer.displayName

if (selectedOffer.priceMatch.priceReduction.amount < 1) {
// No price reduction due to incomparable offers
const amount = formatter.monthlyPrice(selectedOffer.priceMatch.externalPrice)
return {
children: t('PRICE_MATCH_BUBBLE_INCOMPARABLE_TITLE', { amount, company }),
subtitle: t('PRICE_MATCH_BUBBLE_INCOMPARABLE_SUBTITLE'),
color: 'gray',
} as const
}

const priceReduction = formatter.monthlyPrice(selectedOffer.priceMatch.priceReduction)

return {
children: t('PRICE_MATCH_BUBBLE_SUCCESS_TITLE', { amount: priceReduction }),
subtitle: t('PRICE_MATCH_BUBBLE_SUCCESS_SUBTITLE', { company }),
color: 'green',
} as const
}

const redeemedCampaign = redeemedCampaigns?.[0]
if (redeemedCampaign && selectedOffer.cost.discount.amount > 0) {
return {
children: getDiscountExplanation({
...redeemedCampaign.discount,
amount: selectedOffer.cost.discount,
}),
subtitle: t('DISCOUNT_PRICE_AFTER_EXPIRATION', {
amount: formatter.monthlyPrice(selectedOffer.cost.gross),
ns: 'cart',
}),
color: 'green',
} as const
}
}, [t, formatter, getDiscountExplanation, selectedOffer, redeemedCampaigns])

return tooltipProps
}

const getCancellationOption = (params: GetCancellationOptionParams): CancellationOption => {
const {
productOffer: { cancellation, startDate },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const ProductTierSelector = ({ offers, selectedOffer, onValueChange }: Pr
key={offer.id}
value={offer.id}
title={offer.variant.displayName}
price={formatter.monthlyPrice(offer.cost.net)}
price={formatter.monthlyPrice(offer.price)}
description={getVariantDescription(offer.variant.typeOfContract)}
/>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export const PurchaseForm = () => {

notifyProductAdded({
name: productData.displayNameFull,
price: formatter.monthlyPrice(item.cost.net),
price: formatter.monthlyPrice(item.price),
pillowSrc: productData.pillowImage.src,
description:
!item.cancellation.requested ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { ProductOfferFragment } from '@/services/apollo/generated'

export const getOffersByPrice = (offers: Array<ProductOfferFragment>) => {
return [...offers].sort((a, b) => {
if (a.cost.net.amount < b.cost.net.amount) return -1
if (a.cost.net.amount > b.cost.net.amount) return 1
if (a.price.amount < b.price.amount) return -1
if (a.price.amount > b.price.amount) return 1
return 0
})
}
16 changes: 3 additions & 13 deletions apps/store/src/graphql/OfferRecommendationFragment.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,8 @@ fragment OfferRecommendation on ProductOffer {
displayNameFull
}
}
cost {
gross {
amount
currencyCode
}
net {
amount
currencyCode
}
discount {
amount
currencyCode
}
price {
amount
currencyCode
}
}
16 changes: 3 additions & 13 deletions apps/store/src/graphql/ProductOfferFragment.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,9 @@ fragment ProductOffer on ProductOffer {
url
}
}
cost {
gross {
amount
currencyCode
}
net {
amount
currencyCode
}
discount {
amount
currencyCode
}
price {
amount
currencyCode
}
startDate
cancellation {
Expand Down
15 changes: 7 additions & 8 deletions apps/store/src/services/Tracking/Tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type TrackingProductData = {
}

type TrackingOffer = {
cost: ProductOfferFragment['cost']
price: ProductOfferFragment['price']
variant: {
typeOfContract: ProductOfferFragment['variant']['typeOfContract']
product: {
Expand Down Expand Up @@ -140,9 +140,8 @@ export class Tracking {
const userData = await getLegacyUserData(this.context)
const eventData = {
offerData: {
insurance_price: offer.cost.gross.amount,
currency: offer.cost.gross.currencyCode as string,

insurance_price: offer.price.amount,
currency: offer.price.currencyCode as string,
insurance_type: offer.variant.typeOfContract,
flow_type: offer.variant.product.name,
...getLegacyEventFlags([offer.variant.typeOfContract]),
Expand Down Expand Up @@ -304,13 +303,13 @@ const offerToEcommerceEvent = ({
return {
event,
ecommerce: {
value: offer.cost.gross.amount,
currency: offer.cost.gross.currencyCode,
value: offer.price.amount,
currency: offer.price.currencyCode,
items: [
{
item_id: offer.variant.product.id,
item_name: offer.variant.product.displayNameFull,
price: offer.cost.gross.amount,
price: offer.price.amount,
item_variant: offer.variant.typeOfContract,
...(source && { item_list_id: source }),
},
Expand Down Expand Up @@ -341,7 +340,7 @@ const cartToEcommerceEvent = (
items: cart.entries.map((entry) => ({
item_id: entry.variant.product.id,
item_name: entry.variant.product.displayNameFull,
price: entry.cost.gross.amount,
price: entry.price.amount,
variant: entry.variant.typeOfContract,
})),
},
Expand Down

0 comments on commit 289a4c5

Please sign in to comment.