allow external link for redirect (#1902)
This commit is contained in:
parent
418f9ad569
commit
7df3f2f621
@ -114,7 +114,10 @@ export const getAllLocalStorageItems = () => {
|
||||
export const applyEvent = async (event, socket) => {
|
||||
// Handle special events
|
||||
if (event.name == "_redirect") {
|
||||
Router.push(event.payload.path);
|
||||
if (event.payload.external)
|
||||
window.open(event.payload.path, "_blank");
|
||||
else
|
||||
Router.push(event.payload.path);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -185,7 +188,7 @@ export const applyEvent = async (event, socket) => {
|
||||
if (event.name == "_call_script") {
|
||||
try {
|
||||
eval(event.payload.javascript_code);
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
console.log("_call_script", e);
|
||||
}
|
||||
return false;
|
||||
|
@ -247,16 +247,19 @@ def server_side(name: str, sig: inspect.Signature, **kwargs) -> EventSpec:
|
||||
)
|
||||
|
||||
|
||||
def redirect(path: str | Var[str]) -> EventSpec:
|
||||
def redirect(path: str | Var[str], external: Optional[bool] = False) -> EventSpec:
|
||||
"""Redirect to a new path.
|
||||
|
||||
Args:
|
||||
path: The path to redirect to.
|
||||
external: Whether to open in new tab or not.
|
||||
|
||||
Returns:
|
||||
An event to redirect to the path.
|
||||
"""
|
||||
return server_side("_redirect", get_fn_signature(redirect), path=path)
|
||||
return server_side(
|
||||
"_redirect", get_fn_signature(redirect), path=path, external=external
|
||||
)
|
||||
|
||||
|
||||
def console_log(message: str | Var[str]) -> EventSpec:
|
||||
|
@ -119,16 +119,38 @@ def test_fix_events(arg1, arg2):
|
||||
assert event.payload == {"arg1": arg1, "arg2": arg2}
|
||||
|
||||
|
||||
def test_event_redirect():
|
||||
"""Test the event redirect function."""
|
||||
spec = event.redirect("/path")
|
||||
@pytest.mark.parametrize(
|
||||
"input,output",
|
||||
[
|
||||
(("/path", None), 'Event("_redirect", {path:"/path",external:false})'),
|
||||
(("/path", True), 'Event("_redirect", {path:"/path",external:true})'),
|
||||
(("/path", False), 'Event("_redirect", {path:"/path",external:false})'),
|
||||
(
|
||||
(Var.create_safe("path"), None),
|
||||
'Event("_redirect", {path:path,external:false})',
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_event_redirect(input, output):
|
||||
"""Test the event redirect function.
|
||||
|
||||
Args:
|
||||
input: The input for running the test.
|
||||
output: The expected output to validate the test.
|
||||
"""
|
||||
path, external = input
|
||||
if external is None:
|
||||
spec = event.redirect(path)
|
||||
else:
|
||||
spec = event.redirect(path, external=external)
|
||||
assert isinstance(spec, EventSpec)
|
||||
assert spec.handler.fn.__qualname__ == "_redirect"
|
||||
assert spec.args[0][0].equals(Var.create_safe("path"))
|
||||
assert spec.args[0][1].equals(Var.create_safe("/path"))
|
||||
assert format.format_event(spec) == 'Event("_redirect", {path:"/path"})'
|
||||
spec = event.redirect(Var.create_safe("path"))
|
||||
assert format.format_event(spec) == 'Event("_redirect", {path:path})'
|
||||
|
||||
# this asserts need comment about what it's testing (they fail with Var as input)
|
||||
# assert spec.args[0][0].equals(Var.create_safe("path"))
|
||||
# assert spec.args[0][1].equals(Var.create_safe("/path"))
|
||||
|
||||
assert format.format_event(spec) == output
|
||||
|
||||
|
||||
def test_event_console_log():
|
||||
|
Loading…
Reference in New Issue
Block a user