refactor: patch the whole thing for now, opt later
This commit is contained in:
parent
50d1acc756
commit
cbb2f35e7f
27
packages/entity/src/hooks/use-trait-patcher.js
Normal file
27
packages/entity/src/hooks/use-trait-patcher.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
import {join} from 'path';
|
||||
|
||||
import {useDispatch} from '@latus/redux';
|
||||
import {useProject, useUri} from '@persea/core';
|
||||
import {patchJsonResource} from '@persea/json';
|
||||
|
||||
export default (json, path) => {
|
||||
const dispatch = useDispatch();
|
||||
const project = useProject();
|
||||
const uri = useUri();
|
||||
return (type, key, value) => {
|
||||
dispatch(patchJsonResource({
|
||||
patch: [
|
||||
{
|
||||
op: 'replace',
|
||||
path: join(path, type),
|
||||
value: {
|
||||
...json[type],
|
||||
[key]: value,
|
||||
},
|
||||
},
|
||||
],
|
||||
project,
|
||||
uri,
|
||||
}));
|
||||
};
|
||||
};
|
|
@ -1,24 +1,25 @@
|
|||
import './existent.scss';
|
||||
|
||||
import {join} from 'path';
|
||||
|
||||
import {PropTypes, React} from '@latus/react';
|
||||
import {useDispatch} from '@latus/redux';
|
||||
import {useProject, useUri} from '@persea/core';
|
||||
import {patchJsonResource} from '@persea/json';
|
||||
|
||||
import useTraitPatcher from '../hooks/use-trait-patcher';
|
||||
|
||||
const Existent = ({
|
||||
json,
|
||||
path,
|
||||
}) => {
|
||||
const dispatch = useDispatch();
|
||||
const project = useProject();
|
||||
const uri = useUri();
|
||||
const patchTrait = useTraitPatcher(json, path);
|
||||
return (
|
||||
<div className="existent">
|
||||
<label>
|
||||
Is ticking
|
||||
<input type="checkbox" checked={json.state.isTicking} readOnly />
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={json.state.isTicking}
|
||||
onChange={(event) => {
|
||||
patchTrait('state', 'isTicking', event.target.checked);
|
||||
}}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Name
|
||||
|
@ -26,22 +27,7 @@ const Existent = ({
|
|||
type="text"
|
||||
value={json.state.name}
|
||||
onChange={(event) => {
|
||||
dispatch(patchJsonResource({
|
||||
patch: [
|
||||
{
|
||||
op: 'add',
|
||||
path: join(path, 'state'),
|
||||
value: {},
|
||||
},
|
||||
{
|
||||
op: 'replace',
|
||||
path: join(path, 'state/name'),
|
||||
value: event.target.value,
|
||||
},
|
||||
],
|
||||
project,
|
||||
uri,
|
||||
}));
|
||||
patchTrait('state', 'name', event.target.value);
|
||||
}}
|
||||
/>
|
||||
</label>
|
||||
|
|
Loading…
Reference in New Issue
Block a user