File size: 1,831 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
import { Action, BulkActionResponse } from '../action.interface.js'
import NotFoundError from '../../utils/errors/not-found-error.js'

/**
 * @implements Action
 * @category Actions
 * @module BulkDeleteAction
 * @description
 * Removes given records from the database.
 * @private
 */
export const BulkDeleteAction: Action<BulkActionResponse> = {
  name: 'bulkDelete',
  isVisible: true,
  actionType: 'bulk',
  icon: 'Trash2',
  showInDrawer: true,
  variant: 'danger',
  /**
   * Responsible for deleting existing records.
   *
   * To invoke this action use {@link ApiClient#bulkAction}
   * with {actionName: _bulkDelete_}
   *
   * @return  {Promise<BulkActionResponse>}
   * @implements ActionHandler
   * @memberof module:BulkDeleteAction
   */
  handler: async (request, response, context) => {
    const { records, resource, h } = context

    if (!records || !records.length) {
      throw new NotFoundError('no records were selected.', 'Action#handler')
    }
    if (request.method === 'get') {
      const recordsInJSON = records.map((record) => record.toJSON(context.currentAdmin))
      return {
        records: recordsInJSON,
      }
    }
    if (request.method === 'post') {
      await Promise.all(records.map((record) => resource.delete(record.id(), context)))
      return {
        records: records.map((record) => record.toJSON(context.currentAdmin)),
        notice: {
          message: records.length > 1 ? 'successfullyBulkDeleted_plural' : 'successfullyBulkDeleted',
          options: { count: records.length },
          resourceId: resource.id(),
          type: 'success',
        },
        redirectUrl: h.resourceUrl({ resourceId: resource._decorated?.id() || resource.id() }),
      }
    }
    throw new Error('method should be either "post" or "get"')
  },
}

export default BulkDeleteAction