2022-01-05 11:34:43 +01:00
|
|
|
import { useRef, useCallback, useMemo, useState } from 'react';
|
|
|
|
import { fire } from '@utils';
|
2021-06-09 17:13:26 +02:00
|
|
|
|
2022-02-08 12:49:51 +01:00
|
|
|
export function useDeferredSubmit(input?: HTMLInputElement): {
|
|
|
|
(callback: () => void): void;
|
|
|
|
done: () => void;
|
|
|
|
} {
|
2021-06-09 17:13:26 +02:00
|
|
|
const calledRef = useRef(false);
|
|
|
|
const awaitFormSubmit = useCallback(
|
2022-02-08 12:49:51 +01:00
|
|
|
(callback: () => void) => {
|
2021-07-06 15:06:38 +02:00
|
|
|
const form = input?.form;
|
2021-06-09 17:13:26 +02:00
|
|
|
if (!form) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-08 12:49:51 +01:00
|
|
|
const interceptFormSubmit = (event: Event) => {
|
2021-06-09 17:13:26 +02:00
|
|
|
event.preventDefault();
|
|
|
|
runCallback();
|
2023-01-24 09:22:04 +01:00
|
|
|
|
|
|
|
if (
|
2023-01-30 18:22:53 +01:00
|
|
|
!Array.from(form.elements).some(
|
|
|
|
(e) =>
|
|
|
|
e.hasAttribute('data-direct-upload-url') &&
|
|
|
|
'value' in e &&
|
|
|
|
e.value != ''
|
2023-01-24 09:22:04 +01:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
form.submit();
|
|
|
|
}
|
|
|
|
// else: form will be submitted by diret upload once file have been uploaded
|
2021-06-09 17:13:26 +02:00
|
|
|
};
|
|
|
|
calledRef.current = false;
|
|
|
|
form.addEventListener('submit', interceptFormSubmit);
|
|
|
|
const runCallback = () => {
|
|
|
|
form.removeEventListener('submit', interceptFormSubmit);
|
|
|
|
clearTimeout(timer);
|
|
|
|
if (!calledRef.current) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const timer = setTimeout(runCallback, 400);
|
|
|
|
},
|
|
|
|
[input]
|
|
|
|
);
|
2022-02-08 12:49:51 +01:00
|
|
|
const done = () => {
|
2021-06-09 17:13:26 +02:00
|
|
|
calledRef.current = true;
|
|
|
|
};
|
2022-02-08 12:49:51 +01:00
|
|
|
return Object.assign(awaitFormSubmit, { done });
|
2021-06-09 17:13:26 +02:00
|
|
|
}
|
2022-01-05 11:34:43 +01:00
|
|
|
|
2022-02-08 12:49:51 +01:00
|
|
|
export function groupId(id: string) {
|
2024-03-19 16:16:45 +01:00
|
|
|
return `#${id.replace(/-input$/, '')}`;
|
2022-01-05 11:34:43 +01:00
|
|
|
}
|
|
|
|
|
2022-02-08 12:49:51 +01:00
|
|
|
export function useHiddenField(
|
|
|
|
group?: string,
|
|
|
|
name = 'value'
|
|
|
|
): [
|
|
|
|
value: string | undefined,
|
|
|
|
setValue: (value: string) => void,
|
|
|
|
input: HTMLInputElement | undefined
|
|
|
|
] {
|
2022-01-05 11:34:43 +01:00
|
|
|
const hiddenField = useMemo(
|
|
|
|
() => selectInputInGroup(group, name),
|
|
|
|
[group, name]
|
|
|
|
);
|
|
|
|
const [value, setValue] = useState(() => hiddenField?.value);
|
|
|
|
|
|
|
|
return [
|
|
|
|
value,
|
|
|
|
(value) => {
|
|
|
|
if (hiddenField) {
|
|
|
|
hiddenField.setAttribute('value', value);
|
|
|
|
setValue(value);
|
2022-05-06 19:52:55 +02:00
|
|
|
fire(hiddenField, 'change');
|
2022-01-05 11:34:43 +01:00
|
|
|
}
|
|
|
|
},
|
2022-02-08 12:49:51 +01:00
|
|
|
hiddenField ?? undefined
|
2022-01-05 11:34:43 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-02-08 12:49:51 +01:00
|
|
|
function selectInputInGroup(
|
|
|
|
group: string | undefined,
|
|
|
|
name: string
|
|
|
|
): HTMLInputElement | undefined | null {
|
2022-01-05 11:34:43 +01:00
|
|
|
if (group) {
|
2022-02-08 12:49:51 +01:00
|
|
|
return document.querySelector<HTMLInputElement>(
|
2022-01-05 11:34:43 +01:00
|
|
|
`${group} input[type="hidden"][name$="[${name}]"], ${group} input[type="hidden"][name="${name}"]`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|