@sarawebs/sb-hooks
Version:
SaraWebs reusable hooks for react apps
135 lines (134 loc) • 3.47 kB
JavaScript
import { useState as u, useEffect as p, useCallback as C } from "react";
const i = async (t) => {
let e = null;
try {
e = await t.json();
} catch {
}
return t.ok ? { data: e, error: null, status: t.status } : {
data: null,
error: (e == null ? void 0 : e.error) || t.statusText,
status: t.status
};
}, E = (t) => fetch(t, { mode: "cors", credentials: "include" }).then(i), D = (t, e) => fetch(`${t}/${e}`, { mode: "cors", credentials: "include" }).then(
i
), I = (t, e) => fetch(t, {
method: "POST",
headers: { "Content-Type": "application/json" },
mode: "cors",
credentials: "include",
body: JSON.stringify(e)
}).then(i), S = (t, e, s) => fetch(`${t}/${e}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
mode: "cors",
credentials: "include",
body: JSON.stringify(s)
}).then(i), b = (t, e) => fetch(`${t}/${e}`, {
method: "DELETE",
mode: "cors",
credentials: "include"
}).then(i);
function x(t) {
const [e, s] = u([]), [f, m] = u(null), [T, y] = u(!1), [w, h] = u(null), d = async (a) => {
y(!0), h(null);
try {
const r = await a();
return r.error && h(r.error), r;
} finally {
y(!1);
}
};
return {
data: e,
item: f,
loading: T,
error: w,
load: (a) => d(async () => {
const { data: r, error: o, status: n } = await E(a || t);
return o || s(r), { data: r, error: o, status: n };
}),
loadOne: (a, r) => d(async () => {
const { data: o, error: n, status: c } = await D(r || t, a);
return n || m(o), { data: o, error: n, status: c };
}),
create: (a, r) => d(async () => {
const {
data: o,
error: n,
status: c
} = await I(r || t, a);
return n || s((l) => [...l, o]), { data: o, error: n, status: c };
}),
update: (a, r, o) => d(async () => {
const {
data: n,
error: c,
status: l
} = await S(o || t, a, r);
return c || s(
($) => $.map((g) => g.id === a ? n : g)
), { data: n, error: c, status: l };
}),
remove: (a, r) => d(async () => {
const { error: o, status: n } = await b(r || t, a);
return o || s((c) => c.filter((l) => l.id !== a)), { data: null, error: o, status: n };
}),
search: (a, r) => d(
() => E(`${r || t}?search=${encodeURIComponent(a)}`)
),
clearError: () => h(null)
};
}
function M(t = "light") {
const [e, s] = u(() => localStorage.getItem("theme") || t);
p(() => {
e === "dark" ? document.documentElement.classList.add("dark") : document.documentElement.classList.remove("dark"), localStorage.setItem("theme", e);
}, [e]);
const f = C(() => {
s((m) => m === "light" ? "dark" : "light");
}, []);
return { theme: e, setTheme: s, toggleTheme: f };
}
const k = [
"#8e1330",
"#8a6c1d",
"#406555",
"#7c0e45",
"#8d1978",
"#181770",
"#cc38d7",
"#47504e",
"#0fa080",
"#0f64a0",
"#460fa0",
"#a00f65",
"#a00f24",
"#0f94a0",
"#0fa067",
"#0fa03c",
"#38a00f",
"#a09d0f",
"#a0670f",
"#a0370f",
"#a00f0f",
"#2b634d",
"#2b4c63",
"#2e6bc6",
"#1992d4"
];
function O(t) {
const e = Array.isArray(t) && t.length > 0 ? t : k;
return e[Math.floor(Math.random() * e.length)];
}
function B(t) {
p(() => {
const e = O(t);
document.documentElement.style.setProperty("--primary", e);
}, [t]);
}
export {
x as useCrud,
B as useRandomPrimaryColor,
M as useTheme
};