Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/data-fetching/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { useDoc } from './useDoc/useDoc'
export { useDoctype } from './useDoctype/useDoctype'
export { useNewDoc } from './useNewDoc/useNewDoc'
export { useFrappeFetch } from './useFrappeFetch'
export { useBulkUpdate } from './useBulkUpdate/useBulkUpdate'
38 changes: 38 additions & 0 deletions src/data-fetching/useBulkUpdate/useBulkUpdate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @vitest-environment node
*/

import { ref } from 'vue'
import { baseUrl, waitUntilValueChanges } from '../../mocks/utils'
import { useBulkUpdate } from '../index'

describe.skip('useBulkUpdate', () => {
it('it returns expected object', async () => {
let bulkUpdate = useBulkUpdate({ baseUrl })

// Verify initial state
expect(bulkUpdate.data).toBe(null)
expect(bulkUpdate.error).toBe(null)
expect(typeof bulkUpdate.submit).toBe('function')

bulkUpdate.submit([
{ doctype: 'User', name: 'user3', email: 'user3@example.com' },
{ doctype: 'User', name: 'user4', email: 'user4@example.com' },
{ doctype: 'User', name: 'user5', email: 'user5@example.com' },
])

await waitUntilValueChanges(() => bulkUpdate.data)

console.log(bulkUpdate)

// Verify final state
expect(bulkUpdate.data).toStrictEqual([
{ doctype: 'User', name: 'user3', email: 'user3@example.com' },
{ doctype: 'User', name: 'user4', email: 'user4@example.com' },
{ doctype: 'User', name: 'user5', email: 'user5@example.com' },
])
expect(bulkUpdate.error).toBe(null)
expect(bulkUpdate.isFinished).toBe(true)
expect(bulkUpdate.loading).toBe(false)
})
})
52 changes: 52 additions & 0 deletions src/data-fetching/useBulkUpdate/useBulkUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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,
})
}
17 changes: 17 additions & 0 deletions src/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ export const handlers = [
})
},
),

http.post(url('/api/v2/method/bulk_update'), async ({ request }) => {
const body = (await request.json()) as any
const docs = body.docs || []

// For bulk update, documents should already have names
// Just return the updated documents as-is
const updatedDocs = docs.map((doc: any) => {
return {
...doc,
}
})

return HttpResponse.json({
data: updatedDocs,
})
}),
]

function getUsers(listParams) {
Expand Down