Skip to content

Commit bd6f00c

Browse files
committed
Add support for patch APIs
1 parent 3f49405 commit bd6f00c

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

Diff for: src/components/AutoEditForm.vue

+47-4
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
function update(value:ApiRequest) {
115115
}
116116
117-
const { typeOf, typeProperties, getId, getPrimaryKey, Crud } = useAppMetadata()
117+
const { typeOf, apiOf, typeProperties, createFormLayout, getPrimaryKey, Crud } = useAppMetadata()
118118
119119
const typeName = computed(() => typeof props.type == 'string'
120120
? props.type
@@ -153,15 +153,58 @@
153153
154154
if (HttpMethods.hasRequestBody(method)) {
155155
let requestDto = new model.value.constructor()
156+
let pkValue = mapGet(props.modelValue, pk.name)
156157
let formData = new FormData(form)
157158
if (pk && !Array.from(formData.keys()).some(k => k.toLowerCase() == pk.name.toLowerCase())) {
158-
formData.append(pk.name, mapGet(props.modelValue, pk.name))
159+
formData.append(pk.name, pkValue)
159160
}
160161
162+
let reset:string[] = []
163+
const apiType = apiOf(typeName.value)
164+
if (apiType && Crud.isPatch(apiType)) {
165+
let origModel = sanitizeForUi(props.modelValue)
166+
let formLayout = createFormLayout(metaType.value)
167+
let dirtyValues:{[k:string]:any} = {}
168+
if (pk) dirtyValues[pk.name] = pkValue
169+
formLayout.forEach(input => {
170+
let id = input.id
171+
let origValue = mapGet(origModel, id)
172+
if (pk && pk.name.toLowerCase() === id.toLowerCase()) {
173+
return
174+
}
175+
let newValue = formData.get(id)
176+
let exists = newValue != null // only exists if checked
177+
let changed = input.type === 'checkbox'
178+
? exists !== !!origValue
179+
: input.type === 'file'
180+
? exists
181+
: newValue != origValue
182+
if (!newValue && !origValue) changed = false
183+
if (changed) {
184+
console.log('changed', id, {newValue, origValue})
185+
if (newValue) {
186+
dirtyValues[id] = newValue
187+
} else {
188+
if (input.type !== 'file') {
189+
reset.push(id)
190+
}
191+
}
192+
}
193+
})
194+
Array.from(formData.keys()).filter(k => !dirtyValues[k]).forEach(k => formData.delete(k))
195+
196+
let keys = Array.from(formData.keys()).filter(k => k.toLowerCase() != pk.name.toLowerCase())
197+
if (keys.length == 0 && reset.length == 0) {
198+
close()
199+
return
200+
}
201+
}
202+
203+
const args = reset.length > 0 ? { jsconfig: 'eccn', reset } : { jsconfig: 'eccn' }
161204
if (!returnsVoid) {
162-
api.value = await client.apiForm(requestDto, formData, { jsconfig: 'eccn' })
205+
api.value = await client.apiForm(requestDto, formData, args)
163206
} else {
164-
api.value = await client.apiFormVoid(requestDto, formData, { jsconfig: 'eccn' })
207+
api.value = await client.apiFormVoid(requestDto, formData, args)
165208
}
166209
} else {
167210
let fieldValues = formValues(form, typeProperties(metaType.value))

0 commit comments

Comments
 (0)