-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(REACH-374): Prepare live embed code initializer (#587)
* feat(REACH-374): Prepare single embed initializer * feat(REACH-374): Initialize live and regular embeds separately * feat(REACH-374): Single Embed Code cypress test * fix(REACH-374): Use the right demo for single-embed-code test * fix(REACH-374): use the right single-embed endpoint * fix(REACH-374): fetch embed by embedID * fix(REACH-374): single-embed => live embed rename
- Loading branch information
Showing
7 changed files
with
99 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<title>Static HTML Demo</title> | ||
<style> | ||
#wrapper { | ||
width: 100%; | ||
max-width: 600px; | ||
height: 400px; | ||
margin: 0 auto; | ||
} | ||
.element { | ||
position: fixed; | ||
bottom: 0; | ||
right: 0; | ||
background: red; | ||
color: white; | ||
padding: 10px; | ||
width: 200px; | ||
height: 60px; | ||
line-height: 40px; | ||
z-index: 10000; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="element">I have z-index 10k</div> | ||
<div id="wrapper" data-tf-live="embed-id"></div> | ||
<script src="./lib/embed.js"></script> | ||
</body> | ||
</html> |
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,27 @@ | ||
describe('Single Embed Code', () => { | ||
describe(`Should load`, () => { | ||
before(() => { | ||
cy.intercept('https://api.typeform.com/single-embed/embed-id', { | ||
statusCode: 200, | ||
body: { | ||
html: `<div | ||
id="wrapper" | ||
data-tf-widget="HLjqXS5W" | ||
data-tf-medium="demo-test" | ||
data-tf-transitive-search-params="foo,bar" | ||
data-tf-hidden="foo=foo value" | ||
data-tf-tracking="utm_source=facebook" | ||
data-tf-iframe-props="title=Foo Bar" | ||
></div>`, | ||
}, | ||
}) | ||
cy.visit(`/live-embed.html`) | ||
}) | ||
|
||
it('should display widget', () => { | ||
cy.wait(500) | ||
cy.get('.tf-v1-widget iframe').should('be.visible') | ||
cy.get('.tf-v1-widget iframe').invoke('attr', 'src').should('contain', 'form.typeform.com/to/') | ||
}) | ||
}) | ||
}) |
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
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,20 @@ | ||
import { LIVE_EMBED_ATTRIBUTE } from '../constants' | ||
import { fetchLiveEmbed } from '../live-embed/fetch-live-embed' | ||
|
||
export const initializeLiveEmbeds = async (forceReload: boolean) => { | ||
const embedTypeElements = document.querySelectorAll<HTMLElement>(`[${LIVE_EMBED_ATTRIBUTE}]`) | ||
|
||
for (let index = 0; index < embedTypeElements.length; index += 1) { | ||
const element = embedTypeElements.item(index) | ||
if (forceReload || element.dataset.tfLoaded !== 'true') { | ||
const embedId = element.getAttribute(LIVE_EMBED_ATTRIBUTE) | ||
if (!embedId) { | ||
throw new Error(`Invalid ${LIVE_EMBED_ATTRIBUTE}=${embedId} for embed #${index}`) | ||
} | ||
|
||
const { html } = await fetchLiveEmbed(embedId) | ||
element.innerHTML = html | ||
element.dataset.tfLoaded = 'true' | ||
} | ||
} | ||
} |
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,9 @@ | ||
export const fetchLiveEmbed = async (embedId: string) => { | ||
const response = await fetch(`https://api.typeform.com/single-embed/${embedId}`) | ||
|
||
if (!response.ok) { | ||
throw new Error(`Cannot fetch embed ${embedId}`) | ||
} | ||
|
||
return await response.json() | ||
} |