Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added feature to have custom src per account #41

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- New prop `srcAccount`

## [0.6.0] - 2023-05-15

### Added
Expand Down
20 changes: 20 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ An app that makes it possible to render external iframes on a store.
| `id` | String | ID attribute of the iframe | `null` |
| `className` | String | class attribute of the iframe | `null` |
| `onLoad` | String | onLoad attribute of the iframe | `null` |
| `srcAccount` | Object | Object with account name and src | `null` |

### srcAccount

Using srcAccount

```json
"iframe#logout": {
"props": {
"src": "//www.mywebsiteprod.com/logout",
"srcAccount": {
"mywebsiteprod": "//www.mywebsite.com/logout",
"mywebsiteqa": "//qa.mywebsite.com/logout"
},
"onLoad": "setTimeout(() => {window.location.href='/'}, 5000)",
"className": "iframeLogout",
"id": "iframeLogout"
}
},
```

## Customization

Expand Down
27 changes: 25 additions & 2 deletions react/Iframe.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-disable no-console */
import React from 'react'
import { useCssHandles } from 'vtex.css-handles'
import { useRuntime } from 'vtex.render-runtime'

const CSS_HANDLES = ['container'] as const

interface Props {
src?: string
srcAccount?: any
width?: number
height?: number
allow?: string
Expand All @@ -16,20 +18,35 @@ interface Props {
}

function Iframe(props: Props) {
const { src, width, height, title, allow, id, className, onLoad } = props
const {
src,
srcAccount,
width,
height,
title,
allow,
id,
className,
onLoad,
} = props
const handles = useCssHandles(CSS_HANDLES)
const runtime = useRuntime()

const handleIframeLoad = () => {
if (!onLoad) return null
// eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
return Function('IframeHandler', `"use strict";(${onLoad});`)(onLoad)
}

const processSrc = () => srcAccount?.[runtime.account] ?? src

console.log('processSrc =>', processSrc())

return (
<div className={`${handles.container} w-100 flex justify-center`}>
<iframe
title={title}
src={src}
src={processSrc()}
width={width}
height={height}
allow={allow}
Expand All @@ -52,6 +69,12 @@ Iframe.schema = {
type: 'string',
default: null,
},
srcAccount: {
title: 'editor.iframe.srcAccount.title',
description: 'editor.iframe.srcAccount.description',
type: 'string',
default: null,
},
width: {
title: 'editor.iframe.width.title',
type: 'number',
Expand Down
Loading