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

It turns out Kaffy uses pretty crude solution for large select widget, where
jQuery's `.val(value)` is called on input element. This in turn does not trigger
any events and the behavior is not extensible in any sane way. The prefill
callback is now called periodically to ensure any change in the input gets
picked up and acted upon eventually when conditions are met.
This commit is contained in:
Adrian Gruntkowski 2024-06-17 15:01:22 +02:00 committed by GitHub
parent f9a4cc784d
commit 2eed1fd913
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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) {
[