-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
285 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
import { createRoot } from 'react-dom/client'; | ||
import { SubscribeContext, SigninButton } from '@poool/react-subscribe'; | ||
|
||
const App = () => null; | ||
const App = () => ( | ||
<SubscribeContext | ||
config={{ debug: true }} | ||
appId="155PF-L7Q6Q-EB2GG-04TF8" | ||
> | ||
<SigninButton /> | ||
</SubscribeContext> | ||
); | ||
|
||
createRoot(document.getElementById('app')).render(<App />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
useRef, | ||
useMemo, | ||
useEffect, | ||
useImperativeHandle, | ||
forwardRef, | ||
} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { useSubscribe } from '../hooks'; | ||
import { generateId } from '../utils'; | ||
|
||
const Element = forwardRef(({ | ||
styles, | ||
type, | ||
tag: Tag = 'div', | ||
useGlobalFactory = true, | ||
...rest | ||
}, ref) => { | ||
const containerRef = useRef(); | ||
const elementRef = useRef(); | ||
const id = useMemo(() => generateId(), []); | ||
const { lib, globalFactory, createFactory } = useSubscribe(); | ||
|
||
useImperativeHandle(ref, () => ({ | ||
containerRef, | ||
elementRef, | ||
destroy, | ||
})); | ||
|
||
useEffect(() => { | ||
const factory = useGlobalFactory ? globalFactory : createFactory(); | ||
|
||
if (!factory) { | ||
return; | ||
} | ||
|
||
if (elementRef.current) { | ||
elementRef.current.destroy(); | ||
} | ||
|
||
elementRef.current = factory.createAuthElement(type, { | ||
target: containerRef.current, | ||
styles, | ||
...rest, | ||
}); | ||
|
||
return () => { | ||
destroy(); | ||
}; | ||
}, [lib, globalFactory]); | ||
|
||
const destroy = () => { | ||
elementRef.current?.destroy?.(); | ||
}; | ||
|
||
return ( | ||
<Tag ref={containerRef} id={id} /> | ||
); | ||
}); | ||
|
||
Element.displayName = 'Element'; | ||
Element.propTypes = { | ||
styles: PropTypes.object, | ||
type: PropTypes.string.isRequired, | ||
tag: PropTypes.oneOfType([ | ||
PropTypes.string, | ||
PropTypes.elementType, | ||
]), | ||
useGlobalFactory: PropTypes.bool, | ||
}; | ||
|
||
export default Element; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { forwardRef } from 'react'; | ||
|
||
import Element from '../Element'; | ||
|
||
const SigninButton = forwardRef((props, ref) => ( | ||
<Element type="signin-button" {...props} ref={ref} /> | ||
)); | ||
|
||
SigninButton.displayName = 'SigninButton'; | ||
|
||
export default SigninButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { forwardRef } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import Element from '../Element'; | ||
|
||
const SigninWithSubscribeButton = forwardRef((props, ref) => ( | ||
<Element type="signin-with-subscribe-button" {...props} ref={ref} /> | ||
)); | ||
|
||
SigninWithSubscribeButton.displayName = 'SigninWithSubscribeButton'; | ||
SigninWithSubscribeButton.propTypes = { | ||
offer: PropTypes.string, | ||
}; | ||
|
||
export default SigninWithSubscribeButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { useEffect, useCallback, useReducer } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { mockState, mergeDeep } from '@poool/junipero-utils'; | ||
|
||
import { SubscribeContext as Ctx } from '../contexts'; | ||
import { loadScript } from '../utils'; | ||
|
||
const SubscribeContext = ({ | ||
appId, | ||
config, | ||
events, | ||
scriptUrl = 'https://assets.poool-subscribe.fr/subscribe.js', | ||
...rest | ||
}) => { | ||
const [state, dispatch] = useReducer(mockState, { | ||
lib: null, | ||
factory: null, | ||
}); | ||
|
||
useEffect(() => { | ||
init(); | ||
}, []); | ||
|
||
const init = async () => { | ||
if ( | ||
!globalThis.Subscribe || | ||
!globalThis.Subscribe.isPoool || | ||
!globalThis.PooolSubscribe || | ||
!globalThis.PooolSubscribe.isPoool | ||
) { | ||
await loadScript(scriptUrl, 'poool-react-subscribe-lib'); | ||
} | ||
|
||
const lib = globalThis.Subscribe.noConflict(); | ||
|
||
const factory = lib | ||
.init(appId) | ||
.config(config); | ||
|
||
Object | ||
.entries(events || {}) | ||
.forEach(([event, callback]) => { | ||
if (callback.once) { | ||
factory.once(event, callback.callback); | ||
} else { | ||
factory.on(event, callback); | ||
} | ||
}); | ||
|
||
dispatch({ lib, factory }); | ||
}; | ||
|
||
const createFactory = (opts = {}) => { | ||
if (!state.lib) { | ||
return; | ||
} | ||
|
||
const factory = state.lib | ||
.init(appId) | ||
.config(mergeDeep({}, config, opts.config)); | ||
|
||
Object | ||
.entries(events || {}) | ||
.concat(Object.entries(opts.events || {})) | ||
.forEach(([event, callback]) => { | ||
if (callback.once) { | ||
factory.once(event, callback.callback); | ||
} else { | ||
factory.on(event, callback); | ||
} | ||
}); | ||
|
||
return factory; | ||
}; | ||
|
||
const getContext = useCallback(() => ({ | ||
appId, | ||
config, | ||
events, | ||
scriptUrl, | ||
lib: state.lib, | ||
globalFactory: state.factory, | ||
createFactory, | ||
}), [state.lib, state.factory]); | ||
|
||
return ( | ||
<Ctx.Provider value={getContext()} { ...rest } /> | ||
); | ||
}; | ||
|
||
SubscribeContext.displayName = 'SubscribeContext'; | ||
SubscribeContext.propTypes = { | ||
appId: PropTypes.string.isRequired, | ||
config: PropTypes.object, | ||
events: PropTypes.object, | ||
scriptUrl: PropTypes.string, | ||
}; | ||
|
||
export default SubscribeContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { createContext } from 'react'; | ||
|
||
export const SubscribeContext = createContext({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { useContext } from 'react'; | ||
|
||
import { SubscribeContext } from './contexts'; | ||
|
||
export const useSubscribe = () => { | ||
return useContext(SubscribeContext); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export { default as SubscribeContext } from './SubscribeContext'; | ||
export { default as Element } from './Element'; | ||
export { default as SigninButton } from './SigninButton'; | ||
export { | ||
default as SigninWithSubscribeButton, | ||
} from './SigninWithSubscribeButton'; | ||
|
||
export { useSubscribe } from './hooks'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
export const randomString = () => | ||
Math.random().toString(36).substring(2, 5); | ||
|
||
export const generateId = () => { | ||
let id; | ||
|
||
while (!id) { | ||
const temp = `poool-${randomString()}-${randomString()}`; | ||
|
||
/* istanbul ignore else: virtually impossible to happen */ | ||
if (typeof document === 'undefined' || !document.getElementById?.(temp)) { | ||
id = temp; | ||
} | ||
} | ||
|
||
return id; | ||
}; | ||
|
||
export const loadScript = (url, id) => new Promise((resolve, reject) => { | ||
/* istanbul ignore else: tested inside puppeteer */ | ||
if (process.env.NODE_ENV === 'test') { | ||
return resolve(); | ||
} | ||
|
||
if (document.querySelector(`#${id}`)) { | ||
return resolve(); | ||
} | ||
|
||
const script = globalThis.document.createElement('script'); | ||
script.onload = resolve; | ||
script.onerror = reject; | ||
script.async = true; | ||
script.src = url; | ||
script.id = id; | ||
globalThis.document.head.appendChild(script); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters