@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
55 lines (54 loc) • 1.47 kB
JavaScript
"use client";
import { useEffect, useRef, useState } from 'react';
import useHasContentChanged from "./useHasContentChanged.js";
import useReportError from "./useReportError.js";
export default function useHandleStatus({
outerContext,
preventUncommittedChanges,
error,
name
}) {
const {
hasContentChanged
} = useHasContentChanged({
enabled: preventUncommittedChanges
});
let errorToReport = undefined;
if (preventUncommittedChanges && hasContentChanged) {
errorToReport = error;
}
useReportError(errorToReport, outerContext, name);
const showStatus = useShowStatus({
outerContext,
hasContentChanged,
preventUncommittedChanges
});
return {
hasContentChanged,
showStatus
};
}
function useShowStatus({
outerContext,
hasContentChanged,
preventUncommittedChanges
}) {
const showAllErrors = outerContext === null || outerContext === void 0 ? void 0 : outerContext.showAllErrors;
const [showStatus, setShowStatus] = useState(showAllErrors);
const showRef = useRef(showAllErrors);
useEffect(() => {
if (!preventUncommittedChanges) {
return;
}
if (!hasContentChanged) {
setShowStatus(false);
} else {
if (showRef.current !== showAllErrors) {
setShowStatus(showAllErrors);
}
}
showRef.current = showAllErrors;
}, [hasContentChanged, preventUncommittedChanges, showAllErrors]);
return showStatus;
}
//# sourceMappingURL=useHandleStatus.js.map