-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transact): support nested JSON txs (#48)
- Loading branch information
1 parent
b391ab5
commit 86a4ac2
Showing
11 changed files
with
543 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ node_modules/ | |
public/js | ||
out/ | ||
dist/ | ||
.clj-kondo/ | ||
.lsp/ | ||
.history/ | ||
|
||
/target | ||
/checkouts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React from 'react' | ||
const { HomebaseProvider, useTransact, useEntity } = window.homebase.react | ||
|
||
const config = { | ||
schema: { | ||
store: { | ||
items: { type: 'ref', cardinality: 'many' } | ||
}, | ||
item: { | ||
date: { type: 'ref', cardinality: 'one' } | ||
} | ||
}, | ||
initialData: [{ | ||
store: { | ||
identity: 'store 1', | ||
items: [ | ||
{ item: { name: 'item 1' } }, | ||
{ item: { name: 'item 2' } }, | ||
{ item: { name: 'item 3' } }, | ||
{ item: { name: 'item 4' } }, | ||
{ item: { name: 'item 5', date: { year: 2021, month: 1, day: 3 } } }, | ||
] | ||
} | ||
}] | ||
} | ||
|
||
export const App = () => ( | ||
<HomebaseProvider config={config}> | ||
<Items/> | ||
</HomebaseProvider> | ||
) | ||
|
||
const Items = () => { | ||
const [store] = useEntity({ identity: 'store 1' }) | ||
const [transact] = useTransact() | ||
|
||
let newI = null | ||
const onDragOver = React.useCallback(e => { | ||
e.preventDefault() | ||
newI = parseInt(e.target.dataset.index) | ||
}) | ||
|
||
const reorder = React.useCallback((id, orderMin, orderMax) => { | ||
const order = (orderMin + orderMax) / 2.0 | ||
transact([{'homebase.array': {id, order}}]) | ||
}, [transact]) | ||
|
||
return ( | ||
<div> | ||
{store.get('items').map((item, i) => ( | ||
<div | ||
key={item.get('ref', 'id')} | ||
style={{ cursor: 'move' }} | ||
data-index={i} | ||
draggable | ||
onDragOver={onDragOver} | ||
onDragEnd={e => reorder( | ||
item.get('id'), | ||
newI > 0 && store.get('items', newI - 1, 'order') || 0, | ||
store.get('items', newI, 'order'), | ||
)} | ||
> | ||
↕ {item.get('ref', 'name')} | ||
<small>{item.get('ref', 'date', 'year')}</small> | ||
</div> | ||
))} | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
(ns example.array | ||
(:require | ||
[devcards.core :as dc] | ||
[homebase.react] | ||
["../js_gen/array-example" :as react-example]) | ||
(:require-macros | ||
[devcards.core :refer [defcard-rg defcard-doc]] | ||
[dev.macros :refer [inline-resource]])) | ||
|
||
(defcard-rg array-example | ||
[react-example/App]) | ||
|
||
(def code-snippet | ||
(clojure.string/replace-first | ||
(inline-resource "js/array-example.jsx") | ||
"const { HomebaseProvider, useTransact, useEntity } = window.homebase.react" | ||
"import { HomebaseProvider, useTransact, useEntity } from 'homebase-react'")) | ||
(defcard-doc | ||
"[🔗GitHub](https://github.com/homebaseio/homebase-react/blob/master/js/array-example.jsx)" | ||
(str "```javascript\n" code-snippet "\n```")) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.