Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Launched optin video #582

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions src/components/optinvideo/OptInVideo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
'use client'

import gtag from 'ga-gtag'
import React, { useState } from 'react'
import PropTypes from 'prop-types'
import Modal from '@material-ui/core/Modal'
import IconButton from '@material-ui/core/IconButton'
import CloseIcon from '@material-ui/icons/Close'

const isBrowser = typeof window !== 'undefined'

const sParams = {
NotificationOverride: null,
}

if (isBrowser) {
// eslint-disable-next-line no-undef
const p = new Proxy(new URLSearchParams(window.location.search), {
get: (searchParams, prop) => searchParams.get(prop),
})

sParams.NotificationOverride = p['opt-in-override'] || ''
}

// Example Overrides: ?opt-in-override={any user id}
const OptInVideo = ({ user }) => {
const [showModal, setShowModal] = useState(false)

Check warning on line 27 in src/components/optinvideo/OptInVideo.js

View check run for this annotation

Codecov / codecov/patch

src/components/optinvideo/OptInVideo.js#L27

Added line #L27 was not covered by tests

// User is not loaded yet.
if (!user) {
return null

Check warning on line 31 in src/components/optinvideo/OptInVideo.js

View check run for this annotation

Codecov / codecov/patch

src/components/optinvideo/OptInVideo.js#L31

Added line #L31 was not covered by tests
}

// Not a browser, so we can't render the widget.
if (typeof window === 'undefined') {
return null

Check warning on line 36 in src/components/optinvideo/OptInVideo.js

View check run for this annotation

Codecov / codecov/patch

src/components/optinvideo/OptInVideo.js#L36

Added line #L36 was not covered by tests
}

//
// Called when widget is closed by user.
//
const onClose = () => {
setShowModal(false)
gtag('event', 'optin-video_modal_close')

Check warning on line 44 in src/components/optinvideo/OptInVideo.js

View check run for this annotation

Codecov / codecov/patch

src/components/optinvideo/OptInVideo.js#L42-L44

Added lines #L42 - L44 were not covered by tests
}

//
// Function to handle received messages from the iframe
//
function receiveMessage(event) {

Check warning on line 50 in src/components/optinvideo/OptInVideo.js

View check run for this annotation

Codecov / codecov/patch

src/components/optinvideo/OptInVideo.js#L50

Added line #L50 was not covered by tests
// TODO(spicer): Add origin check for added security
// if (event.origin !== 'http://127.0.0.1:9000') return

// Check if the message is for us. If not, ignore it.
if (typeof event.data.show === 'undefined') return

// Check if the message is for us. If not, ignore it.
if (event.data.slot !== 'opt-in-video') {
return

Check warning on line 59 in src/components/optinvideo/OptInVideo.js

View check run for this annotation

Codecov / codecov/patch

src/components/optinvideo/OptInVideo.js#L59

Added line #L59 was not covered by tests
}

// Do we want to show the notification?
if (event.data.show) {
setShowModal(true)
} else {
setShowModal(false)

Check warning on line 66 in src/components/optinvideo/OptInVideo.js

View check run for this annotation

Codecov / codecov/patch

src/components/optinvideo/OptInVideo.js#L64-L66

Added lines #L64 - L66 were not covered by tests
}
}

// Set up the event listener
// eslint-disable-next-line no-undef
window.addEventListener(

Check warning on line 72 in src/components/optinvideo/OptInVideo.js

View check run for this annotation

Codecov / codecov/patch

src/components/optinvideo/OptInVideo.js#L72

Added line #L72 was not covered by tests
'message',
(event) => {
receiveMessage(event)

Check warning on line 75 in src/components/optinvideo/OptInVideo.js

View check run for this annotation

Codecov / codecov/patch

src/components/optinvideo/OptInVideo.js#L74-L75

Added lines #L74 - L75 were not covered by tests
},
false
)

return (

Check warning on line 80 in src/components/optinvideo/OptInVideo.js

View check run for this annotation

Codecov / codecov/patch

src/components/optinvideo/OptInVideo.js#L80

Added line #L80 was not covered by tests
<>
<Modal
id="optin-video-modal"
open={showModal}
style={{
height: 650,
marginTop: 'auto',
marginBottom: 'auto',
marginLeft: 'auto',
marginRight: 'auto',
maxWidth: 1000,
position: 'absolute',
backgroundColor: '#fff',
zIndex: 100000000,
overflow: 'hidden',
}}
>
<div style={{ height: '100%' }}>
<IconButton
onClick={onClose}
style={{ position: 'absolute', right: 25, top: 5 }}
>
<CloseIcon sx={{ color: '#fff', width: 28, height: 28 }} />
</IconButton>

<div
style={{
height: '100%',
width: '100%',
padding: 0,
backgroundColor: 'white',
display: 'flex',
flexFlow: 'column',
}}
>
<iframe
id="optin-video-modal-iframe"
src={`${process.env.NEXT_PUBLIC_API_ENDPOINT}/v5/iframe/opt-in-video?user_id=${user.userId}&override=${sParams.NotificationOverride}`}
title="optin-video-modal-iframe"
style={{
marginTop: '10px',
marginBottom: '10px',
border: 'none',
}}
width="100%"
height="100%"
frameBorder="0"
/>
</div>
</div>
</Modal>
</>
)
}

OptInVideo.displayName = 'OptInVideoComponent'

OptInVideo.propTypes = {
user: PropTypes.shape({
userId: PropTypes.string,
}).isRequired,
}

OptInVideo.defaultProps = {}

export default OptInVideo
36 changes: 36 additions & 0 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
import GroupImpactContainer from 'src/components/groupImpactComponents/GroupImpactContainer'
import ShopFullPage from 'src/components/promos/ShopFullPage'
import SearchFullPage from 'src/components/promos/SearchFullPage'
import OptInVideo from 'src/components/optinvideo/OptInVideo'
import Notification from 'src/components/notification/Notification'

// import NotificationOld from 'src/components/Notification'
Expand All @@ -108,6 +109,8 @@
import Modal from '@material-ui/core/Modal'
import { Box } from '@material-ui/core'

const isBrowser = typeof window !== 'undefined'

const getNotifDismissKey = (code) => `${NOTIF_DISMISS_PREFIX}.${code}`

const useStyles = makeStyles((theme) => ({
Expand Down Expand Up @@ -770,6 +773,19 @@
setShowLeaderboardFunc(true)
}

const sParams = {
OptInOverride: null,
}

if (isBrowser) {
// eslint-disable-next-line no-undef
const p = new Proxy(new URLSearchParams(window.location.search), {
get: (searchParams, prop) => searchParams.get(prop),
})

sParams.OptInOverride = p['opt-in-override'] || ''
}

return (
<>
<Head>
Expand Down Expand Up @@ -860,6 +876,26 @@
</a>
)}

{user && sParams.OptInOverride && (
<>

Check warning on line 880 in src/pages/index.js

View check run for this annotation

Codecov / codecov/patch

src/pages/index.js#L880

Added line #L880 was not covered by tests
<iframe
title="opt-in-video"
frameBorder="0"
allowtransparency="true"
src={`${process.env.NEXT_PUBLIC_API_ENDPOINT}/v5/iframe/opt-in-video?show=icon&user_id=${user.userId}&override=${sParams.OptInOverride}`}
style={{
height: 40,
width: 40,
border: 'none',
marginRight: 10,
backgroundColor: 'transparent',
}}
/>

<OptInVideo user={user} />
</>
)}

{user && (
<IconButton
style={{
Expand Down
Loading