|
114 | 114 | function update(value:ApiRequest) {
|
115 | 115 | }
|
116 | 116 |
|
117 |
| - const { typeOf, typeProperties, getId, getPrimaryKey, Crud } = useAppMetadata() |
| 117 | + const { typeOf, apiOf, typeProperties, createFormLayout, getPrimaryKey, Crud } = useAppMetadata() |
118 | 118 |
|
119 | 119 | const typeName = computed(() => typeof props.type == 'string'
|
120 | 120 | ? props.type
|
|
153 | 153 |
|
154 | 154 | if (HttpMethods.hasRequestBody(method)) {
|
155 | 155 | let requestDto = new model.value.constructor()
|
| 156 | + let pkValue = mapGet(props.modelValue, pk.name) |
156 | 157 | let formData = new FormData(form)
|
157 | 158 | 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) |
159 | 160 | }
|
160 | 161 |
|
| 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' } |
161 | 204 | if (!returnsVoid) {
|
162 |
| - api.value = await client.apiForm(requestDto, formData, { jsconfig: 'eccn' }) |
| 205 | + api.value = await client.apiForm(requestDto, formData, args) |
163 | 206 | } else {
|
164 |
| - api.value = await client.apiFormVoid(requestDto, formData, { jsconfig: 'eccn' }) |
| 207 | + api.value = await client.apiFormVoid(requestDto, formData, args) |
165 | 208 | }
|
166 | 209 | } else {
|
167 | 210 | let fieldValues = formValues(form, typeProperties(metaType.value))
|
|
0 commit comments