Skip to content

Commit d9807e7

Browse files
authored
Merge pull request #241 from cenit-io/update-json-viewer
Update json-viewer
2 parents 6467d58 + a1dec13 commit d9807e7

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/actions/Show.js

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const Show = ({ docked, dataType, record, onSubjectPicked, height }) => {
2929

3030
const classes = useStyles();
3131

32+
const jsonProjection = ({ id }) => dataType.get(id);
33+
3234
useEffect(() => {
3335
setContainerState({ breadcrumbActionName: "Show" });
3436
}, []);
@@ -65,6 +67,7 @@ const Show = ({ docked, dataType, record, onSubjectPicked, height }) => {
6567
height={height}
6668
readOnly={true}
6769
noSubmitButton={true}
70+
jsonProjection={jsonProjection}
6871
formActionKey={Show.key} />
6972
{editButton}
7073
</div>

src/components/JsonViewer.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
import clsx from "clsx";
2-
import React, { useState, useEffect } from "react";
1+
import React, { useEffect } from "react";
32
import { useFormObjectValue } from "./FormContext";
3+
import { useSpreadState } from "../common/hooks";
4+
import Loading from "./Loading";
45

56
export default function JsonViewer({ className, projection }) {
67

7-
let value = useFormObjectValue();
8+
const value = useFormObjectValue();
9+
const [state, setState] = useSpreadState({ record: {}, loaded: false });
10+
const { record, loaded } = state;
811

9-
if (projection) {
10-
value = projection(value);
11-
}
12+
useEffect(() => {
13+
if (projection) {
14+
const subscription = projection(value).subscribe((record) => setState({ record, loaded: true }));
15+
return () => subscription.unsubscribe();
16+
} else {
17+
setState({ record: value, loaded: true });
18+
}
19+
}, [value]);
1220

1321
return (
1422
<div className={className}>
15-
<pre>
16-
{JSON.stringify(value, null, 2)}
17-
</pre>
23+
{loaded ? <pre>{JSON.stringify(record, null, 2)}</pre> : <Loading/>}
1824
</div>
1925
);
2026
}

0 commit comments

Comments
 (0)