Skip to content

Commit 0fdde8d

Browse files
authored
Merge pull request #5 from cindywu/cindywu/move-add-item-to-uses
move add item to uses and clear input after submit
2 parents 840edf6 + a968309 commit 0fdde8d

File tree

5 files changed

+88
-7
lines changed

5 files changed

+88
-7
lines changed

src/datamodel/item.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function putItem(
2525
tx: WriteTransaction,
2626
{ id, item }: { id: string; item: Item }
2727
): Promise<void> {
28-
console.log('creating item...')
28+
console.log('creating item...', item)
2929
return tx.put(key(id), item);
3030
}
3131

src/datamodel/subscriptions.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Reflect } from "@rocicorp/reflect";
22
import { useSubscribe } from "replicache-react";
33
import { getClientState, clientStatePrefix } from "./client-state";
44
import { getShape, shapePrefix } from "./shape";
5-
import { itemPrefix } from "./item";
5+
import { getItem, itemPrefix } from "./item";
66
import type { M } from "./mutators";
77

88
export function useItemIDs(reflect: Reflect<M>) {
@@ -19,6 +19,16 @@ export function useItemIDs(reflect: Reflect<M>) {
1919
);
2020
}
2121

22+
export function useItemByID(reflect: Reflect<M>, id: string) {
23+
return useSubscribe(
24+
reflect,
25+
async (tx) => {
26+
return await getItem(tx, id);
27+
},
28+
null
29+
);
30+
}
31+
2232
export function useShapeIDs(reflect: Reflect<M>) {
2333
return useSubscribe(
2434
reflect,

src/frontend/nav.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function Nav({ reflect, online }: NavProps) {
2121
const urlBox = useRef<HTMLInputElement>(null);
2222
const userInfo = useUserInfo(reflect);
2323
const itemIDs = useItemIDs(reflect);
24+
const inputElement = useRef<HTMLInputElement>(null);
2425

2526
useEffect(() => {
2627
if (shareVisible) {
@@ -33,13 +34,21 @@ export function Nav({ reflect, online }: NavProps) {
3334
};
3435

3536
function handleCreateItem(){
36-
reflect.mutate.createItem(randomItem());
37+
const thing : any = randomItem();
38+
thing.item.name = inputElement?.current?.value;
39+
reflect.mutate.createItem(thing);
3740
};
3841

3942
return (
4043
<>
4144
<div>
4245
add item
46+
<input
47+
placeholder={"item name"}
48+
ref={inputElement}
49+
type="text"
50+
>
51+
</input>
4352
<button
4453
onClick={() => handleCreateItem()}
4554
>

src/frontend/uses.module.css

+21
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,25 @@
1717
.itemsList {
1818
padding: 2rem;
1919
text-align: left;
20+
}
21+
22+
.addItem {
23+
position: fixed;
24+
bottom: 0;
25+
right: 0;
26+
padding: 1rem 3rem;
27+
margin: 1rem;
28+
}
29+
30+
.input {
31+
padding: .5rem 1rem;
32+
background-color: hsl(0, 0%, 70%);
33+
outline: none;
34+
border: 1px solid transparent;
35+
}
36+
37+
.button {
38+
padding: .5rem 1rem;
39+
border: 1px solid transparent;
40+
background-color: hsl(0, 0%, 40%);
2041
}

src/frontend/uses.tsx

+45-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
import React from 'react'
1+
import React, { useRef } from 'react'
22
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";
45

56
type UsesProps = {
67
reflect: any
78
}
89

910
export default function Uses({ reflect } : UsesProps) {
10-
1111
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+
};
1222

1323
return (
1424
<div className={styles.container}>
@@ -18,12 +28,43 @@ export default function Uses({ reflect } : UsesProps) {
1828
{itemIDs && itemIDs.map((id) => {
1929
return (
2030
<div key={id}>
21-
{id}
31+
<Item
32+
reflect={reflect}
33+
itemID={id}
34+
/>
2235
</div>
2336
)
2437
})
2538
}
2639
</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}
2767
</div>
2868
)
2969
}
70+

0 commit comments

Comments
 (0)