Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTMX and Alpine.js alongside helpful utilities, common js_entry point #35224

Merged
merged 30 commits into from
Oct 17, 2024
Merged
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e4cd0af
add HTMX to js dependencies
biyeun Oct 8, 2024
d9015fc
add Alpine.js to javascript dependencies
biyeun Oct 8, 2024
d649ef6
add HtmxActionMixin and support for hq-hx-action attribute
biyeun Oct 16, 2024
9418499
configure HTMX requests to include the CSRF token
biyeun Oct 16, 2024
40490ec
add a utility for triggering HTMX error modal
biyeun Oct 16, 2024
768a9c4
add utility for retrying HTMX requests
biyeun Oct 16, 2024
b10bad0
stitch everything together to create a common entry point for htmx + …
biyeun Oct 16, 2024
62c6a2e
add CacheStore helper model for HTMX demos to prototype app
biyeun Oct 16, 2024
9875e12
add example for using HTMX and Alpine with a simple Crispy Form
biyeun Oct 16, 2024
844d757
adds an example using HtmxActionMixin and alpine for inline editing
biyeun Oct 16, 2024
0c6e760
update the timeout to something more reasonable
biyeun Oct 16, 2024
66bacd3
feedback: make retryPathCounts a const
biyeun Oct 16, 2024
9f752fa
improvement: reorder file so that HtmxActionMixin comes first
biyeun Oct 16, 2024
599aafa
feedback: use constant instead of string with method='auto'
biyeun Oct 16, 2024
e3b5903
feedback: update naming of HtmxActionMixin and related
biyeun Oct 16, 2024
1db74ee
feedback: move Htmx debug tools into its own mixin
biyeun Oct 16, 2024
08a7e59
update response on errors to provide a reason so that HTMX displays t…
biyeun Oct 16, 2024
e694374
feedback: return forbidden response if action isn't callable
biyeun Oct 16, 2024
4b9ce1b
feedback: not every HTMX evt is the same
biyeun Oct 16, 2024
fbb587f
feedback: only retry on GET timeouts
biyeun Oct 16, 2024
796cade
feedback: clarify error codes, messages, and timeout units
biyeun Oct 16, 2024
c8660f6
feedback: default_value is renamed to initial_value
biyeun Oct 16, 2024
59a92be
improvement: add help text to hint what the operative action is
biyeun Oct 16, 2024
ab542e2
rename templates and make paths shorter for htmx examples so that it'…
biyeun Oct 16, 2024
8002cf3
update comments
biyeun Oct 16, 2024
e0f7c90
add django ace language support to styleguide
biyeun Oct 16, 2024
d30e7c4
also disable checkbox when isSubmitting is true
biyeun Oct 16, 2024
cba4207
add styleguide section for htmx and alpine
biyeun Oct 16, 2024
29a4d31
feedback: clarify timeout units and value
biyeun Oct 16, 2024
17517be
Merge branch 'master' of github.com:dimagi/commcare-hq into bmb/htmx-…
biyeun Oct 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import htmx from 'htmx.org';

const DEFAULT_MAX_RETRIES = 20;
let retryPathCounts = {};
biyeun marked this conversation as resolved.
Show resolved Hide resolved

/**
* Retries an HTMX request up to a specified max retry count.
*
* @param {Object} evt - The HTMX event object containing details about the request.
* @param {number} [maxRetries=DEFAULT_MAX_RETRIES] - The maximum number of retries allowed.
* @returns {boolean} - Returns `false` if max retries are exceeded, otherwise `true`.
*/
const retryHtmxRequest = (evt, maxRetries = DEFAULT_MAX_RETRIES) => {
// Extract values from the HTMX event
const replaceUrl = evt.detail.elt.getAttribute('hx-replace-url');
const requestPath = evt.detail.pathInfo.finalRequestPath;

// Initialize retry count if necessary
retryPathCounts[requestPath] = retryPathCounts[requestPath] || 0;
retryPathCounts[requestPath]++;

// Return false if the max number of retries for that path has been exceeded
if (retryPathCounts[requestPath] > maxRetries) {
return false;
}

// Prepare the context for the htmx request
const context = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does htmx have a way of constructing its own request that we could use here? The answer could be "no"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated the signature of the function to make things easier to read. but, yes, no easy retry option with HTMX sadly

source: evt.detail.elt,
target: evt.detail.requestConfig.target,
swap: evt.detail.elt.getAttribute('hx-swap'),
headers: evt.detail.requestConfig.headers,
values: JSON.parse(evt.detail.elt.getAttribute('hx-vals')),
};

// Make the htmx request and handle URL update if necessary
htmx.ajax(evt.detail.requestConfig.verb, requestPath, context).then(() => {
if (replaceUrl === 'true') {
window.history.pushState(null, '', requestPath);
} else if (replaceUrl) {
const newUrl = `${window.location.origin}${window.location.pathname}${replaceUrl}`;
window.history.pushState(null, '', newUrl);
}
delete retryPathCounts[requestPath];
});

return true;
};

export default retryHtmxRequest;