From eb3c9dcd2f684ddf9ef4af23c6fa7bfd9bf973c6 Mon Sep 17 00:00:00 2001 From: Subhranshu Das Date: Mon, 9 May 2022 16:26:04 +0530 Subject: [PATCH 1/4] feat: EPNS embed sidebar notifications on Home page --- src/components/EPNSBellIcon/EPNSBellIcon.js | 31 ++ src/hooks/useEPNSEmbed.js | 30 ++ src/hooks/useEPNSEmbed.spec.js | 41 ++ src/routes/Home.js | 14 + src/routes/Home.spec.js | 23 +- src/utils/embedsdk.esm.js | 492 ++++++++++++++++++++ 6 files changed, 629 insertions(+), 2 deletions(-) create mode 100644 src/components/EPNSBellIcon/EPNSBellIcon.js create mode 100644 src/hooks/useEPNSEmbed.js create mode 100644 src/hooks/useEPNSEmbed.spec.js create mode 100644 src/utils/embedsdk.esm.js diff --git a/src/components/EPNSBellIcon/EPNSBellIcon.js b/src/components/EPNSBellIcon/EPNSBellIcon.js new file mode 100644 index 000000000..2fd31b7ab --- /dev/null +++ b/src/components/EPNSBellIcon/EPNSBellIcon.js @@ -0,0 +1,31 @@ +import React from 'react' +import styled from '@emotion/styled/macro' + +const Icon = styled('div')` + width: 32px; + height: 32px; + margin-top: -7px; + cursor: pointer; +` +const defaultSize = '32px' + +const Image = styled('img')` + width: ${props => props.size || defaultSize}; + height: ${props => props.size || defaultSize}; +` + +export default function({ + size, // px + ...otherProps +}) { + return ( + + EPNS Bell Icon + + ) +} diff --git a/src/hooks/useEPNSEmbed.js b/src/hooks/useEPNSEmbed.js new file mode 100644 index 000000000..b000118d6 --- /dev/null +++ b/src/hooks/useEPNSEmbed.js @@ -0,0 +1,30 @@ +import { useEffect } from 'react' +/** intend to replace this with npm package */ +import EmbedSDK from '../utils/embedsdk.esm' + +export const useEPNSEmbed = ({ user, targetID, appName }) => { + useEffect(() => { + if (user && targetID && appName) { + if (typeof EmbedSDK.init === 'function') { + EmbedSDK.init({ + targetID: targetID, // MANDATORY + appName: appName, // MANDATORY + user: user, // MANDATORY + headerText: 'EPNS Notifications', + viewOptions: { + type: 'sidebar', + showUnreadIndicator: true, + unreadIndicatorColor: '#cc1919', + unreadIndicatorPosition: 'top-right' + } + }) + } + } + + return () => { + if (typeof EmbedSDK.cleanup === 'function') { + EmbedSDK.cleanup() + } + } + }, [user, targetID, appName]) +} diff --git a/src/hooks/useEPNSEmbed.spec.js b/src/hooks/useEPNSEmbed.spec.js new file mode 100644 index 000000000..911a55607 --- /dev/null +++ b/src/hooks/useEPNSEmbed.spec.js @@ -0,0 +1,41 @@ +/** intend to replace this with npm package */ +jest.mock('../utils/embedsdk.esm', () => ({ + __esModule: true, + default: { + init: jest.fn(), + cleanup: jest.fn() + } +})) + +import EmbedSDK from '../utils/embedsdk.esm' + +import { renderHook } from '@testing-library/react-hooks' +import { useEPNSEmbed } from './useEPNSEmbed' + +describe('useEPNSEmbed', () => { + it('should not call init() if all mandatory params are not passed', () => { + const { rerender } = renderHook(() => useEPNSEmbed({})) + + rerender() + expect(EmbedSDK.init).toBeCalledTimes(0) + }) + + it('should not call init() if all mandatory params are not passed', () => { + const initOptions = { + user: '0xaddress1', + targetID: 'trigger-id', + appName: 'ens' + } + + const { rerender } = renderHook(() => useEPNSEmbed(initOptions)) + + rerender() + expect(EmbedSDK.init).toBeCalledTimes(1) + + const calledWithArgs = EmbedSDK.init.mock.calls[0][0] + + expect(calledWithArgs.user).toEqual(initOptions.user) + expect(calledWithArgs.targetID).toEqual(initOptions.targetID) + expect(calledWithArgs.appName).toEqual(initOptions.appName) + }) +}) diff --git a/src/routes/Home.js b/src/routes/Home.js index 3f1c8a4ac..ecebf52c1 100644 --- a/src/routes/Home.js +++ b/src/routes/Home.js @@ -20,6 +20,8 @@ import { MainPageBannerContainer, DAOBannerContent } from '../components/Banner/DAOBanner' +import EPNSBellIcon from '../components/EPNSBellIcon/EPNSBellIcon' +import { useEPNSEmbed } from 'hooks/useEPNSEmbed' const HeroTop = styled('div')` display: grid; @@ -297,6 +299,9 @@ const animation = { } } +export const epnsTriggerId = 'epns-sdk-trigger-id' +export const epnsTriggerAppName = 'ensApp' + export default ({ match }) => { const { url } = match const { t } = useTranslation() @@ -311,6 +316,12 @@ export default ({ match }) => { variables: { address: accounts?.[0] } }) + useEPNSEmbed({ + user: accounts?.[0], + targetID: epnsTriggerId, + appName: epnsTriggerAppName + }) + return ( @@ -330,6 +341,9 @@ export default ({ match }) => { )}