@sanity/sdk-react 2.14.0 → 2.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -29
- package/dist/index.d.ts +459 -335
- package/dist/index.js +22 -6
- package/dist/index.js.map +1 -1
- package/package.json +29 -31
- package/src/_exports/sdk-react.ts +1 -0
- package/src/components/auth/AuthBoundary.recovery.test.tsx +86 -0
- package/src/components/auth/AuthBoundary.tsx +11 -1
- package/src/hooks/document/useCreateDocument.test.tsx +83 -0
- package/src/hooks/document/useCreateDocument.ts +117 -0
package/README.md
CHANGED
|
@@ -108,6 +108,10 @@ const {data} = useQuery({
|
|
|
108
108
|
#### Document Manipulation
|
|
109
109
|
|
|
110
110
|
```tsx
|
|
111
|
+
// Create a new document and get back its handle
|
|
112
|
+
const createArticle = useCreateDocument({documentType: 'article'})
|
|
113
|
+
const newHandle = await createArticle({title: 'Untitled', status: 'draft'})
|
|
114
|
+
|
|
111
115
|
// Edit field (emits optimistic updates to useEditDocument listeners, creates a draft automatically)
|
|
112
116
|
const editTitle = useEditDocument({...handle, path: 'title'})
|
|
113
117
|
editTitle('New Title') // fires on every keystroke, debounced internally
|
|
@@ -133,13 +137,6 @@ await apply(publishDocument(handle))
|
|
|
133
137
|
|
|
134
138
|
// Batch actions
|
|
135
139
|
await apply([publishDocument(handle1), publishDocument(handle2), deleteDocument(handle3)])
|
|
136
|
-
|
|
137
|
-
// Create new document with an optional initial content
|
|
138
|
-
const newHandle = createDocumentHandle({
|
|
139
|
-
documentId: crypto.randomUUID(),
|
|
140
|
-
documentType: 'article',
|
|
141
|
-
})
|
|
142
|
-
await apply(createDocument(newHandle, {title: 'Untitled', status: 'draft'}))
|
|
143
140
|
```
|
|
144
141
|
|
|
145
142
|
#### Events & Permissions
|
|
@@ -183,41 +180,33 @@ The `useApplyDocumentActions` hook is used to perform document lifecycle operati
|
|
|
183
180
|
|
|
184
181
|
#### Creating Documents
|
|
185
182
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
1. Generate your own document ID (using `crypto.randomUUID()`)
|
|
189
|
-
2. Create a document handle with `createDocumentHandle`
|
|
190
|
-
3. Apply the `createDocument` action using the document handle, along with optional initial content
|
|
183
|
+
`useCreateDocument` handles the common case: it generates the document ID for you and returns the new document's handle, ready to pass to `useDocument`, `useEditDocument`, or your router.
|
|
191
184
|
|
|
192
185
|
```tsx
|
|
193
|
-
import {
|
|
186
|
+
import {useCreateDocument} from '@sanity/sdk-react'
|
|
194
187
|
|
|
195
188
|
function CreateArticleButton() {
|
|
196
|
-
const
|
|
189
|
+
const createArticle = useCreateDocument({documentType: 'article'})
|
|
197
190
|
|
|
198
|
-
const handleCreateArticle = () => {
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
191
|
+
const handleCreateArticle = async () => {
|
|
192
|
+
const handle = await createArticle({
|
|
193
|
+
title: 'New Article',
|
|
194
|
+
status: 'draft',
|
|
195
|
+
author: {_type: 'reference', _ref: 'author-123'},
|
|
203
196
|
})
|
|
204
197
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
title: 'New Article',
|
|
208
|
-
status: 'draft',
|
|
209
|
-
author: {_type: 'reference', _ref: 'author-123'},
|
|
210
|
-
}),
|
|
211
|
-
)
|
|
212
|
-
|
|
213
|
-
// Navigate to the new document
|
|
214
|
-
navigate(`/articles/${newId}`)
|
|
198
|
+
// Navigate to the new document using the returned handle
|
|
199
|
+
navigate(`/articles/${handle.documentId}`)
|
|
215
200
|
}
|
|
216
201
|
|
|
217
202
|
return <button onClick={handleCreateArticle}>Create Article</button>
|
|
218
203
|
}
|
|
219
204
|
```
|
|
220
205
|
|
|
206
|
+
To use a specific ID instead of a generated one, pass it on the handle (`useCreateDocument({documentType: 'article', documentId})`) or per call (`createArticle(initialValue, {documentId})`).
|
|
207
|
+
|
|
208
|
+
For atomic create-and-publish, or to create several documents in a single transaction, drop down to `useApplyDocumentActions` with the `createDocument` and `publishDocument` action creators (see [Batch Operations](#batch-operations) below).
|
|
209
|
+
|
|
221
210
|
#### Publishing Documents
|
|
222
211
|
|
|
223
212
|
```tsx
|