Skip to content

Commit

Permalink
fix: update docStore after useNewDoc response
Browse files Browse the repository at this point in the history
  • Loading branch information
netchampfaris committed Jan 7, 2025
1 parent 0b5f409 commit 4bdb5f6
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/data-fetching/useNewDoc/useNewDoc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { reactive, unref } from 'vue'
import { useCall } from '../useCall/useCall'
import { UseCallOptions } from '../useCall/types'
import { docStore } from '../docStore'

type UseNewDocOptions = Omit<
UseCallOptions,
Expand All @@ -18,7 +19,11 @@ export function useNewDoc<T extends object>(
) {
let doc = reactive<NewDoc<T>>(initialValues)

const out = useCall<T>({
type DocResponse = T & {
name: string
}

const out = useCall<DocResponse>({
url: `/api/v2/document/${doctype}`,
method: 'POST',
params() {
Expand All @@ -34,8 +39,19 @@ export function useNewDoc<T extends object>(
...options,
})

function submit() {
return out
.submit()
.then((doc) =>
docStore
.setDoc({ doctype, ...(doc as DocResponse) })
.then(() => docStore.getDoc(doctype, doc.name.toString()).value as T),
)
}

return reactive({
...out,
submit,
doc,
})
}

0 comments on commit 4bdb5f6

Please sign in to comment.