-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathuseBulkUpdate.ts
More file actions
52 lines (47 loc) · 1.28 KB
/
useBulkUpdate.ts
File metadata and controls
52 lines (47 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { reactive, readonly, ref, type Ref } from 'vue'
import { useCall } from '../useCall/useCall'
import { listStore } from '../useList/listStore'
import { docStore } from '../docStore'
export interface BulkUpdateOptions {
baseUrl?: string
}
export interface DocumentUpdate {
doctype: string
name: string
[key: string]: any
}
export function useBulkUpdate(options: BulkUpdateOptions = {}) {
const bulkUpdateCall = useCall<DocumentUpdate[], { docs: DocumentUpdate[] }>({
method: 'POST',
url: '/api/v2/method/bulk_update',
immediate: false,
refetch: false,
...options,
transform(data) {
return data.map((row) => ({
...row,
name: String(row.name),
}))
},
onSuccess(data) {
listStore.updateRows(data)
docStore.setDocs(data)
},
})
async function submit(docs: DocumentUpdate[]) {
return bulkUpdateCall.submit({ docs })
}
return reactive({
data: bulkUpdateCall.data,
error: bulkUpdateCall.error,
loading: bulkUpdateCall.loading,
isFinished: bulkUpdateCall.isFinished,
isFetching: bulkUpdateCall.isFetching,
canAbort: bulkUpdateCall.canAbort,
abort: bulkUpdateCall.abort,
aborted: bulkUpdateCall.aborted,
reset: bulkUpdateCall.reset,
execute: submit,
submit,
})
}