Skip to content

Instantly share code, notes, and snippets.

@siminino
Last active April 20, 2023 13:06
Show Gist options
  • Save siminino/8599bc8d388cf1259b8b0ad3ee0cc6d8 to your computer and use it in GitHub Desktop.
Save siminino/8599bc8d388cf1259b8b0ad3ee0cc6d8 to your computer and use it in GitHub Desktop.
import { memo } from 'react'
import PropTypes from 'prop-types'
import renderInAppMessage from './render-in-app-message.js'
import sections from './sections.js'
import useFetchInAppMessages from './hooks'
const InAppMessages = ({ section, onMessageClose, onMessageOpen, displayMessage, campaign }) => {
const { message, onClose, refetch } = useFetchInAppMessages(section, campaign)
const hasUnpublishedChanges = useSelector(hasUnpublishedChangesSelector)
// Will refetch when `hasUnpublishedChanges` changes to `true`
useEffect(() => {
if (hasUnpublishedChanges) {
refetch()
}
}, [refetch, hasUnpublishedChanges])
// When the app acomplish the rules to display the message, provide `displayMessage: true`
if (!message || !displayMessage) {
return null
}
const onCloseHandler = () => {
onClose()
// Call SharePopup
onMessageClose && onMessageClose()
}
const onOpenHandler = () => {
// Hide SharePopup
onMessageOpen && onMessageOpen(message)
}
// support render for embed typeforms type of message
return renderInAppMessage(message, onCloseHandler, onOpenHandler)
}
InAppMessages.propTypes = {
section: PropTypes.string.isRequired,
onMessageClose: PropTypes.func,
onMessageOpen: PropTypes.func,
}
module.exports = {
InAppMessages: memo(InAppMessages),
sections,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment