Skip to content

Commit

Permalink
feat: move accessibility check to after before hook is called
Browse files Browse the repository at this point in the history
  • Loading branch information
dziraf committed Jan 22, 2021
1 parent 7dc2e70 commit 58c809a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/backend/controllers/api-controller.ts
Expand Up @@ -152,9 +152,13 @@ class ApiController {
actionContext.record = record
const jsonWithRecord = await actionContext.action.handler(request, response, actionContext)

if (jsonWithRecord && jsonWithRecord.record && jsonWithRecord.record.recordActions) {
const isValidRecord = !!(jsonWithRecord && jsonWithRecord.record && jsonWithRecord.record.recordActions)
const anErrorWasHandled = jsonWithRecord && jsonWithRecord.notice && jsonWithRecord.notice.type === 'error'

if (isValidRecord || anErrorWasHandled) {
return jsonWithRecord
}

throw new ConfigurationError(
'handler of a recordAction should return a RecordJSON object',
'Action#handler',
Expand Down
2 changes: 1 addition & 1 deletion src/backend/decorators/action/action-decorator.ts
Expand Up @@ -86,8 +86,8 @@ class ActionDecorator {
context: ActionContext,
): Promise<any> {
try {
this.canInvokeAction(context)
const modifiedRequest = await this.invokeBeforeHook(request, context)
this.canInvokeAction(context)
const res = await this.invokeHandler(modifiedRequest, response, context)
return this.invokeAfterHook(res, modifiedRequest, context)
} catch (error) {
Expand Down
5 changes: 4 additions & 1 deletion src/frontend/components/routes/record-action.tsx
Expand Up @@ -33,6 +33,9 @@ const RecordAction: React.FC = () => {
setLoading(true)
api.recordAction(match.params).then((response) => {
setLoading(false)
if (response.data.notice && response.data.notice.type === 'error') {
addNotice(response.data.notice)
}
setRecord(response.data.record)
}).catch((error) => {
addNotice({
Expand Down Expand Up @@ -65,7 +68,7 @@ const RecordAction: React.FC = () => {
// Alternative approach would be to setRecord(undefined) before the fetch, but it is async and
// we cannot be sure that the component wont be rendered (it will be at least once) with the
// wrong data.
const hasDifferentRecord = record && record.id.toString() !== recordId
const hasDifferentRecord = record && record.id && record.id.toString() !== recordId

if (loading || hasDifferentRecord) {
const actionFromResource = resource.actions.find(r => r.name === actionName)
Expand Down
4 changes: 2 additions & 2 deletions src/locale/en.ts
Expand Up @@ -53,8 +53,8 @@ const translations = {
theseRecordsWillBeRemoved_plural: 'Following records will be removed',
pickSomeFirstToRemove: 'In order to remove records, you have to pick them first',
error404Resource: 'Resource of given id: {{resourceId}} cannot be found',
error404Action: 'Resource of given id: {{resourceId}} does not have an action with name: {{actionName}}',
error404Record: 'Resource of given id: {{resourceId}} does not have a record with id: {{recordId}}',
error404Action: 'Resource of given id: {{resourceId}} does not have an action with name: {{actionName}} or you are not authorized to use it!',
error404Record: 'Resource of given id: {{resourceId}} does not have a record with id: {{recordId}} or you are not authorized to use it!',
seeConsoleForMore: 'See development console for more details...',
noActionComponent: 'You have to implement action component for your Action',
noRecordsInResource: 'There are no records in this resource',
Expand Down

0 comments on commit 58c809a

Please sign in to comment.