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 7, 2023
1 parent 8c1afa9 commit a698b78
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@ import { motion } from 'framer-motion'
import { useTranslation } from 'next-i18next'
import React, { ReactNode } from 'react'
import { ChevronIcon, Text, theme } from 'ui'
import { ProductOfferFragment } from '@/services/apollo/generated'
import { useFormatter } from '@/utils/useFormatter'

type Props = {
open: boolean
onOpenChange: (open: boolean) => void
cost: ProductOfferFragment['cost']
price: string
children: ReactNode
}

export const CartEntryCollapsible = ({ open, onOpenChange, cost, children }: Props) => {
export const CartEntryCollapsible = ({ open, onOpenChange, price, children }: Props) => {
const { t } = useTranslation('cart')
const formatter = useFormatter()

const hasDiscountApplied = cost.discount.amount > 0

return (
<Collapsible.Root open={open} onOpenChange={onOpenChange}>
Expand All @@ -28,12 +23,7 @@ export const CartEntryCollapsible = ({ open, onOpenChange, cost, children }: Pro
<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 @@ -70,7 +60,6 @@ const PriceFlex = styled.div({
display: 'flex',
alignItems: 'center',
height: '100%',
gap: theme.space.xs,
})

const Trigger = styled.div({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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 @@ -24,6 +25,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 [expanded, setExpanded] = useState(defaultOpen)

const [editProductOffer, editState] = useEditProductOffer()
Expand Down Expand Up @@ -53,7 +55,11 @@ export const CartEntryItem = ({ defaultOpen = false, ...props }: Props) => {

<Layout.Bottom>
<Layout.Details>
<CartEntryCollapsible open={expanded} onOpenChange={setExpanded} cost={cost}>
<CartEntryCollapsible
open={expanded}
onOpenChange={setExpanded}
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 @@ -77,20 +77,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
4 changes: 2 additions & 2 deletions apps/store/src/pages/manypets/migration/[shopSessionId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ export const getServerSideProps: GetServerSideProps<Props, Params> = async (cont
const baseOffer = offers[0]
// TODO: take total cost from shopsession.cart.cost
const totalCost: Money = {
amount: offers.reduce((sum, offer) => sum + offer.cost.net.amount, 0),
currencyCode: baseOffer.cost.net.currencyCode,
amount: offers.reduce((sum, offer) => sum + offer.price.amount, 0),
currencyCode: baseOffer.price.currencyCode,
}
const offersWithStartDate = offers.filter((offer) => offer.startDate !== undefined)
const latestAdoptionDate = offersWithStartDate.sort(sortByStartDate)[0].startDate
Expand Down
Loading

0 comments on commit a698b78

Please sign in to comment.