UNPKG

@sarawebs/sb-hooks

Version:

SaraWebs reusable hooks for react apps

155 lines (154 loc) 3.5 kB
import { useState as d, useEffect as y, useCallback as p } from "react"; const u = async (e) => { if (!e.ok) throw new Error(await e.text()); return e.json(); }, m = (e) => fetch(e, { mode: "cors", credentials: "include" }).then(u), D = (e, a) => fetch(`${e}/${a}`, { mode: "cors", credentials: "include" }).then( u ), T = (e, a) => fetch(e, { method: "POST", headers: { "Content-Type": "application/json" }, mode: "cors", credentials: "include", body: JSON.stringify(a) }).then(u), $ = (e, a, c) => fetch(`${e}/${a}`, { method: "PUT", headers: { "Content-Type": "application/json" }, mode: "cors", credentials: "include", body: JSON.stringify(c) }).then(u), k = (e, a) => fetch(`${e}/${a}`, { method: "DELETE", mode: "cors", credentials: "include" }).then((c) => { if (!c.ok) throw new Error("Delete failed"); return !0; }); function J(e) { const [a, c] = d([]), [f, i] = d(null), [g, o] = d(!1), [w, s] = d(null); return { data: a, item: f, loading: g, error: w, load: async (n) => { o(!0); try { const r = await m(n || e); return c(r), s(null), r; } catch (r) { s(r.message); } finally { o(!1); } }, loadOne: async (n, r) => { o(!0); try { const t = await D(r || e, n); return i(t), s(null), t; } catch (t) { s(t.message); } finally { o(!1); } }, create: async (n, r) => { o(!0); try { const t = await T(r || e, n); return c((l) => [...l, t]), t; } catch (t) { throw s(t.message), t; } finally { o(!1); } }, update: async (n, r, t) => { o(!0); try { const l = await $(t || e, n, r); return c((E) => E.map((h) => h.id === n ? l : h)), l; } catch (l) { throw s(l.message), l; } finally { o(!1); } }, remove: async (n, r) => { o(!0); try { await k(r || e, n), c((t) => t.filter((l) => l.id !== n)); } catch (t) { throw s(t.message), t; } finally { o(!1); } }, search: async (n, r) => { o(!0); try { const t = await m( `${r || e}?search=${encodeURIComponent(n)}` ); return s(null), t; } catch (t) { throw s(t.message), t; } finally { o(!1); } } }; } function N(e = "light") { const [a, c] = d(() => localStorage.getItem("theme") || e); y(() => { a === "dark" ? document.documentElement.classList.add("dark") : document.documentElement.classList.remove("dark"), localStorage.setItem("theme", a); }, [a]); const f = p(() => { c((i) => i === "light" ? "dark" : "light"); }, []); return { theme: a, setTheme: c, toggleTheme: f }; } const C = [ "#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 S(e) { const a = Array.isArray(e) && e.length > 0 ? e : C; return a[Math.floor(Math.random() * a.length)]; } function R(e) { y(() => { const a = S(e); document.documentElement.style.setProperty("--primary", a); }, [e]); } export { J as useCrud, R as useRandomPrimaryColor, N as useTheme };