Skip to content

Commit

Permalink
feat: add @errors edge tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Harminder Virk authored and Harminder Virk committed Mar 19, 2024
1 parent d5c8e05 commit 9dd5bb1
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/plugins/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,54 @@ export const edgePluginSession: PluginFn<undefined> = (edge) => {
buffer.writeStatement(`}`, token.filename, token.loc.start.line)
},
})

edge.registerTag({
tagName: 'errors',
seekable: true,
block: true,
compile(parser, buffer, token) {
/**
* Write an if statement
*/
buffer.writeStatement(
`if (state.flashMessages.has('errorsBag')) {`,
token.filename,
token.loc.start.line
)

/**
* Define a local variable
*/
buffer.writeExpression(
`let $messages = state.flashMessages.get('errorsBag')`,
token.filename,
token.loc.start.line
)

/**
* Create a local variables scope and tell the parser about
* the existence of the "messages" variable
*/
parser.stack.defineScope()
parser.stack.defineVariable('$messages')

/**
* Process component children using the parser
*/
token.children.forEach((child) => {
parser.processToken(child, buffer)
})

/**
* Clear the scope of the local variables before we
* close the if statement
*/
parser.stack.clearScope()

/**
* Close if statement
*/
buffer.writeStatement(`}`, token.filename, token.loc.start.line)
},
})
}
56 changes: 56 additions & 0 deletions tests/session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1419,4 +1419,60 @@ test.group('Session | Flash', (group) => {
['<p> Access denied </p>', '']
)
})

test('access errorsBag using the @errors tag', async ({ assert }) => {
let sessionId: string | undefined

edge.registerTemplate('flash_errors_messages', {
template: `
@errors()
<div>
@each(message in $messages)
<p> {{ message }} </p>
@end
</div>
@else
<p> No error messages </p>
@end
`,
})

const server = httpServer.create(async (req, res) => {
const request = new RequestFactory().merge({ req, res, encryption }).create()
const response = new ResponseFactory().merge({ req, res, encryption }).create()
const ctx = new HttpContextFactory().merge({ request, response }).create()

const session = new Session(sessionConfig, cookieDriver, emitter, ctx)
await session.initiate(false)
sessionId = session.sessionId

if (request.url() === '/prg') {
response.send(await ctx.view.render('flash_errors_messages'))
await session.commit()
response.finish()
} else {
session.flashErrors({
E_ACCESS_DENIED: 'Access denied',
})
await session.commit()
}

response.finish()
})

const { headers } = await supertest(server).get('/')
const cookies = setCookieParser.parse(headers['set-cookie'], { map: true })

const { text } = await supertest(server)
.get('/prg')
.set(
'Cookie',
`adonis_session=${cookies.adonis_session.value}; ${sessionId}=${cookies[sessionId!].value}`
)

assert.deepEqual(
text.split('\n').map((line) => line.trim()),
['<div>', '<p> Access denied </p>', '</div>', '']
)
})
})

0 comments on commit 9dd5bb1

Please sign in to comment.