diff --git a/src/components/optinvideo/OptInVideo.js b/src/components/optinvideo/OptInVideo.js new file mode 100644 index 00000000..a20cc8fe --- /dev/null +++ b/src/components/optinvideo/OptInVideo.js @@ -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) + + // User is not loaded yet. + if (!user) { + return null + } + + // Not a browser, so we can't render the widget. + if (typeof window === 'undefined') { + return null + } + + // + // Called when widget is closed by user. + // + const onClose = () => { + setShowModal(false) + gtag('event', 'optin-video_modal_close') + } + + // + // Function to handle received messages from the iframe + // + function receiveMessage(event) { + // 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 + } + + // Do we want to show the notification? + if (event.data.show) { + setShowModal(true) + } else { + setShowModal(false) + } + } + + // Set up the event listener + // eslint-disable-next-line no-undef + window.addEventListener( + 'message', + (event) => { + receiveMessage(event) + }, + false + ) + + return ( + <> + +
+ + + + +
+