diff --git a/components/doc/common/docactions.js b/components/doc/common/docactions.js index 90b70a8773..0ce91b6723 100644 --- a/components/doc/common/docactions.js +++ b/components/doc/common/docactions.js @@ -2,12 +2,14 @@ import React, { useEffect, useContext } from 'react'; import { useRef } from 'react'; import { Button } from '../../lib/button/Button'; import { Menu } from '../../lib/menu/Menu'; +import { Toast } from '../../lib/toast/Toast'; import DomHandler from '../../lib/utils/DomHandler'; import { useLiveEditor } from './liveeditor'; import AppContentContext from '../../../components/layout/appcontentcontext'; export const DocActions = (props) => { const menu = useRef(null); + const toast = useRef(null); const liveEditor = useRef(null); const context = useContext(AppContentContext); @@ -20,19 +22,19 @@ export const DocActions = (props) => { const items = [ { label: 'Hooks Source Demo', - command: () => liveEditor.current.postSandboxParameters('hooks') + command: () => liveEditor.current.postSandboxParameters('hooks', toast) }, { label: 'Class Source Demo', - command: () => liveEditor.current.postSandboxParameters('class') + command: () => liveEditor.current.postSandboxParameters('class', toast) }, { label: 'TS Source Demo', - command: () => liveEditor.current.postSandboxParameters('ts') + command: () => liveEditor.current.postSandboxParameters('ts', toast) }, { label: 'Browser Source Demo', - command: () => liveEditor.current.postSandboxParameters('browser') + command: () => liveEditor.current.postSandboxParameters('browser', toast) } ]; @@ -61,6 +63,8 @@ export const DocActions = (props) => { } return ( + <> +
+ ) } diff --git a/components/doc/common/liveeditor.js b/components/doc/common/liveeditor.js index fd39abf658..4ba42b8859 100644 --- a/components/doc/common/liveeditor.js +++ b/components/doc/common/liveeditor.js @@ -382,6 +382,9 @@ ${extIndexCSS} const getSandboxParameters = (sourceType) => { let { name, sources, dependencies } = props; + if (!sources[sourceType]) { + return null; + } let extension = '.js'; let serviceExtension = extension; let extDependencies = dependencies || {}; @@ -485,14 +488,19 @@ ${extIndexCSS} } return { - postSandboxParameters(sourceType) { + postSandboxParameters(sourceType, toast) { + const sandboxParameters = getSandboxParameters(sourceType); + if (!sandboxParameters) { + toast.current.show({severity: 'warn', summary: 'Not Available', detail: 'That code sandbox demonstration is not available!'}); + return; + } fetch('https://codesandbox.io/api/v1/sandboxes/define?json=1', { method: "POST", headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' }, - body: JSON.stringify(getSandboxParameters(sourceType)) + body: JSON.stringify(sandboxParameters) }) .then(response => response.json()) .then(data => window.open(`https://codesandbox.io/s/${data.sandbox_id}`, '_blank'));