forked from paypal/paypal-messaging-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBodyContent.jsx
More file actions
174 lines (156 loc) · 6.77 KB
/
BodyContent.jsx
File metadata and controls
174 lines (156 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/** @jsx h */
import { h, Fragment } from 'preact';
import { useEffect, useState, useMemo } from 'preact/hooks';
import {
useContent,
useServerData,
useProductMeta,
useXProps,
useScroll,
useDidUpdateEffect,
useTransitionState,
isLander
} from '../lib';
import Header from './Header';
import { LongTerm, ShortTerm, NoInterest, ProductList, PayIn1 } from './views';
import CheckoutHeader from './CheckoutHeader';
const VIEW_IDS = {
// TODO: add an error view in case we receive an invalid view?
PAYPAL_CREDIT_NO_INTEREST: 'PAYPAL_CREDIT_NO_INTEREST',
PAY_LATER_LONG_TERM: 'PAY_LATER_LONG_TERM',
PAY_LATER_PAY_IN_1: 'PAY_LATER_PAY_IN_1',
PAY_LATER_SHORT_TERM: 'PAY_LATER_SHORT_TERM',
PRODUCT_LIST: 'PRODUCT_LIST'
};
const BodyContent = () => {
const { views } = useServerData();
const { offer, features } = useXProps();
const { scrollTo } = useScroll();
const [transitionState] = useTransitionState();
const primaryViewName = useMemo(() => {
if (offer) {
const viewName = views.find(view => view.meta.product === offer)?.meta.product;
if (viewName) {
return viewName;
}
}
let defaultViewName;
const productViews = views.filter(view => view?.meta?.product !== VIEW_IDS.PRODUCT_LIST);
const hasProductList = views.find(view => view?.meta?.product === VIEW_IDS.PRODUCT_LIST);
if (productViews?.length === 1) {
defaultViewName = productViews[0]?.meta?.product;
} else if (productViews?.length > 1 && hasProductList) {
defaultViewName = VIEW_IDS.PRODUCT_LIST;
} else if (productViews?.length > 1 && !hasProductList) {
defaultViewName = productViews[0]?.meta?.product;
}
return defaultViewName;
}, [offer, ...views.map(view => view?.meta?.product)]);
const [viewName, setViewName] = useState(primaryViewName);
const content = useContent(viewName);
const productMeta = useProductMeta(viewName);
const { headline, subheadline, qualifyingSubheadline = '', closeButtonLabel, cta } = content;
const isQualifying = productMeta?.qualifying;
const useV4Design = productMeta?.useV4Design === 'true';
const useV5Design = productMeta?.useV5Design === 'true';
const use5Dot1Design = productMeta?.['v5.1'];
const useNewCheckoutDesign = features?.includes('new-checkout-design') ? 'true' : 'false';
// add v4Design or v5Design class to root html to update lander specific styles to v4 or v5 respectively
const documentClassName = document.documentElement.className;
if (useV4Design && isLander) {
document.documentElement.className = `${documentClassName} v4Design`;
}
if (useV5Design && isLander) {
document.documentElement.className = `${documentClassName} v5Design`;
}
const isPreapproved = productMeta?.preapproved;
const preapprovalHeadline = content?.preapproval?.preapprovalHeadline;
const preapprovalSubHeadline = content?.preapproval?.preapprovalSubHeadline;
const preapprovalLabel = content?.preapproval?.preapprovalLabel;
const openProductList = () => setViewName(VIEW_IDS.PRODUCT_LIST);
useDidUpdateEffect(() => {
scrollTo(0); // Reset scroll position to top when view changes
if (transitionState === 'OPEN') {
window.document.querySelector('#close-btn')?.focus();
}
}, [viewName]);
useDidUpdateEffect(() => {
// Update view when offer param dynamically changed
setViewName(primaryViewName ?? viewName);
}, [offer]);
useEffect(() => {
// Reset back to the primary view after closing the modal
if (transitionState === 'CLOSED') {
setViewName(primaryViewName);
}
}, [transitionState]);
// Add views to viewComponents object where the keys are the product name and the values are the view component
const viewComponents = {
[VIEW_IDS.PAYPAL_CREDIT_NO_INTEREST]: <NoInterest content={content} openProductList={openProductList} />,
[VIEW_IDS.PAY_LATER_LONG_TERM]: (
<LongTerm
content={content}
productMeta={productMeta}
useNewCheckoutDesign={useNewCheckoutDesign}
openProductList={openProductList}
/>
),
[VIEW_IDS.PAY_LATER_PAY_IN_1]: (
<PayIn1 productMeta={productMeta} content={content} openProductList={openProductList} />
),
[VIEW_IDS.PAY_LATER_SHORT_TERM]: (
<ShortTerm
content={content}
productMeta={productMeta}
useNewCheckoutDesign={useNewCheckoutDesign}
openProductList={openProductList}
/>
),
[VIEW_IDS.PRODUCT_LIST]: <ProductList content={content} useV5Design={useV5Design} setViewName={setViewName} />
};
// IMPORTANT: These elements cannot be nested inside of other elements.
// They are using very precise CSS position sticky rules that require this
// specific adjacent DOM structure
return (
<Fragment>
{typeof cta !== 'undefined' && features?.includes('new-checkout-design') ? (
<CheckoutHeader
headline={headline}
subheadline={subheadline}
isQualifying={isQualifying ?? 'false'}
qualifyingSubheadline={qualifyingSubheadline}
closeButtonLabel={closeButtonLabel}
viewName={viewName}
preapprovalHeadline={preapprovalHeadline}
preapprovalSubHeadline={preapprovalSubHeadline}
preapprovalLabel={preapprovalLabel}
// toggles preapproval content
isPreapproved={isPreapproved ?? 'false'}
/>
) : (
<Header
logo="logo"
headline={headline}
subheadline={subheadline}
isQualifying={isQualifying ?? 'false'}
qualifyingSubheadline={qualifyingSubheadline}
closeButtonLabel={closeButtonLabel}
viewName={viewName}
useV4Design={useV4Design}
useV5Design={useV5Design}
use5Dot1Design={use5Dot1Design}
/>
)}
<div
className={`content__container ${useV4Design ? 'v4Design' : ''} ${useV5Design ? 'v5Design' : ''} ${
useNewCheckoutDesign === 'true' ? 'checkout' : ''
}`}
>
<main className="main">
<div className="content__body">{viewComponents[viewName]}</div>
</main>
</div>
</Fragment>
);
};
export default BodyContent;