hammady / wwpray

A website that shows the prayer times for a preconfigured list of masjids in a tabular format. Visitors can subscribe for email notifications.
https://wwpray.net
MIT License
1 stars 0 forks source link

Support for Zero-JS Form Submissions with Query Params for Success and Error Messages #21

Closed fayez-nazzal closed 1 year ago

fayez-nazzal commented 1 year ago

We are willing not to require JS for users to use the website. We only use JS to enhance the experience, but the basic functionality isn't JS dependent. So, we are not using AJAX requests. We rely on HTML form elements.

The subscription endpoint is a POST method that takes email and topics as query parameters and returns a FormData payload with "message" and "topics" properties.

We use a form element to handle subscriptions. Here is a minimized version of the code:

<form
    method="POST"
    action={SUBSCRIPTIONS_BASE_URL}
>
    <input type="email" name="email" />
    <input type="checkbox" name="topics" value="MNNexus" />
    <input type="checkbox" name="topics" value="Shalimar" />
    <button type="submit"> Subscribe </button>
</form>

Upon submission, this will be done:

To enhance compatibility, the following changes could be made to SUBSCRIPTIONS_BASE_URL:

hammady commented 1 year ago

@fayez-nazzal thanks for the detailed explanation.

Change the endpoint method to GET, or read data as FormData instead of query params.

Will support both GET and POST. The recommended method is POST, and that's what we should use in the JS version. Reading FormData in Python does not seem clean using the simple http server, not worth the migration to Flask or an advanced server.

If subscription is successful, redirect to back to the original page with message= and topics= query params If subscription failed, redirect with error_message= query param.

The server can check for the request header Accept: application/json and if found will return JSON response, otherwise, it will redirect back to the referrer (found in the Referrer request header) with the above parameters. If no referrer found, it will return an HTML response. Does this sound good?

fayez-nazzal commented 1 year ago

Cool @hammady, I will check.

fayez-nazzal commented 1 year ago

@hammady Is support for GET request implemented?

hammady commented 1 year ago

@fayez-nazzal finalizing the PR.