@@ -7,7 +7,8 @@ import { useProductsAllQuery } from '@/api/mutations/product_mutations'
77import { Section } from '@/components/layout/Section'
88import { tw , tx } from '@twind/core'
99import type {
10- ProductPlanTranslation } from '@/api/dataclasses/product'
10+ ProductPlanTranslation
11+ } from '@/api/dataclasses/product'
1112import {
1213 defaultProductPlanTranslation
1314} from '@/api/dataclasses/product'
@@ -21,12 +22,22 @@ import { withCart } from '@/hocs/withCart'
2122import { LoadingAndErrorComponent } from '@helpwave/common/components/LoadingAndErrorComponent'
2223import { useRouter } from 'next/router'
2324import { Modal } from '@helpwave/common/components/modals/Modal'
24- import { useState } from 'react'
25+ import { useEffect , useState } from 'react'
2526import { Input } from '@helpwave/common/components/user-input/Input'
2627import { VoucherAPI } from '@/api/services/voucher'
2728import type { Voucher } from '@/api/dataclasses/voucher'
2829import { Chip } from '@helpwave/common/components/ChipList'
2930import { useCustomerProductsCalculateQuery } from '@/api/mutations/customer_product_mutations'
31+ import { ProductAPI } from '@/api/services/product'
32+ import { LoadingAnimation } from '@helpwave/common/components/LoadingAnimation'
33+
34+ type ReferralStatus = 'loading' | 'error'
35+ type ReferralData = {
36+ product : string ,
37+ plan : string ,
38+ voucher ?: string ,
39+ }
40+
3041
3142type CartOverviewTranslation = {
3243 removeFromCart : string ,
@@ -43,6 +54,8 @@ type CartOverviewTranslation = {
4354 redeemVoucherFor : ( name : string ) => string ,
4455 code : string ,
4556 invalidCode : string ,
57+ referral : string ,
58+ referralError : string ,
4659} & ProductPlanTranslation
4760
4861const defaultCartOverviewTranslations : Record < Languages , CartOverviewTranslation > = {
@@ -62,6 +75,8 @@ const defaultCartOverviewTranslations: Record<Languages, CartOverviewTranslation
6275 redeemVoucherFor : ( name : string ) => `Redeem Voucher for ${ name } ` ,
6376 code : 'Code' ,
6477 invalidCode : 'The provided Code is not valid (for this product), please try a different one.' ,
78+ referral : 'Referral' ,
79+ referralError : 'The Referral could not be processed.' ,
6580 } ,
6681 de : {
6782 ...defaultProductPlanTranslation . de ,
@@ -79,30 +94,103 @@ const defaultCartOverviewTranslations: Record<Languages, CartOverviewTranslation
7994 redeemVoucherFor : ( name : string ) => `Gutschein einlösen für ${ name } ` ,
8095 code : 'Code' ,
8196 invalidCode : 'Der eingegebenen Gutschein-Code ist nicht gültig (für dieses Produkt), versuchen Sie einen anderen.' ,
97+ referral : 'Überweisung' , // TODO fix translation
98+ referralError : 'Die Überweisung hat nicht funktioniert.' ,
8299 }
83100}
84101
85102
86103const CartOverview : NextPage = ( ) => {
87104 const translation = useTranslation ( defaultCartOverviewTranslations )
88105 const router = useRouter ( )
106+ const [ hasUsedReferral , setHasUsedReferral ] = useState < boolean > ( false )
89107 const { authHeader } = useAuth ( )
90- const { cart, removeItem, updateItem } = useCart ( )
108+ const [ referralStatus , setReferralStatus ] = useState < ReferralStatus > ( )
109+ const { cart, removeItem, updateItem, addItem, isLoading : cartIsLoading } = useCart ( )
91110 const [ productVoucherModalId , setProductVoucherModalId ] = useState < string > ( )
92111 const [ voucherCode , setVoucherCode ] = useState < string > ( '' )
93112 const [ redeemResponse , setRedeemResponse ] = useState < string > ( )
94113 const { data : products , isError : productsError , isLoading : productsLoading } = useProductsAllQuery ( )
95- const { data : prices , isError : pricesError , isLoading : pricesLoading } = useCustomerProductsCalculateQuery ( cart . map ( item => ( {
114+ const {
115+ data : prices ,
116+ isError : pricesError ,
117+ isLoading : pricesLoading ,
118+ isRefetching,
119+ refetch,
120+ } = useCustomerProductsCalculateQuery ( cart . map ( item => ( {
96121 productUuid : item . id ,
97122 productPlanUuid : item . plan . uuid ,
98123 voucherUuid : item . voucher ?. uuid
99124 } ) ) )
100125
126+ useEffect ( ( ) => {
127+ refetch ( ) . catch ( console . error )
128+ } , [ cart ] )
129+
130+ useEffect ( ( ) => {
131+ if ( ! router . isReady || cartIsLoading || hasUsedReferral ) return
132+
133+ const referralParam = router . query [ 'referral' ]
134+
135+ if ( ! referralParam || typeof referralParam !== 'string' ) return
136+
137+ try {
138+ const decoded = atob ( referralParam )
139+ const parsed : ReferralData = JSON . parse ( decoded )
140+
141+ const check = async ( ) => {
142+ const products = await ProductAPI . getAvailable ( authHeader )
143+ const product = products . find ( value => value . uuid === parsed . product )
144+ const plan = product ?. plan . find ( value => value . uuid === parsed . plan )
145+
146+ // TODO try to parse voucher
147+
148+ if ( ! product || ! plan ) {
149+ setReferralStatus ( 'error' )
150+ setHasUsedReferral ( true )
151+ return
152+ }
153+
154+ if ( cart . find ( value => value . id === product ?. uuid ) ) {
155+ // TODO maybe show an additional dialog here
156+ updateItem ( { id : product ?. uuid , plan : plan , quantity : 1 } )
157+ } else {
158+ addItem ( { id : product ?. uuid , plan : plan , quantity : 1 } )
159+ }
160+ }
161+
162+ check ( ) . catch ( ( reason ) => {
163+ console . error ( reason )
164+ setReferralStatus ( 'error' )
165+ setHasUsedReferral ( true )
166+ } ) . then ( ( ) => {
167+ setReferralStatus ( undefined )
168+ setHasUsedReferral ( true )
169+ } )
170+ } catch ( err ) {
171+ console . error ( err )
172+ setReferralStatus ( 'error' )
173+ setHasUsedReferral ( true )
174+ }
175+ } , [ router , cart ] )
176+
101177 const isError = pricesError || productsError
102- const isLoading = pricesLoading || productsLoading || Object . keys ( prices ?. products ?? { } ) . length === 0
178+ const isLoading = pricesLoading || productsLoading || isRefetching
103179
104180 return (
105181 < Page pageTitle = { titleWrapper ( translation . overview ) } >
182+ < Modal
183+ id = "referral-modal"
184+ isOpen = { ! ! referralStatus }
185+ onBackgroundClick = { referralStatus === 'error' ? ( ) => setReferralStatus ( undefined ) : undefined }
186+ onCloseClick = { referralStatus === 'error' ? ( ) => setReferralStatus ( undefined ) : undefined }
187+ titleText = { translation . referral }
188+ modalClassName = { tw ( 'gap-y-2' ) }
189+ >
190+ { referralStatus === 'error' ? (
191+ < span className = { tw ( 'text-hw-negative-500' ) } > { translation . referralError } </ span >
192+ ) : ( < LoadingAnimation /> ) }
193+ </ Modal >
106194 < Modal
107195 id = "voucher-modal"
108196 isOpen = { ! ! productVoucherModalId }
@@ -153,7 +241,7 @@ const CartOverview: NextPage = () => {
153241 const plan = product ?. plan . find ( value => value . uuid === cartItem . plan . uuid )
154242 const voucher = cartItem . voucher
155243
156- if ( ! product || ! plan ) {
244+ if ( ! product || ! plan ) {
157245 return [ ]
158246 }
159247 const priceResult = prices . products [ product . uuid ] !
0 commit comments