Skip to content

Commit

Permalink
refact: hide discount related changed under DISCOUNTS feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermespopolin committed Jul 5, 2023
1 parent 6f82517 commit e2c978c
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ 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 type { Money, ProductOfferFragment } from '@/services/apollo/generated'
import { Features } from '@/utils/Features'
import { useFormatter } from '@/utils/useFormatter'

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

export const CartEntryCollapsible = ({ defaultOpen, cost, children }: Props) => {
export const CartEntryCollapsible = ({ defaultOpen, price, cost, 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}>
<DetailsHeader>
Expand All @@ -24,12 +28,16 @@ export const CartEntryCollapsible = ({ defaultOpen, cost, children }: Props) =>
<ChevronIcon color={theme.colors.textTertiary} size="1rem" />
</Trigger>
<PriceFlex>
{hasDiscountApplied && (
{Features.enabled('DISCOUNTS') && cost.discount.amount > 0 && (
<Text color="textSecondary" strikethrough={true}>
{formatter.monthlyPrice(cost.gross)}
</Text>
)}
<Text>{formatter.monthlyPrice(cost.net)}</Text>
<Text>
{Features.enabled('DISCOUNTS')
? formatter.monthlyPrice(cost.net)
: formatter.monthlyPrice(price)}
</Text>
</PriceFlex>
</DetailsHeader>
<CollapsibleContent forceMount>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Props = CartEntry & {

export const CartEntryItem = ({ defaultOpen = false, ...props }: Props) => {
const { shopSessionId, readOnly, onRemove, ...cartEntry } = props
const { title: titleLabel, cost, pillow } = cartEntry
const { title: titleLabel, price, cost, pillow } = cartEntry
const { t } = useTranslation('cart')

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

<Space y={1}>
<Layout.Details>
<CartEntryCollapsible defaultOpen={defaultOpen} cost={cost}>
<CartEntryCollapsible defaultOpen={defaultOpen} price={price} cost={cost}>
<DetailsSheet {...cartEntry} />
</CartEntryCollapsible>
</Layout.Details>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ProductRecommendationFragment,
} from '@/services/apollo/generated'
import { useTracking } from '@/services/Tracking/useTracking'
import { Features } from '@/utils/Features'
import { useFormatter } from '@/utils/useFormatter'

type CartOfferItemProps = {
Expand Down Expand Up @@ -53,8 +54,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 @@ -98,12 +97,14 @@ export const CartEntryOfferItem = ({ shopSessionId, product, offer }: CartOfferI
</Layout.Actions>

<Layout.Price>
{hasDiscountApplied && (
{Features.enabled('DISCOUNTS') && offer.cost.discount.amount > 0 && (
<Text color="textSecondary" strikethrough={true}>
{formatter.monthlyPrice(offer.cost.gross)}
</Text>
)}
{formatter.monthlyPrice(offer.cost.net)}
{Features.enabled('DISCOUNTS')
? formatter.monthlyPrice(offer.cost.net)
: formatter.monthlyPrice(offer.price)}
</Layout.Price>
</Layout.Main>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const getCartEntry = (item: ShopSession['cart']['entries'][number]): Cart
return {
offerId: item.id,
title: item.variant.product.displayNameFull,
price: item.price,
cost: item.cost,
startDate: hasCancellation ? undefined : convertToDate(item.startDate),
pillow: {
Expand Down
1 change: 1 addition & 0 deletions apps/store/src/components/CartInventory/CartInventory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const CartInventory = ({ shopSessionId, cart, readOnly = false }: Props)
key={item.id}
offerId={item.id}
title={item.variant.product.displayNameFull}
price={item.price}
cost={item.cost}
pillow={{
src: item.variant.product.pillowImage.src,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Money } from '@/utils/formatter'
export type CartEntry = {
offerId: string
title: string
price: Money
cost: ProductOfferFragment['cost']
startDate?: Date | null
pillow: { src: string; alt?: string }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { FormElement } from '@/components/ProductPage/PurchaseForm/PurchaseForm.
import * as TierLevelRadioGroup from '@/components/TierSelector/TierLevelRadioGroup'
import * as TierSelector from '@/components/TierSelector/TierSelector'
import { Money, ProductOfferFragment } from '@/services/apollo/generated'
import { Features } from '@/utils/Features'
import { useCurrentLocale } from '@/utils/l10n/useCurrentLocale'
import { PageLink } from '@/utils/PageLink'
import { useFormatter } from '@/utils/useFormatter'
Expand Down Expand Up @@ -35,9 +36,10 @@ export const DeductibleSelector = ({ offers, selectedOffer, onValueChange }: Pro

offers.forEach((offer) => {
if (offer.deductible) {
const price = Features.enabled('DISCOUNTS') ? offer.cost.net : offer.price
levels.push({
id: offer.id,
price: offer.cost.net,
price,
title: offer.deductible.displayName,
description: offer.deductible.tagline,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Text } from 'ui'
import * as TierLevelRadioGroup from '@/components/TierSelector/TierLevelRadioGroup'
import * as TierSelector from '@/components/TierSelector/TierSelector'
import { ProductOfferFragment } from '@/services/apollo/generated'
import { Features } from '@/utils/Features'
import { useFormatter } from '@/utils/useFormatter'

type Props = {
Expand All @@ -26,15 +27,18 @@ export const ProductTierSelector = ({ offers, selectedOffer, onValueChange }: Pr

<TierSelector.Content>
<TierLevelRadioGroup.Root value={selectedOffer.id} onValueChange={onValueChange}>
{offers.map((offer) => (
<TierLevelRadioGroup.Item
key={offer.id}
value={offer.id}
title={offer.variant.displayName}
price={formatter.monthlyPrice(offer.cost.net)}
description={getVariantDescription(offer.variant.typeOfContract)}
/>
))}
{offers.map((offer) => {
const price = Features.enabled('DISCOUNTS') ? offer.cost.net : offer.price
return (
<TierLevelRadioGroup.Item
key={offer.id}
value={offer.id}
title={offer.variant.displayName}
price={formatter.monthlyPrice(price)}
description={getVariantDescription(offer.variant.typeOfContract)}
/>
)
})}
</TierLevelRadioGroup.Root>
</TierSelector.Content>
</TierSelector.Root>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { useShopSession } from '@/services/shopSession/ShopSessionContext'
import { TrackingContextKey } from '@/services/Tracking/Tracking'
import { useTracking } from '@/services/Tracking/useTracking'
import { sendDialogEvent } from '@/utils/dialogEvent'
import { Features } from '@/utils/Features'
import { useBreakpoint } from '@/utils/useBreakpoint/useBreakpoint'
import { useFormatter } from '@/utils/useFormatter'
import { ScrollPast } from '../ScrollPast/ScrollPast'
Expand Down Expand Up @@ -197,9 +198,10 @@ export const PurchaseForm = () => {
return await router.push(nextUrl)
}

const price = Features.enabled('DISCOUNTS') ? item.cost.net : item.price
notifyProductAdded({
name: productData.displayNameFull,
price: formatter.monthlyPrice(item.cost.net),
price: formatter.monthlyPrice(price),
pillowSrc: productData.pillowImage.src,
description:
!item.cancellation.requested ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { ProductOfferFragment } from '@/services/apollo/generated'
import { Features } from '@/utils/Features'

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
return 0
})
return [...offers].sort(Features.enabled('DISCOUNTS') ? sortByCost : sortByPrice)
}

const sortByCost = (a: ProductOfferFragment, b: ProductOfferFragment) => {
if (a.cost.net.amount < b.cost.net.amount) return -1
if (a.cost.net.amount > b.cost.net.amount) return 1
return 0
}

const sortByPrice = (a: ProductOfferFragment, b: ProductOfferFragment) => {
if (a.price.amount < b.price.amount) return -1
if (a.price.amount > b.price.amount) return 1
return 0
}
4 changes: 4 additions & 0 deletions apps/store/src/graphql/OfferRecommendationFragment.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ fragment OfferRecommendation on ProductOffer {
displayNameFull
}
}
price {
amount
currencyCode
}
cost {
gross {
amount
Expand Down
4 changes: 4 additions & 0 deletions apps/store/src/graphql/ProductOfferFragment.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ fragment ProductOffer on ProductOffer {
url
}
}
price {
amount
currencyCode
}
cost {
gross {
amount
Expand Down
30 changes: 19 additions & 11 deletions apps/store/src/services/Tracking/Tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '@/services/gtm'
import { PriceIntent } from '@/services/priceIntent/priceIntent.types'
import { ShopSession } from '@/services/shopSession/ShopSession.types'
import { Features } from '@/utils/Features'
import { getAdtractionProductCategories } from './adtraction'

type TrackingContext = Partial<Record<TrackingContextKey, unknown>>
Expand All @@ -20,6 +21,7 @@ type TrackingProductData = {
}

type TrackingOffer = {
price: ProductOfferFragment['price']
cost: ProductOfferFragment['cost']
variant: {
typeOfContract: ProductOfferFragment['variant']['typeOfContract']
Expand Down Expand Up @@ -138,10 +140,11 @@ export class Tracking {
public async reportOfferCreated(offer: ProductOfferFragment) {
const event = TrackingEvent.OfferCreated
const userData = await getLegacyUserData(this.context)
const price = Features.enabled('DISCOUNTS') ? offer.cost.gross : offer.price
const eventData = {
offerData: {
insurance_price: offer.cost.gross.amount,
currency: offer.cost.gross.currencyCode as string,
insurance_price: price.amount,
currency: price.currencyCode as string,

insurance_type: offer.variant.typeOfContract,
flow_type: offer.variant.product.name,
Expand Down Expand Up @@ -301,16 +304,17 @@ const offerToEcommerceEvent = ({
context,
source,
}: OfferToEcommerseEventParams): EcommerceEvent => {
const price = Features.enabled('DISCOUNTS') ? offer.cost.gross : offer.price
return {
event,
ecommerce: {
value: offer.cost.gross.amount,
currency: offer.cost.gross.currencyCode,
value: price.amount,
currency: price.currencyCode,
items: [
{
item_id: offer.variant.product.id,
item_name: offer.variant.product.displayNameFull,
price: offer.cost.gross.amount,
price: price.amount,
item_variant: offer.variant.typeOfContract,
...(source && { item_list_id: source }),
},
Expand Down Expand Up @@ -338,12 +342,16 @@ const cartToEcommerceEvent = (
ecommerce: {
value: cart.cost.net.amount,
currency: cart.cost.net.currencyCode,
items: cart.entries.map((entry) => ({
item_id: entry.variant.product.id,
item_name: entry.variant.product.displayNameFull,
price: entry.cost.gross.amount,
variant: entry.variant.typeOfContract,
})),
items: cart.entries.map((entry) => {
const price = Features.enabled('DISCOUNTS') ? entry.cost.gross : entry.price

return {
item_id: entry.variant.product.id,
item_name: entry.variant.product.displayNameFull,
price: price.amount,
variant: entry.variant.typeOfContract,
}
}),
},
shopSession: {
id: context[TrackingContextKey.ShopSessionId] as string,
Expand Down

0 comments on commit e2c978c

Please sign in to comment.