|
1 | 1 | import * as React from "react";
|
| 2 | +import {useState} from "react"; |
2 | 3 |
|
3 | 4 | import {ServerItemRendererWithDebugUI} from "../../../../testing/server-item-renderer-with-debug-ui";
|
4 | 5 | import {storybookDependenciesV2} from "../../../../testing/test-dependencies";
|
@@ -83,3 +84,100 @@ export const MultiWidgetWithInteractionCallback = (
|
83 | 84 | />
|
84 | 85 | );
|
85 | 86 | };
|
| 87 | + |
| 88 | +export const Interactive = () => { |
| 89 | + const [value, setValue] = useState(""); |
| 90 | + const [debug, setDebug] = useState(true); |
| 91 | + |
| 92 | + function findItemData(input) { |
| 93 | + const parsed = JSON.parse(input); |
| 94 | + const itemData = |
| 95 | + parsed?.data?.assessmentItem?.item?.itemData || |
| 96 | + parsed?.assessmentItem?.item?.itemData || |
| 97 | + parsed?.item?.itemData || |
| 98 | + parsed?.itemData; |
| 99 | + if (itemData) { |
| 100 | + return JSON.parse(itemData); |
| 101 | + } else if (parsed.question && parsed.hints) { |
| 102 | + return parsed; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + function extractItemData() { |
| 107 | + const itemData = findItemData(value); |
| 108 | + setValue(JSON.stringify(itemData, null, 2)); |
| 109 | + } |
| 110 | + |
| 111 | + let content = null; |
| 112 | + let parsingError = false; |
| 113 | + try { |
| 114 | + if (value) { |
| 115 | + content = findItemData(value); |
| 116 | + } |
| 117 | + } catch (e) { |
| 118 | + parsingError = true; |
| 119 | + // eslint-disable-next-line no-console |
| 120 | + console.error(e); |
| 121 | + } |
| 122 | + |
| 123 | + return ( |
| 124 | + <div> |
| 125 | + <div |
| 126 | + style={{ |
| 127 | + padding: "2rem", |
| 128 | + }} |
| 129 | + > |
| 130 | + <label> |
| 131 | + Dump Perseus data here |
| 132 | + <textarea |
| 133 | + value={value} |
| 134 | + onChange={(e) => setValue(e.target.value)} |
| 135 | + style={{ |
| 136 | + display: "block", |
| 137 | + width: "100%", |
| 138 | + height: "20rem", |
| 139 | + }} |
| 140 | + /> |
| 141 | + </label> |
| 142 | + {content && ( |
| 143 | + <div> |
| 144 | + <button onClick={extractItemData}> |
| 145 | + Extract item data |
| 146 | + </button> |
| 147 | + <button onClick={() => setDebug(!debug)}> |
| 148 | + {debug |
| 149 | + ? "Use ServerItemRenderer" |
| 150 | + : "Use ServerItemRendererWithDebugUI"} |
| 151 | + </button> |
| 152 | + </div> |
| 153 | + )} |
| 154 | + </div> |
| 155 | + <div |
| 156 | + style={{ |
| 157 | + padding: "2rem", |
| 158 | + }} |
| 159 | + > |
| 160 | + {parsingError && <p>There was a problem parsing the JSON</p>} |
| 161 | + {content && ( |
| 162 | + <> |
| 163 | + {debug ? ( |
| 164 | + <ServerItemRendererWithDebugUI item={content} /> |
| 165 | + ) : ( |
| 166 | + <ServerItemRenderer |
| 167 | + problemNum={0} |
| 168 | + item={content} |
| 169 | + dependencies={storybookDependenciesV2} |
| 170 | + linterContext={{ |
| 171 | + contentType: "", |
| 172 | + highlightLint: true, |
| 173 | + paths: [], |
| 174 | + stack: [], |
| 175 | + }} |
| 176 | + /> |
| 177 | + )} |
| 178 | + </> |
| 179 | + )} |
| 180 | + </div> |
| 181 | + </div> |
| 182 | + ); |
| 183 | +}; |
0 commit comments