Skip to content

Redirect Handling in a Dynamic Form Component #832

Closed

Description

@amrutadotorg
Screen.Recording.2025-02-27.at.10.31.59.mov

Hi, I would like to know if my redirect implementation in the form component with dynamic options is correct. In the attached video, you can see that when I abort the form submission, I get redirected to handle_redirect.sql. Is this expected behavior, or could this be a bug?

thank you

see live https://coolbreeze.org/

select 'form' as component,
    TRUE as auto_submit;
    'handle_redirect.sql' as action;  -- This is where the form submits

select 
    'selected_country' as name,
    'select' as type,
    (
        SELECT COALESCE(
            (SELECT translation_value FROM language 
             WHERE language_code = $lang 
             AND translation_key = 'country.form_label'),
            (SELECT translation_value FROM language 
             WHERE language_code = 'en' 
             AND translation_key = 'country.form_label')
        )
    ) as label,
    'options_source.sql' as options_source,
    TRUE as searchable;


-- handle_redirect.sql
select 'redirect' as component,
    -- Use the selected URL from the form submission
    COALESCE(:selected_country, 'index.sql') as link;

-- options_source.sql
select 'json' as component;
set sahaj_links = sqlpage.fetch('https://amrutasahajmaterials.amruta.org/map_json/sahaj_links.json');
select 
    json_extract(value, '$.url') as value,
    CASE
        WHEN json_extract(value, '$.native_country_name') = json_extract(value, '$.country_name')
        THEN json_extract(value, '$.country_name') || ' ⤿ ' || json_extract(value, '$.clean_url')
        ELSE json_extract(value, '$.native_country_name') || ' (' || json_extract(value, '$.country_name') || ')' || ' ⤿ ' || json_extract(value, '$.clean_url')
    END as label
from json_each($sahaj_links->'links')
where lower(json_extract(value, '$.country_name')) like '%' || lower($search) || '%'
   or lower(json_extract(value, '$.native_country_name')) like '%' || lower($search) || '%';

Activity

  1. added
    bug
    on Feb 27, 2025
  2. lovasoa commented on Feb 27, 2025

    @lovasoa
    Collaborator

    Hello ! I think your implementation is fine. When you manually abort the loading just at the right time, then you end up on the intermediate handle_redirect page, but I don't think this is an issue for your users, is it ?

  3. amrutadotorg commented on Feb 27, 2025

    @amrutadotorg
    ContributorAuthor

    It’s hard to say how many users will not click the link. The timing does not matter; the redirection to handle_redirect.sql happens whenever a user does not select an option from the dropdown menu. I think in such a case, COALESCE(:selected_country, 'index.sql') should be triggered to index,sql, but it isn’t.

  4. lovasoa commented on Feb 27, 2025

    @lovasoa
    Collaborator

    Oh, I see, sorry ! Indeed, the change is triggered even when the value did not actually change.

  5. lovasoa commented on Feb 27, 2025

    @lovasoa
    Collaborator

    In the meantime, you can redirect back to the home page from handle_redirect.sql when no value was submitted.

    select 'redirect' as component, '/' as link where COALESCE(:selected_country, '') = '';
  6. 81reap commented on Oct 29, 2026

    @81reap
    Collaborator

    I don't think this is an issue anymore

    reproduction

    $ cat tests/end-to-end/fixtures/repro_832/index.sql 
    SELECT 'form' AS component, TRUE AS auto_submit, 'submitted.sql' AS action;
    SELECT 'country' AS name, 'select' AS type, TRUE AS dropdown, 'FR' AS value, '[{"label":"France","value":"FR"},{"label":"Spain","value":"ES"}]' AS options;
    
    $ cat tests/end-to-end/fixtures/repro_832/submitted.sql 
    SELECT 'text' AS component, 'Submitted ' || :country AS contents;
    
    $ cat tests/end-to-end/fixtures/repro_832/test.ts 
    import { expect, test } from "../../fixture.ts";
    
    test("closing an auto-submit dropdown without choosing does not submit", async ({ page }) => {
      await page.locator(".ts-control").click();
      await expect(page.locator(".ts-dropdown .option", { hasText: "Spain" })).toBeVisible();
      await page.keyboard.press("Escape");
      await page.locator("body").click({ position: { x: 5, y: 5 } });
      await page.waitForLoadState("networkidle");
      await expect(page).toHaveURL(/\/repro_832\/$/);
    });
    
    test("choosing another option in an auto-submit dropdown submits", async ({ page }) => {
      await page.locator(".ts-control").click();
      await page.locator(".ts-dropdown .option", { hasText: "Spain" }).click();
      await expect(page.getByText("Submitted ES")).toBeVisible();
    });
    
    test("choosing the selected option again in an auto-submit dropdown does not submit", async ({ page }) => {
      await page.locator(".ts-control").click();
      await page.locator(".ts-dropdown .option", { hasText: "France" }).click();
      await page.waitForLoadState("networkidle");
      await expect(page).toHaveURL(/\/repro_832\/$/);
    });
    repro_832 postgres
    v0.33.0 ✔ passed
    v0.47.0 ✔ passed
    tree ✔ passed

    On commit 616df27: nix run .#reproduce -- repro_832 postgres v0.33.0 v0.47.0 tree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions