berttaylor / collabl.io

Django/Python Web Application for Collaborate Goal Tracking & Task Management
GNU General Public License v3.0
0 stars 0 forks source link

Improve HTMX Modal Logic #66

Closed berttaylor closed 2 years ago

berttaylor commented 2 years ago

I have developed a cleaner, more readable way of using HTMX for modals.

  1. On get request, the View should return a modal only, which is rendered to hx-target="#modals-here"
  2. On post, we also set hx-target="#modals-here"
  3. If the POST data is invalid, then we send the modal back with the appropriate guidance
  4. If the POST data is valid, We preocess the request and send back a small piece of htmx (kept as a string in the view) to refresh the state of any objects that have changed, and clear any forms. Examples of this is below:

Clears text input htmx = '<input id="new_tag_text_input" hx-swap-oob="true" type="text" name="new_question_tag">'

Refresh the state htmx = f'<div hx-get="{category_options_endpoint}" hx-trigger="load" hx-target="#id_category" hx-swap="innerHTML"></div>' (NOTE: Refreshing the state like this will result in an extra request, but I feel for this project, this is worthwhile for the simplicity of keeping the HTMX rendering in one place only- the #modals-here div )