Skip to content

Commit

Permalink
fix(REACH-374): fetch embed by embedID
Browse files Browse the repository at this point in the history
  • Loading branch information
gierlas authored and Matej Lednicky committed May 31, 2023
1 parent 248bd13 commit 6ca4b64
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/demo-html/public/single-embed-code.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</head>
<body>
<div class="element">I have z-index 10k</div>
<div id="wrapper" data-tf-live="HLjqXS5W-embed-id"></div>
<div id="wrapper" data-tf-live="embed-id"></div>
<script src="./lib/embed.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion packages/embed/e2e/spec/functional/single-embed-code.cy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe('Single Embed Code', () => {
describe(`Should load`, () => {
before(() => {
cy.intercept('https://api.typeform.com/single-embed/HLjqXS5W-embed-id', {
cy.intercept('https://api.typeform.com/single-embed/embed-id', {
statusCode: 200,
body: {
html: `<div
Expand Down
15 changes: 4 additions & 11 deletions packages/embed/src/initializers/initialize-single-embeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@ export const initializeSingleEmbeds = async (forceReload: boolean) => {
for (let index = 0; index < embedTypeElements.length; index += 1) {
const element = embedTypeElements.item(index)
if (forceReload || element.dataset.tfLoaded !== 'true') {
const code = element.getAttribute(SINGLE_EMBED_ATTRIBUTE)
if (!code) {
throw new Error(`Invalid ${SINGLE_EMBED_ATTRIBUTE}=${code} for embed #${index}`)
const embedId = element.getAttribute(SINGLE_EMBED_ATTRIBUTE)
if (!embedId) {
throw new Error(`Invalid ${SINGLE_EMBED_ATTRIBUTE}=${embedId} for embed #${index}`)
}

const separatorPosition = code.indexOf('-')
const formId = code.substring(0, separatorPosition)
const embedId = code.substring(separatorPosition + 1)
if (formId === '' || embedId === '') {
throw new Error(`Invalid ${SINGLE_EMBED_ATTRIBUTE}=${code} for embed #${index}`)
}

const { html } = await fetchSingleEmbed(formId, embedId)
const { html } = await fetchSingleEmbed(embedId)
element.innerHTML = html
element.dataset.tfLoaded = 'true'
}
Expand Down
6 changes: 3 additions & 3 deletions packages/embed/src/single-embed/fetch-single-embed.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export const fetchSingleEmbed = async (formId: string, embedId: string) => {
const response = await fetch(`https://api.typeform.com/single-embed/${formId}-${embedId}`)
export const fetchSingleEmbed = async (embedId: string) => {
const response = await fetch(`https://api.typeform.com/single-embed/${embedId}`)

if (!response.ok) {
throw new Error(`Cannot fetch embed ${embedId} for form ${formId}`)
throw new Error(`Cannot fetch embed ${embedId}`)
}

return await response.json()
Expand Down

0 comments on commit 6ca4b64

Please sign in to comment.