@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
48 lines (47 loc) • 1.75 kB
JavaScript
"use client";
import { useCallback, useContext, useMemo, useRef } from 'react';
import { createReferenceKey, createSharedState } from "../../../../shared/helpers/useSharedState.js";
import { defaultContextState } from "../../DataContext/Context.js";
import DataContextRefContext from "../../DataContext/DataContextRefContext.js";
import { useDataReturn } from "./useData.js";
export default function useDataWithoutSubscription(id = undefined, initialData = undefined) {
var _dataContextRef$curre;
const sharedDataRef = useRef(null);
const sharedAttachmentsRef = useRef(null);
sharedDataRef.current = id ? createSharedState(id, initialData) : null;
sharedAttachmentsRef.current = id ? createSharedState(createReferenceKey(id, 'attachments')) : null;
const dataContextRef = useContext(DataContextRefContext);
const dataContext = (_dataContextRef$curre = dataContextRef?.current) !== null && _dataContextRef$curre !== void 0 ? _dataContextRef$curre : defaultContextState;
const {
remove,
update,
set,
getValue,
reduceToVisibleFields,
filterData
} = useDataReturn({
id,
initialData,
sharedDataRef,
sharedAttachmentsRef,
dataContext,
dataContextRef,
errorMessage: 'useDataWithoutSubscription needs to run inside DataContext (Form.Handler) or have a valid id'
});
const getData = useCallback(() => {
if (id) {
return sharedDataRef.current?.get?.();
}
return getValue('/');
}, [getValue, id]);
return useMemo(() => ({
getData,
remove,
update,
set,
getValue,
reduceToVisibleFields,
filterData
}), [getData, remove, update, set, getValue, reduceToVisibleFields, filterData]);
}
//# sourceMappingURL=useDataWithoutSubscription.js.map