-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v2.0.0 - Refactor to use hook & support new Evenbrite widgets
- Loading branch information
1 parent
3184285
commit 7af1b78
Showing
11 changed files
with
1,945 additions
and
3,625 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,3 +1,4 @@ | ||
.idea | ||
src | ||
images | ||
webpack.config.js |
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 @@ | ||
16.17 |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 was deleted.
Oops, something went wrong.
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,89 @@ | ||
import React from 'react'; | ||
import { v4 as uuid } from 'uuid'; | ||
|
||
const TAG_ID = `EB_SCRIPT_${uuid()}`; | ||
const SCRIPT_URL = 'https://www.eventbrite.com/static/widgets/eb_widgets.js'; | ||
|
||
const useEventbrite = ({ | ||
eventId, | ||
modal, | ||
onOrderComplete, | ||
iFrameHeight, | ||
iFrameAutoAdapt, | ||
promoCode, | ||
}) => { | ||
const id = `EB_${uuid()}`; | ||
const [isLoaded, setLoaded] = React.useState(false); | ||
const onLoad = React.useCallback(() => setLoaded(true), [setLoaded]); | ||
const onErr = React.useCallback(e => { | ||
console.error(`Failed to load Eventbrite script from ${scriptUrl}`); | ||
console.error(e); | ||
|
||
setLoaded(false); | ||
}, [setLoaded]); | ||
|
||
React.useEffect(() => { | ||
if (window?.EBWidgets) { | ||
setLoaded(true); | ||
return; | ||
} | ||
|
||
const existing = document.getElementById(TAG_ID); | ||
|
||
if (existing) { | ||
existing.remove(); | ||
} | ||
|
||
const script = document.createElement('script'); | ||
script.id = TAG_ID; | ||
script.async = true; | ||
script.src = SCRIPT_URL; | ||
script.addEventListener('load', onLoad); | ||
script.addEventListener('error', onErr); | ||
script.addEventListener('abort', onErr); | ||
document.head.appendChild(script); | ||
|
||
return () => { | ||
script.removeEventListener('load', onLoad); | ||
script.removeEventListener('error', onErr); | ||
script.removeEventListener('abort', onErr); | ||
script.remove(); | ||
setLoaded(false); | ||
}; | ||
}, [ | ||
setLoaded, | ||
onLoad, | ||
onErr, | ||
]); | ||
|
||
React.useEffect(() => { | ||
if (!isLoaded) { | ||
return; | ||
} | ||
|
||
const config = { | ||
widgetType: 'checkout', | ||
eventId, | ||
onOrderComplete, | ||
modal, | ||
}; | ||
|
||
if (modal) { | ||
config.modalTriggerElementId = id; | ||
} else { | ||
config.iFrameContainerId = id; | ||
config.iFrameContainerHeight = iFrameHeight || 425; | ||
config.iFrameAutoAdapt = iFrameAutoAdapt || 100; | ||
} | ||
|
||
if (promoCode) { | ||
config.promoCode = promoCode; | ||
} | ||
|
||
window.EBWidgets.createWidget(config); | ||
}, [isLoaded]); | ||
|
||
return isLoaded ? { id } : null; | ||
}; | ||
|
||
export default useEventbrite; |
Oops, something went wrong.