|
import { populator } from '../../utils/populator/index.js' |
|
import { paramConverter } from '../../../utils/param-converter/index.js' |
|
import { Action, RecordActionResponse } from '../action.interface.js' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const NewAction: Action<RecordActionResponse> = { |
|
name: 'new', |
|
isVisible: true, |
|
actionType: 'resource', |
|
icon: 'Plus', |
|
showInDrawer: false, |
|
variant: 'primary', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handler: async (request, response, context) => { |
|
const { resource, h, currentAdmin } = context |
|
if (request.method === 'post') { |
|
const params = paramConverter.prepareParams(request.payload ?? {}, resource) |
|
|
|
let record = await resource.build(params) |
|
|
|
record = await record.create(context) |
|
const [populatedRecord] = await populator([record], context) |
|
|
|
|
|
context.record = populatedRecord |
|
|
|
if (record.isValid()) { |
|
return { |
|
redirectUrl: h.resourceUrl({ resourceId: resource._decorated?.id() || resource.id() }), |
|
notice: { |
|
message: 'successfullyCreated', |
|
type: 'success', |
|
}, |
|
record: record.toJSON(currentAdmin), |
|
} |
|
} |
|
const baseMessage = populatedRecord.baseError?.message |
|
|| 'thereWereValidationErrors' |
|
return { |
|
record: record.toJSON(currentAdmin), |
|
notice: { |
|
message: baseMessage, |
|
type: 'error', |
|
}, |
|
} |
|
} |
|
|
|
throw new Error('new action can be invoked only via `post` http method') |
|
}, |
|
} |
|
|
|
export default NewAction |
|
|