Skip to content

Commit d0f02f2

Browse files
committed
fix(data): fix for #5156
1 parent 43b0d75 commit d0f02f2

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

src/fragments/lib/datastore/js/data-access/observe-update-snippet.mdx

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,36 @@ function App() {
2525
};
2626
}, []);
2727

28+
/**
29+
* Create a new Post
30+
*/
31+
async function onCreate() {
32+
const post = await DataStore.save(
33+
new Post({
34+
title: `New title ${Date.now()}`,
35+
rating: (function getRandomInt(min, max) {
36+
min = Math.ceil(min);
37+
max = Math.floor(max);
38+
return Math.floor(Math.random() * (max - min)) + min;
39+
})(1, 7),
40+
status: PostStatus.ACTIVE,
41+
})
42+
);
43+
setPost(post);
44+
}
45+
2846
return (
2947
<>
3048
<h1>{post?.title}</h1>
49+
<input type="button" value="NEW TODO" onClick={onCreate} />
3150
<input
51+
disabled={!post}
3252
type="text"
3353
value={post?.title ?? ""}
3454
onChange={({ target: { value } }) => {
3555
/**
3656
* Each keypress updates the post in local react state.
3757
*/
38-
if (!post) return;
3958
setPost(
4059
Post.copyOf(post, (draft) => {
4160
draft.title = value;
@@ -44,6 +63,7 @@ function App() {
4463
}}
4564
/>
4665
<input
66+
disabled={!post}
4767
type="button"
4868
value="Save"
4969
onClick={async () => {
@@ -87,10 +107,30 @@ function App() {
87107
};
88108
}, []);
89109

110+
/**
111+
* Create a new Post
112+
*/
113+
async function onCreate() {
114+
const post: Post = await DataStore.save<Post>(
115+
new Post({
116+
title: `New title ${Date.now()}`,
117+
rating: (function getRandomInt(min, max) {
118+
min = Math.ceil(min);
119+
max = Math.floor(max);
120+
return Math.floor(Math.random() * (max - min)) + min;
121+
})(1, 7),
122+
status: PostStatus.ACTIVE,
123+
})
124+
);
125+
setPost(post);
126+
}
127+
90128
return (
91129
<>
92130
<h1>{post?.title}</h1>
131+
<input type="button" value="NEW TODO" onClick={onCreate} />
93132
<input
133+
disabled={!post}
94134
type="text"
95135
value={post?.title ?? ""}
96136
onChange={({ target: { value } }) => {
@@ -105,6 +145,7 @@ function App() {
105145
}}
106146
/>
107147
<input
148+
disabled={!post}
108149
type="button"
109150
value="Save"
110151
onClick={async () => {

0 commit comments

Comments
 (0)