Skip to content

Commit 18a5479

Browse files
authored
Rich preview in AttributeAddModal (#724)
1 parent 091d004 commit 18a5479

File tree

3 files changed

+83
-22
lines changed

3 files changed

+83
-22
lines changed

mwdb/web/src/components/AttributesAddModal.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useRef, useState, useEffect, useCallback } from "react";
22

33
import api from "@mwdb-web/commons/api";
44
import { ConfirmationModal } from "@mwdb-web/commons/ui";
5+
import RichAttributeRenderer from "./RichAttribute/RichAttributeRenderer";
56

67
import AceEditor from "react-ace";
78

@@ -13,8 +14,10 @@ import "ace-builds/src-noconflict/ext-searchbox";
1314
export default function AttributesAddModal({ isOpen, onAdd, onRequestClose }) {
1415
const [attributeDefinitions, setAttributeDefinitions] = useState({});
1516
const [attributeKey, setAttributeKey] = useState("");
17+
const [richTemplate, setRichTemplate] = useState("");
1618
const [attributeValue, setAttributeValue] = useState("");
1719
const [attributeType, setAttributeType] = useState("string");
20+
const [invalid, setInvalid] = useState(false);
1821
const [error, setError] = useState(null);
1922
const attributeForm = useRef(null);
2023

@@ -35,6 +38,17 @@ export default function AttributesAddModal({ isOpen, onAdd, onRequestClose }) {
3538

3639
function handleKeyChange(ev) {
3740
setAttributeKey(ev.target.value);
41+
if (!ev.target.value.length) setRichTemplate("");
42+
else {
43+
setRichTemplate(
44+
attributeDefinitions[ev.target.value].rich_template
45+
);
46+
setAttributeValue(
47+
attributeDefinitions[ev.target.value].example_value || ""
48+
);
49+
}
50+
setAttributeType("object");
51+
3852
setError(null);
3953
}
4054

@@ -81,7 +95,7 @@ export default function AttributesAddModal({ isOpen, onAdd, onRequestClose }) {
8195
isOpen={isOpen}
8296
onRequestClose={onRequestClose}
8397
onConfirm={handleSubmit}
84-
confirmDisabled={!attributesAvailable}
98+
confirmDisabled={!attributesAvailable || (invalid && richTemplate)}
8599
>
86100
{error ? (
87101
<div
@@ -123,7 +137,7 @@ export default function AttributesAddModal({ isOpen, onAdd, onRequestClose }) {
123137
</select>
124138
{attributeDefinitions[attributeKey] &&
125139
attributeDefinitions[attributeKey].description ? (
126-
<div className="form-hint">
140+
<div className="form-group pt-2">
127141
{attributeDefinitions[attributeKey].description}
128142
</div>
129143
) : (
@@ -183,14 +197,39 @@ export default function AttributesAddModal({ isOpen, onAdd, onRequestClose }) {
183197
wrapEnabled
184198
onChange={(input) => setAttributeValue(input)}
185199
value={attributeValue}
186-
width="300px"
200+
width="500px"
187201
height="150px"
188202
setOptions={{
189203
useWorker: false,
190204
}}
191205
/>
192206
)}
193207
</div>
208+
{richTemplate ? (
209+
<div className="form-group">
210+
<label>Rich attribute preview</label>
211+
<table
212+
className="table table-striped table-bordered table-hover data-table"
213+
style={{
214+
width: `500px`,
215+
}}
216+
>
217+
<tbody>
218+
<RichAttributeRenderer
219+
template={richTemplate}
220+
value={
221+
attributeType === "string"
222+
? JSON.stringify(attributeValue)
223+
: attributeValue
224+
}
225+
setInvalid={setInvalid}
226+
/>
227+
</tbody>
228+
</table>
229+
</div>
230+
) : (
231+
[]
232+
)}
194233
</form>
195234
)}
196235
</ConfirmationModal>

mwdb/web/src/components/RichAttribute/RichAttributePreview.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React, { useReducer, useState } from "react";
1+
import React, { useReducer, useState, useEffect } from "react";
22
import AceEditor from "react-ace";
3-
import { renderValue } from "./MarkedMustache";
3+
import RichAttributeRenderer from "./RichAttributeRenderer";
44

55
import "ace-builds/src-noconflict/mode-markdown";
66
import "ace-builds/src-noconflict/mode-json";
@@ -62,6 +62,8 @@ export default function RichAttributePreview({
6262
valueInput: storedExampleValue,
6363
});
6464
const [showContext, setShowContext] = useState(false);
65+
const [invalid, setInvalid] = useState(false);
66+
const [contextValue, setContextValue] = useState(null);
6567

6668
function chooseTemplate(ev) {
6769
const index = ev.target.value;
@@ -80,19 +82,15 @@ export default function RichAttributePreview({
8082
templateState.chosenExample !== "custom"
8183
? exampleTemplates[templateState.chosenExample].value
8284
: templateState.valueInput;
83-
let renderedValue,
84-
contextValue,
85-
invalid = false;
86-
try {
87-
contextValue = makeContext(JSON.parse(value));
88-
renderedValue = renderValue(template, contextValue, {
89-
searchEndpoint: "/",
90-
});
91-
} catch (e) {
92-
renderedValue = e.toString();
93-
contextValue = null;
94-
invalid = true;
95-
}
85+
86+
useEffect(() => {
87+
try {
88+
setContextValue(makeContext(JSON.parse(value)));
89+
} catch (e) {
90+
setContextValue(null);
91+
setInvalid(true);
92+
}
93+
}, [value, setContextValue, setInvalid]);
9694

9795
return (
9896
<View ident="attributePreview">
@@ -177,10 +175,11 @@ export default function RichAttributePreview({
177175
<div className="col">
178176
<strong>Preview</strong>
179177
<DataTable>
180-
<tr>
181-
<th>My attribute</th>
182-
<td>{renderedValue}</td>
183-
</tr>
178+
<RichAttributeRenderer
179+
template={template}
180+
value={value}
181+
setInvalid={setInvalid}
182+
/>
184183
</DataTable>
185184
</div>
186185
</div>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from "react";
2+
import { renderValue } from "./MarkedMustache";
3+
4+
import { makeContext } from "./exampleTemplates";
5+
6+
export default function RichAttributeRenderer({ template, value, setInvalid }) {
7+
let renderedValue;
8+
try {
9+
renderedValue = renderValue(template, makeContext(JSON.parse(value)), {
10+
searchEndpoint: "/",
11+
});
12+
setInvalid(false);
13+
} catch (e) {
14+
renderedValue = e.toString();
15+
setInvalid(true);
16+
}
17+
return (
18+
<tr>
19+
<th>{"My attribute"}</th>
20+
<td>{renderedValue}</td>
21+
</tr>
22+
);
23+
}

0 commit comments

Comments
 (0)