jcicl 1.0.60 → 1.0.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DataPage/DataPage.d.ts +24 -0
- package/DataPage/DataPage.js +34 -0
- package/DataPage/index.d.ts +2 -0
- package/DataPage/index.js +7 -0
- package/DataPage/useDataPage.d.ts +33 -0
- package/DataPage/useDataPage.js +38 -0
- package/package.json +1 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { DataPageControls, DataPageResult } from './useDataPage';
|
|
2
|
+
import { WithLoadingProps } from '../WithLoading/WithLoading';
|
|
3
|
+
export interface DataPageProps<T> {
|
|
4
|
+
/** Async function that fetches data. Throw for fatal error. Return withWarnings() for partial success. */
|
|
5
|
+
fetchFn: () => Promise<T | DataPageResult<T>>;
|
|
6
|
+
/** useEffect dependency array. Default: [] (fetch once on mount). */
|
|
7
|
+
deps?: unknown[];
|
|
8
|
+
/** Render function for happy path. Receives data + controls. Read-only pages can ignore the 2nd arg. */
|
|
9
|
+
children: (data: T, controls: DataPageControls<T>) => React.ReactNode;
|
|
10
|
+
/** Shown on empty data. String → centered <p>. ReactNode → rendered directly. Omit for interactive empty. */
|
|
11
|
+
emptyState?: React.ReactNode | string;
|
|
12
|
+
/** Shown on fatal error. String → centered red <p>. Defaults to the caught error message. */
|
|
13
|
+
errorState?: React.ReactNode | string;
|
|
14
|
+
/** Data-dependent guards. Each receives data, returns ReactNode to short-circuit or null to continue. */
|
|
15
|
+
guards?: ((data: T) => React.ReactNode | null)[];
|
|
16
|
+
/** Determines if data is "empty". Default: array length === 0 or nullish. */
|
|
17
|
+
isEmpty?: (data: T) => boolean;
|
|
18
|
+
/** Props forwarded to the internal WithLoading wrapper (e.g. size, style). */
|
|
19
|
+
loadingProps?: Omit<WithLoadingProps, 'loading' | 'children'>;
|
|
20
|
+
/** Custom warning banner renderer. Default: orange centered <p> per warning. Pass null to suppress. */
|
|
21
|
+
renderWarnings?: ((warnings: string[]) => React.ReactNode) | null;
|
|
22
|
+
}
|
|
23
|
+
declare function DataPage<T>({ fetchFn, deps, children, emptyState, errorState, guards, isEmpty, loadingProps, renderWarnings, }: DataPageProps<T>): import("react/jsx-runtime").JSX.Element;
|
|
24
|
+
export default DataPage;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { jsx as e, Fragment as i, jsxs as A } from "react/jsx-runtime";
|
|
2
|
+
import B, { defaultIsEmpty as P } from "./useDataPage.js";
|
|
3
|
+
import k from "../WithLoading/WithLoading.js";
|
|
4
|
+
const c = (n, t) => typeof n == "string" ? /* @__PURE__ */ e("p", { style: { textAlign: "center", color: t }, children: n }) : /* @__PURE__ */ e(i, { children: n }), v = (n) => n.map((t, o) => /* @__PURE__ */ e("p", { style: { textAlign: "center", color: "darkorange" }, children: t }, o));
|
|
5
|
+
function L({
|
|
6
|
+
fetchFn: n,
|
|
7
|
+
deps: t,
|
|
8
|
+
children: o,
|
|
9
|
+
emptyState: s,
|
|
10
|
+
errorState: d,
|
|
11
|
+
guards: u,
|
|
12
|
+
isEmpty: m = P,
|
|
13
|
+
loadingProps: p,
|
|
14
|
+
renderWarnings: f
|
|
15
|
+
}) {
|
|
16
|
+
const { data: r, loading: l, error: g, warnings: a, setData: x, refetch: y } = B({ fetchFn: n, deps: t }), D = { setData: x, refetch: y, warnings: a };
|
|
17
|
+
if (!l && g)
|
|
18
|
+
return /* @__PURE__ */ e(i, { children: c(d || g, "red") });
|
|
19
|
+
if (!l && r !== null && m(r) && s !== void 0)
|
|
20
|
+
return /* @__PURE__ */ e(i, { children: c(s) });
|
|
21
|
+
if (!l && r !== null && u)
|
|
22
|
+
for (const w of u) {
|
|
23
|
+
const h = w(r);
|
|
24
|
+
if (h !== null) return /* @__PURE__ */ e(i, { children: h });
|
|
25
|
+
}
|
|
26
|
+
const j = a.length > 0 && f !== null ? (f ?? v)(a) : null;
|
|
27
|
+
return /* @__PURE__ */ A(k, { loading: l, ...p, children: [
|
|
28
|
+
j,
|
|
29
|
+
r !== null ? o(r, D) : null
|
|
30
|
+
] });
|
|
31
|
+
}
|
|
32
|
+
export {
|
|
33
|
+
L as default
|
|
34
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
declare const DATA_PAGE_RESULT: unique symbol;
|
|
2
|
+
export interface DataPageResult<T> {
|
|
3
|
+
[DATA_PAGE_RESULT]: true;
|
|
4
|
+
data: T;
|
|
5
|
+
warnings: string[];
|
|
6
|
+
}
|
|
7
|
+
/** Wrap a fetchFn return value to signal partial success with warnings. */
|
|
8
|
+
export declare function withWarnings<T>(data: T, warnings: string[]): DataPageResult<T>;
|
|
9
|
+
export interface DataPageControls<T> {
|
|
10
|
+
/** Update local data without re-fetching. For optimistic CRUD (add/edit/delete). */
|
|
11
|
+
setData: (updater: (prev: T) => T) => void;
|
|
12
|
+
/** Re-run fetchFn from scratch (resets to loading state). */
|
|
13
|
+
refetch: () => void;
|
|
14
|
+
/** Warnings from partial success (empty array if none). */
|
|
15
|
+
warnings: string[];
|
|
16
|
+
}
|
|
17
|
+
export interface UseDataPageOptions<T> {
|
|
18
|
+
/** Async function that fetches data. Throw for fatal error. Return withWarnings() for partial success. */
|
|
19
|
+
fetchFn: () => Promise<T | DataPageResult<T>>;
|
|
20
|
+
/** useEffect dependency array. Default: [] (fetch once on mount). */
|
|
21
|
+
deps?: unknown[];
|
|
22
|
+
}
|
|
23
|
+
export interface UseDataPageReturn<T> {
|
|
24
|
+
data: T | null;
|
|
25
|
+
loading: boolean;
|
|
26
|
+
error: string | null;
|
|
27
|
+
warnings: string[];
|
|
28
|
+
setData: (updater: (prev: T) => T) => void;
|
|
29
|
+
refetch: () => void;
|
|
30
|
+
}
|
|
31
|
+
export declare const defaultIsEmpty: <T>(data: T) => boolean;
|
|
32
|
+
declare function useDataPage<T>({ fetchFn, deps }: UseDataPageOptions<T>): UseDataPageReturn<T>;
|
|
33
|
+
export default useDataPage;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useState as n, useRef as R, useEffect as b, useCallback as d } from "react";
|
|
2
|
+
const g = Symbol("DataPageResult");
|
|
3
|
+
function S(e, a) {
|
|
4
|
+
return { [g]: !0, data: e, warnings: a };
|
|
5
|
+
}
|
|
6
|
+
const x = (e) => e == null ? !0 : Array.isArray(e) ? e.length === 0 : !1;
|
|
7
|
+
function F({ fetchFn: e, deps: a = [] }) {
|
|
8
|
+
const [y, c] = n(null), [h, u] = n(!0), [w, o] = n(null), [A, l] = n([]), [E, m] = n(0), f = R(e);
|
|
9
|
+
f.current = e, b(() => {
|
|
10
|
+
let t = !1;
|
|
11
|
+
return u(!0), o(null), l([]), (async () => {
|
|
12
|
+
try {
|
|
13
|
+
const r = await f.current();
|
|
14
|
+
if (t) return;
|
|
15
|
+
if (r && typeof r == "object" && g in r) {
|
|
16
|
+
const i = r;
|
|
17
|
+
c(i.data), l(i.warnings);
|
|
18
|
+
} else
|
|
19
|
+
c(r);
|
|
20
|
+
} catch (r) {
|
|
21
|
+
t || o(r instanceof Error ? r.message : "An unexpected error occurred");
|
|
22
|
+
} finally {
|
|
23
|
+
t || u(!1);
|
|
24
|
+
}
|
|
25
|
+
})(), () => {
|
|
26
|
+
t = !0;
|
|
27
|
+
};
|
|
28
|
+
}, [...a, E]);
|
|
29
|
+
const p = d((t) => {
|
|
30
|
+
c((s) => s !== null ? t(s) : s);
|
|
31
|
+
}, []), D = d(() => m((t) => t + 1), []);
|
|
32
|
+
return { data: y, loading: h, error: w, warnings: A, setData: p, refetch: D };
|
|
33
|
+
}
|
|
34
|
+
export {
|
|
35
|
+
F as default,
|
|
36
|
+
x as defaultIsEmpty,
|
|
37
|
+
S as withWarnings
|
|
38
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jcicl",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.61",
|
|
5
5
|
"description": "Component library for the websites of Johnson County Iowa",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://devops.jc.net/JCIT/Business%20Solutions%20Delivery/_git/JCComponentLibrary?path=%2FREADME.md&version=GBmaster",
|