Skip to content

Commit b868102

Browse files
committed
add delete msg to guestbook
1 parent e702f0e commit b868102

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

GUIDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
SQLSync is distributed as a JavaScript package as well as a Rust Crate. Currently, both are required to use SQLSync. Also, React is the only supported framework at the moment.
77

8-
If you want to jump ahead to a working demo, [check out the finished product here][guestbook-src]
8+
If you want to jump ahead to a working demo, [check out the finished product here][guestbook-src]. Note that it has a couple more features than what you get with this guide.
99

1010
[guestbook-src]: examples/guestbook-react
1111

examples/guestbook-react/src/doctype.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export type Mutation =
1515
tag: "AddMessage";
1616
id: string;
1717
msg: string;
18+
}
19+
| {
20+
tag: "DeleteMessage";
21+
id: string;
1822
};
1923

2024
export const TaskDocType: DocType<Mutation> = {

examples/guestbook-react/src/main.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ export function App() {
6262
[mutate, msg, setMsg],
6363
);
6464

65+
// create a callback to delete a message
66+
const handleDelete = useCallback(
67+
(id: string) => {
68+
mutate({ tag: "DeleteMessage", id }).catch((err) => {
69+
console.error("Failed to delete message", err);
70+
});
71+
},
72+
[mutate],
73+
);
74+
6575
// finally, query SQLSync for all the messages, sorted by created_at
6676
const { rows } = useQuery<{ id: string; msg: string }>(
6777
DOC_ID,
@@ -76,7 +86,12 @@ export function App() {
7686
<h1>Guestbook:</h1>
7787
<ul>
7888
{(rows ?? []).map(({ id, msg }) => (
79-
<li key={id}>{msg}</li>
89+
<li key={id}>
90+
{msg}
91+
<button type="button" onClick={() => handleDelete(id)} style={{ marginLeft: "40px" }}>
92+
remove msg
93+
</button>
94+
</li>
8095
))}
8196
</ul>
8297
<h3>Leave a message:</h3>

examples/reducer-guestbook/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use sqlsync_reducer::{execute, init_reducer, types::ReducerError};
66
enum Mutation {
77
InitSchema,
88
AddMessage { id: String, msg: String },
9+
DeleteMessage { id: String },
910
}
1011

1112
init_reducer!(reducer);
@@ -33,6 +34,10 @@ async fn reducer(mutation: Vec<u8>) -> Result<(), ReducerError> {
3334
)
3435
.await?;
3536
}
37+
38+
Mutation::DeleteMessage { id } => {
39+
execute!("delete from messages where id = ?", id).await?;
40+
}
3641
}
3742

3843
Ok(())

0 commit comments

Comments
 (0)