| import { Action, RecordActionResponse } from '../action.interface.js' |
| import NotFoundError from '../../utils/errors/not-found-error.js' |
| import populator from '../../utils/populator/populator.js' |
| import { paramConverter } from '../../../utils/param-converter/index.js' |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export const EditAction: Action<RecordActionResponse> = { |
| name: 'edit', |
| isVisible: true, |
| actionType: 'record', |
| icon: 'Edit', |
| showInDrawer: false, |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| handler: async (request, response, context) => { |
| const { record, resource, currentAdmin, h } = context |
| if (!record) { |
| throw new NotFoundError([ |
| `Record of given id ("${request.params.recordId}") could not be found`, |
| ].join('\n'), 'Action#handler') |
| } |
| if (request.method === 'get') { |
| return { record: record.toJSON(currentAdmin) } |
| } |
|
|
| const params = paramConverter.prepareParams(request.payload ?? {}, resource) |
| const newRecord = await record.update(params, context) |
| const [populatedRecord] = await populator([newRecord], context) |
|
|
| |
| context.record = populatedRecord |
|
|
| if (record.isValid()) { |
| return { |
| redirectUrl: h.resourceUrl({ resourceId: resource._decorated?.id() || resource.id() }), |
| notice: { |
| message: 'successfullyUpdated', |
| type: 'success', |
| }, |
| record: populatedRecord.toJSON(currentAdmin), |
| } |
| } |
| const baseMessage = populatedRecord.baseError?.message |
| || 'thereWereValidationErrors' |
| return { |
| record: populatedRecord.toJSON(currentAdmin), |
| notice: { |
| message: baseMessage, |
| type: 'error', |
| }, |
| } |
| }, |
| } |
|
|
| export default EditAction |
|
|