Skip to content

Commit

Permalink
Fix AttributeAddModal to make JSON values less concerning
Browse files Browse the repository at this point in the history
  • Loading branch information
psrok1 committed Feb 27, 2024
1 parent 3b11349 commit f49e556
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 29 deletions.
48 changes: 21 additions & 27 deletions mwdb/web/src/components/AttributesAddModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { toast } from "react-toastify";

import { api } from "@mwdb-web/commons/api";
import { ConfirmationModal } from "@mwdb-web/commons/ui";
import { RichAttributeRenderer } from "./RichAttribute/RichAttributeRenderer";
import { AttributeRenderer } from "@mwdb-web/components/ShowObject/common/AttributeRenderer";

import AceEditor from "react-ace";

Expand Down Expand Up @@ -41,7 +41,7 @@ export function AttributesAddModal({ isOpen, onAdd, onRequestClose }: Props) {
if (ev) ev.preventDefault();
if (!attributeForm.current?.reportValidity()) return;
let value = attributeValue;
if (attributeType === "object") {
if (attributeType === "json") {
try {
value = JSON.parse(attributeValue);
} catch (e: any) {
Expand All @@ -63,8 +63,6 @@ export function AttributesAddModal({ isOpen, onAdd, onRequestClose }: Props) {
attributeDefinitions[ev.target.value].example_value || ""
);
}
setAttributeType("object");

setError(null);
}

Expand Down Expand Up @@ -167,17 +165,17 @@ export function AttributesAddModal({ isOpen, onAdd, onRequestClose }: Props) {
<input
className="form-check-input"
type="radio"
id="value-object"
id="value-json"
name="value-type"
checked={attributeType === "object"}
value="object"
checked={attributeType === "json"}
value="json"
onChange={handleTypeChange}
/>
<label
className="form-check-label"
htmlFor="value-object"
htmlFor="value-json"
>
Object
JSON
</label>
</div>
</div>
Expand Down Expand Up @@ -206,32 +204,28 @@ export function AttributesAddModal({ isOpen, onAdd, onRequestClose }: Props) {
/>
)}
</div>
{richTemplate ? (
{attributeDefinitions[attributeKey] ? (
<div className="form-group">
<label>Rich attribute preview</label>
<label>Attribute preview</label>
<table
className="table table-striped table-bordered table-hover data-table"
style={{
width: `500px`,
}}
>
<tbody>
<tr>
<th>{"My attribute"}</th>
<td>
<RichAttributeRenderer
template={richTemplate}
value={
attributeType === "string"
? JSON.stringify(
attributeValue
)
: attributeValue
}
setInvalid={setInvalid}
/>
</td>
</tr>
<AttributeRenderer
attributes={[
{
key: attributeKey,
id: 0,
value: attributeValue,
},
]}
attributeDefinition={
attributeDefinitions[attributeKey]
}
/>
</tbody>
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AttributeDefinition } from "@mwdb-web/types/types";
type Props = {
attributes: Attribute[];
attributeDefinition: AttributeDefinition;
onRemoveAttribute: (id: number) => void;
onRemoveAttribute?: (id: number) => void;
};

export function AttributeRenderer({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Props = {
rich_template: string;
key: string;
};
onRemove: (id: number) => void;
onRemove?: (id: number) => void;
};

export function AttributeValue({
Expand Down

0 comments on commit f49e556

Please sign in to comment.