diff --git a/packages/perseus/src/__stories__/server-item-renderer.stories.tsx b/packages/perseus/src/__stories__/server-item-renderer.stories.tsx index b3afcd3fbb..7599ed3894 100644 --- a/packages/perseus/src/__stories__/server-item-renderer.stories.tsx +++ b/packages/perseus/src/__stories__/server-item-renderer.stories.tsx @@ -1,4 +1,5 @@ import * as React from "react"; +import {useState} from "react"; import {ServerItemRendererWithDebugUI} from "../../../../testing/server-item-renderer-with-debug-ui"; import {storybookDependenciesV2} from "../../../../testing/test-dependencies"; @@ -83,3 +84,100 @@ export const MultiWidgetWithInteractionCallback = ( /> ); }; + +export const Interactive = () => { + const [value, setValue] = useState(""); + const [debug, setDebug] = useState(true); + + function findItemData(input) { + const parsed = JSON.parse(input); + const itemData = + parsed?.data?.assessmentItem?.item?.itemData || + parsed?.assessmentItem?.item?.itemData || + parsed?.item?.itemData || + parsed?.itemData; + if (itemData) { + return JSON.parse(itemData); + } else if (parsed.question && parsed.hints) { + return parsed; + } + } + + function extractItemData() { + const itemData = findItemData(value); + setValue(JSON.stringify(itemData, null, 2)); + } + + let content = null; + let parsingError = false; + try { + if (value) { + content = findItemData(value); + } + } catch (e) { + parsingError = true; + // eslint-disable-next-line no-console + console.error(e); + } + + return ( +
There was a problem parsing the JSON
} + {content && ( + <> + {debug ? ( +