1
- import React from 'react'
1
+ import React , { useRef } from 'react'
2
2
import styles from './uses.module.css'
3
- import { useItemIDs } from "../datamodel/subscriptions" ;
3
+ import { useItemIDs , useItemByID } from "../datamodel/subscriptions" ;
4
+ import { randomItem } from "../datamodel/item" ;
4
5
5
6
type UsesProps = {
6
7
reflect : any
7
8
}
8
9
9
10
export default function Uses ( { reflect } : UsesProps ) {
10
-
11
11
const itemIDs = useItemIDs ( reflect ) ;
12
+ const inputElement = useRef < HTMLInputElement > ( null ) ;
13
+
14
+ function handleCreateItem ( ) {
15
+ const thing : any = randomItem ( ) ;
16
+ thing . item . name = inputElement ?. current ?. value ;
17
+ reflect . mutate . createItem ( thing ) ;
18
+ if ( inputElement && inputElement . current ) {
19
+ inputElement . current . value = "" ;
20
+ }
21
+ } ;
12
22
13
23
return (
14
24
< div className = { styles . container } >
@@ -18,12 +28,43 @@ export default function Uses({ reflect } : UsesProps) {
18
28
{ itemIDs && itemIDs . map ( ( id ) => {
19
29
return (
20
30
< div key = { id } >
21
- { id }
31
+ < Item
32
+ reflect = { reflect }
33
+ itemID = { id }
34
+ />
22
35
</ div >
23
36
)
24
37
} )
25
38
}
26
39
</ div >
40
+ < div className = { styles . addItem } >
41
+ < input
42
+ placeholder = { "item name" }
43
+ ref = { inputElement }
44
+ type = "text"
45
+ className = { styles . input }
46
+ > </ input >
47
+ < button
48
+ className = { styles . button }
49
+ onClick = { ( ) => handleCreateItem ( ) }
50
+ > +</ button >
51
+ </ div >
52
+ </ div >
53
+ )
54
+ }
55
+
56
+ type ItemProps = {
57
+ reflect : any
58
+ itemID : string
59
+ }
60
+
61
+ function Item ( { reflect, itemID } : ItemProps ) {
62
+ const item = useItemByID ( reflect , itemID ) ;
63
+ return (
64
+ item &&
65
+ < div >
66
+ { item . name }
27
67
</ div >
28
68
)
29
69
}
70
+
0 commit comments