From f667d42e0606efaf9c5ce34003aa9775c7f4f580 Mon Sep 17 00:00:00 2001 From: Matej Lednicky Date: Wed, 6 Dec 2023 16:21:02 +0100 Subject: [PATCH] feat(TU-3717): Expose method to fetch form details in callback (#4) --- README.md | 51 ++++++++++++++++++++---------- demo/embed.html | 17 ++-------- demo/iframe-html.html | 12 ++++--- demo/iframe-js.html | 11 +++++-- demo/popup-html.html | 21 ++++++++---- demo/popup-js.html | 11 +++++-- package.json | 3 +- src/lib/fetch-form-details.test.ts | 43 +++++++++++++++++++++++++ src/lib/fetch-form-details.ts | 21 ++++++++++++ src/lib/open.ts | 6 ++-- yarn.lock | 45 ++++++++++++++++++++++++++ 11 files changed, 191 insertions(+), 50 deletions(-) create mode 100644 src/lib/fetch-form-details.test.ts create mode 100644 src/lib/fetch-form-details.ts diff --git a/README.md b/README.md index 7c9a9e0..5fecad0 100644 --- a/README.md +++ b/README.md @@ -89,23 +89,23 @@ window.tfEmbedAdmin.setDefaultConfiguration({ When using HTML API you don't need to call this method separately. You need to specify config options on the button itself. -### selectForm({ callback}) +### selectForm({ callback }) Open embed admin to select form or create a new one. It accepts an object with the following props: -| name | type | description | -| -------- | ------------------------------------------------------- | ----------------------------------------------------------------- | -| callback | `(payload: { action: string, formId: string }) => void` | Method to be called when a form is selected in Typeform Admin UI. | -| type | `"iframe" \| "popup"` | Optional. See `setDefaultConfiguration` above. | -| appName | `string` | Optional. See `setDefaultConfiguration` above. | +| name | type | description | +| -------- | -------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- | +| callback | `(payload: { action: string, formId: string, fetchFormDetails: () => Promise<{}> }) => void` | Method to be called when a form is selected in Typeform Admin UI. | +| type | `"iframe" \| "popup"` | Optional. See `setDefaultConfiguration` above. | +| appName | `string` | Optional. See `setDefaultConfiguration` above. | Example with JavaScript: ```javascript window.tfEmbedAdmin.selectForm({ - callback: ({ action, formId }) => console.log(`you just selected form id: ${formId}`), + callback: ({ action, formId, fetchFormDetails }) => console.log(`you just selected form id: ${formId}`), }) ``` @@ -121,7 +121,7 @@ Or with HTML API: select typeform @@ -133,19 +133,19 @@ Open embed admin to edit a specific form. It accepts an object with the following props: -| name | type | description | -| -------- | ------------------------------------------------------- | --------------------------------------------------------------- | -| formId | `string` | ID of the typeform to edit | -| callback | `(payload: { action: string, formId: string }) => void` | Method to be called when a form is edited in Typeform Admin UI. | -| type | `"iframe" \| "popup"` | Optional. See `setDefaultConfiguration` above. | -| appName | `string` | Optional. See `setDefaultConfiguration` above. | +| name | type | description | +| -------- | -------------------------------------------------------------------------------------------- | --------------------------------------------------------------- | +| formId | `string` | ID of the typeform to edit | +| callback | `(payload: { action: string, formId: string, fetchFormDetails: () => Promise<{}> }) => void` | Method to be called when a form is edited in Typeform Admin UI. | +| type | `"iframe" \| "popup"` | Optional. See `setDefaultConfiguration` above. | +| appName | `string` | Optional. See `setDefaultConfiguration` above. | Example with JavaScript: ```javascript window.tfEmbedAdmin.editForm({ formId: myTypeformId, - callback: ({ action, formId }) => console.log(`you just edited form id: ${formId}`), + callback: ({ action, formId, fetchFormDetails }) => console.log(`you just edited form id: ${formId}`), }) ``` @@ -161,12 +161,27 @@ Or with HTML API: edit typeform ``` +### fetchFormDetails() + +The callback receives `fetchFormDetails` async method in the payload. You can use this method to fetch details about currently selected / edited form. It returns `title`, `url` and `imageUrl` of the meta image. + +Usage: + +```javascript +window.tfEmbedAdmin.selectForm({ + callback: async ({ action, formId, fetchFormDetails }) => { + const { title, url } = await fetchFormDetails() + console.log(`You selected form named ${title}. You can visit it at ${url}.`) + }, +}) +``` + ## Demo Run: @@ -175,10 +190,12 @@ Run: yarn start ``` -Demo implementation of the library will be served at http://localhost:9090 +Demo implementation of the library will be served at http://localhost:1337 Or [open the demo in CodeSandbox](https://codesandbox.io/s/github/Typeform/button), directly in your browser. +_Note:_ Examples with iframe only work on localhost. + ## Development Requirements: diff --git a/demo/embed.html b/demo/embed.html index 3ff8780..15cd901 100644 --- a/demo/embed.html +++ b/demo/embed.html @@ -47,21 +47,10 @@

Typeform Button Demo - with Embed SDK

diff --git a/demo/iframe-js.html b/demo/iframe-js.html index 8b054b2..9c19c67 100644 --- a/demo/iframe-js.html +++ b/demo/iframe-js.html @@ -11,16 +11,18 @@

Typeform Button Demo - Iframe JS

diff --git a/demo/popup-js.html b/demo/popup-js.html index 8c839d2..8f99de4 100644 --- a/demo/popup-js.html +++ b/demo/popup-js.html @@ -9,16 +9,18 @@

Typeform Button Demo - Popup JS