-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3f03ae
commit 945c475
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |