Skip to content

Commit

Permalink
feat: useNewDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
netchampfaris committed Jan 1, 2025
1 parent a3f03ae commit 945c475
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/data-fetching/useNewDoc/useNewDoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { reactive, unref } from 'vue'
import { useCall } from '../useCall/useCall'
import { UseCallOptions } from '../useCall/types'

type UseNewDocOptions = Omit<
UseCallOptions,
'url' | 'method' | 'params' | 'immediate'
>

type NewDoc<T> = Partial<
Omit<T, 'creation' | 'modified' | 'owner' | 'modified_by'>
>

export function useNewDoc<T extends object>(
doctype: string,
initialValues: NewDoc<T> = {},
options: UseNewDocOptions = {},
) {
let doc = reactive<NewDoc<T>>(initialValues)

const out = useCall<T>({
url: `/api/v2/document/${doctype}`,
method: 'POST',
params() {
let payload: Partial<T> = {}
for (let key in doc) {
const typedKey = key as keyof T
const value = (doc as Partial<T>)[typedKey]
payload[typedKey] = unref(value)
}
return payload
},
immediate: false,
...options,
})

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

0 comments on commit 945c475

Please sign in to comment.