import { reactive, unref } from 'vue' import { useCall } from '../useCall/useCall' import { UseCallOptions } from '../useCall/types' import { docStore } from '../docStore' type UseNewDocOptions = Omit< UseCallOptions, 'url' | 'method' | 'params' | 'immediate' > type NewDoc = Partial< Omit > export function useNewDoc( doctype: string, initialValues: NewDoc = {}, options: UseNewDocOptions = {}, ) { let doc = reactive>(initialValues) type DocResponse = T & { name: string } const out = useCall({ url: `/api/v2/document/${doctype}`, method: 'POST', params() { let payload: Partial = {} for (let key in doc) { const typedKey = key as keyof T const value = (doc as Partial)[typedKey] payload[typedKey] = unref(value) } return payload }, immediate: false, ...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, }) }