Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Check prefill on interval instead on event for CRM enterprise plan edit (#4237)

Adrian Gruntkowski2eed1fd913c1
diff --git a/lib/plausible/crm_extensions.ex b/lib/plausible/crm_extensions.ex
index 96d3427e9e..c4d082b668 100644
--- a/lib/plausible/crm_extensions.ex
+++ b/lib/plausible/crm_extensions.ex
@@ -49,32 +49,31 @@ defmodule Plausible.CrmExtensions do
Phoenix.HTML.raw("""
<script type="text/javascript">
(async () => {
+ const CHECK_INTERVAL = 300
const userIdField = document.getElementById("enterprise_plan_user_id") || document.getElementById("user_id")
let planRequest
let lastValue = Number(userIdField.value)
- let scheduledCheck
+ let currentValue = lastValue
- userIdField.addEventListener("change", async () => {
- if (scheduledCheck) clearTimeout(scheduledCheck)
+ setTimeout(prefillCallback, CHECK_INTERVAL)
- scheduledCheck = setTimeout(async () => {
- const currentValue = Number(userIdField.value)
- if (Number.isInteger(currentValue)
- && currentValue > 0
- && currentValue != lastValue
- && !planRequest) {
- planRequest = await fetch("/crm/billing/user/" + currentValue + "/current_plan")
- const result = await planRequest.json()
+ async function prefillCallback() {
+ currentValue = Number(userIdField.value)
+ if (Number.isInteger(currentValue)
+ && currentValue > 0
+ && currentValue != lastValue
+ && !planRequest) {
+ planRequest = await fetch("/crm/billing/user/" + currentValue + "/current_plan")
+ const result = await planRequest.json()
- fillForm(result)
+ fillForm(result)
- lastValue = currentValue
- planRequest = null
- }
- }, 300)
- })
+ lastValue = currentValue
+ planRequest = null
+ }
- userIdField.dispatchEvent(new Event("change"))
+ setTimeout(prefillCallback, CHECK_INTERVAL)
+ }
function fillForm(result) {
[