Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | 6x 6x 287x 287x 287x 27x 287x 12x 287x 12x 287x 12x 12x 287x 12x 12x 287x 287x 287x | import { useCallback } from "react";
import { useMutation, useQuery } from "react-query";
import { customInstance } from "@services/api";
import { useGetMerchantById } from "@hooks/enterprise-api/account/useGetMerchants";
import { LEGAL_DOC_KEYS } from "./keys";
import type { DocumentLock } from "../types";
const LOCK_POLL_INTERVAL_MS = 120_000;
type UseDocumentLockOptions = { enabled?: boolean };
/**
* Polls the edit-lock for a document (every 2 minutes) and exposes helpers to
* keep the lock alive (heartbeat) and release it. `isLockedByOther` is true
* when someone else currently holds the lock.
*/
export const useDocumentLock = (
documentID?: number | string,
{ enabled = true }: UseDocumentLockOptions = {},
) => {
const { data: merchant } = useGetMerchantById();
const accID = merchant?.accID;
const lockQuery = useQuery<DocumentLock>(
[LEGAL_DOC_KEYS.lock, accID, documentID],
() =>
customInstance({
url: `/merchants/${accID}/legal-documents/${documentID}/lock`,
method: "GET",
}),
{
enabled: enabled && !!accID && !!documentID,
refetchOnWindowFocus: false,
refetchInterval: LOCK_POLL_INTERVAL_MS,
},
);
const heartbeatMutation = useMutation(() =>
customInstance({
url: `/merchants/${accID}/legal-documents/${documentID}/lock/heartbeat`,
method: "POST",
}),
);
const releaseMutation = useMutation(() =>
customInstance({
url: `/merchants/${accID}/legal-documents/${documentID}/lock`,
method: "DELETE",
}),
);
const sendHeartbeat = useCallback(() => {
Iif (!accID || !documentID) return;
heartbeatMutation.mutate();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [accID, documentID]);
const release = useCallback(() => {
Iif (!accID || !documentID) return;
releaseMutation.mutate();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [accID, documentID]);
const lock = lockQuery.data;
const isLockedByOther = !!lock?.locked && !lock?.lockedByMe;
return {
...lockQuery,
lock,
isLockedByOther,
sendHeartbeat,
release,
};
};
|