Skip to content

Commit

Permalink
client1 + client2: Fix onsubmit event handler to allow submitting
Browse files Browse the repository at this point in the history
According to the documentation
(https://lona-web.org/1.x/api-reference/views.html#post ) it should be
possible to submit a `<form>` using `POST` to an interactive lona view.
Without this change this fails with a
`Uncaught ReferenceError: lona_window is not defined` in the browser.

This change fixes the missing reference and allows to use the `POST` method
to submit data to the view.

(Also add a test to make sure a POSTed `<form>` still works in the
future.)

Signed-off-by: Chris Fiege <cfi@pengutronix.de>
  • Loading branch information
SmithChart committed Sep 5, 2023
1 parent 7f8ba4c commit 3bbfd64
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lona/client/_lona/client/input-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ export class LonaInputEventHandler {
};

_patch_onsubmit(node) {
var lona_window = this.lona_window;

node.onsubmit = function(event) {
event.preventDefault();
event.stopPropagation();
Expand Down
2 changes: 2 additions & 0 deletions lona/client2/_lona/client2/input-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ export class LonaInputEventHandler {
};

_patch_onsubmit(node) {
var lona_window = this.lona_window;

node.onsubmit = function(event) {
event.preventDefault();
event.stopPropagation();
Expand Down
60 changes: 60 additions & 0 deletions tests/test_0305_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
async def test_post_requests(lona_app_context):
"""
This test checks if <form>-requests as GET and POST work as intended.
"""

import json

from playwright.async_api import async_playwright

from lona.html import TextInput, Submit, HTML, Form, Div
from lona import View

def setup_app(app):

@app.route('/')
class Index(View):
def handle_request(self, request):
return HTML(
Div(request.method, _id='method'),
Div(json.dumps(request.POST), _id='post-data'),
Form(
TextInput(name='input-1'),
Submit(),
method='POST',
),
)

context = await lona_app_context(setup_app)

async def get_method(page):
element = page.locator('#method')

return (await element.inner_html())

async def get_post_data(page):
element = page.locator('#post-data')
json_string = await element.inner_html()

return json.loads(json_string)

async with async_playwright() as p:
browser = await p.chromium.launch()
browser_context = await browser.new_context()
page = await browser_context.new_page()

# GET request
await page.goto(context.make_url('/'))
await page.wait_for_url('/')

assert await get_method(page) == 'GET'
assert await get_post_data(page) == {}

# POST request
await page.locator('input[name=input-1]').fill('foo')
await page.click('input[type=submit]')

await page.wait_for_url('/')

assert await get_method(page) == 'POST'
assert await get_post_data(page) == {'input-1': 'foo'}

0 comments on commit 3bbfd64

Please sign in to comment.