refactor: patch the whole thing for now, opt later

This commit is contained in:
cha0s 2021-01-25 16:37:20 -06:00
parent 50d1acc756
commit cbb2f35e7f
2 changed files with 38 additions and 25 deletions

View 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,
}));
};
};

View File

@ -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>