-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.jsx
94 lines (83 loc) · 2.39 KB
/
App.jsx
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
import styled from '@emotion/styled';
import React, { useState, useEffect } from 'react';
import AmplifyAuth from './components/amplify-auth';
import Header from './components/header';
import EditProducts from './components/edit-products';
import './App.css';
import { useAuthenticator } from '@aws-amplify/ui-react';
import { buildCart } from './helpers/cart';
import { PRODUCTS, STORE_ID } from './data';
const Overlay = styled.div({
position: 'absolute',
top: '100px',
width: '100vw',
height: '100vh',
background: 'white',
zIndex: 300,
});
const App = () => {
const { user } = useAuthenticator((context) => [context.user]);
const [isIframeLoaded, setIsIframeLoaded] = useState(false);
const [isGuest, setIsGuest] = useState(false);
const [showEditCart, setShowEditCart] = useState(false);
const [cartData, setCartData] = useState({
products: PRODUCTS,
storeId: STORE_ID,
});
useEffect(() => {
window.addEventListener(
'message',
(event) => {
var payload = event.data && event.data.payload;
var messageType = event.data && event.data.messageType;
if (
messageType === 'loadingEvent' &&
payload.name === 'headlessAppLoaded'
) {
setIsIframeLoaded(true);
}
},
false
);
// This url can be changed to point to different environments.
document.getElementById('jane-frame-script').src =
'https://staging-api.nonprod-iheartjane.com/v1/headless/embed.js';
}, []);
useEffect(() => {
if (isGuest && isIframeLoaded) {
buildCart(null, cartData);
}
}, [isGuest, isIframeLoaded]);
useEffect(() => {
if (user && isIframeLoaded) {
buildCart(user, cartData);
}
}, [user, isIframeLoaded]);
const onEditCart = (storeId, products) => {
setShowEditCart(false);
setCartData({
storeId,
products,
});
buildCart(user, { storeId, products });
};
const showCart = isGuest || user;
return (
<div className="App">
<Header
setIsGuest={setIsGuest}
setShowEditCart={setShowEditCart}
showCart={showCart}
/>
{!showCart && <Overlay />}
<EditProducts
showEditCart={showEditCart}
setShowEditCart={setShowEditCart}
cartData={cartData}
onEditCart={onEditCart}
/>
{!isGuest && <AmplifyAuth setIsGuest={setIsGuest} />}
</div>
);
};
export default App;