use-abcd 0.1.0 → 0.1.2

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/README.md CHANGED
@@ -29,63 +29,80 @@ bun add use-abcd
29
29
  ## Quick Example
30
30
 
31
31
  ```typescript
32
- import { useCrud } from "use-abcd";
32
+ import React, { useCallback } from "react";
33
+ import { useCrud, type CrudConfig, type ItemWithState, type Updater } from "../useCrud";
34
+ import { map } from "lodash-es";
33
35
 
34
36
  type Todo = {
35
37
  id: string;
36
- title: string;
37
- completed: boolean;
38
+ description: string;
39
+ complete: boolean;
38
40
  };
39
41
 
40
- function TodoList() {
41
- const { items, isLoading, create, update, remove } = useCrud<Todo>({
42
- id: "todos",
43
- context: {},
44
- // Configure caching (optional)
45
- caching: {
46
- capacity: 10,
47
- age: 60000, // 1 minute
48
- },
49
- // Fetch todos from API
50
- fetch: async ({ signal }) => {
51
- const response = await fetch("https://api.example.com/todos", { signal });
52
- const items = await response.json();
53
- return { items, metadata: {} };
54
- },
55
- // Create new todo
56
- create: async (todo) => {
57
- const response = await fetch("https://api.example.com/todos", {
58
- method: "POST",
59
- body: JSON.stringify(todo),
60
- });
61
- const { id } = await response.json();
62
- return { id };
63
- },
64
- });
65
-
66
- if (isLoading) return <div>Loading...</div>;
42
+ const TodoCrud: CrudConfig<Todo> = {
43
+ id: "todo-crud",
44
+ context: {},
45
+ caching: {
46
+ age: 5000000,
47
+ capacity: 10,
48
+ },
49
+ fetch: async () => {
50
+ await new Promise((resolve) => {
51
+ setTimeout(resolve, 200);
52
+ });
53
+ return {
54
+ items: [
55
+ { id: "1", description: "Shop for electronics", complete: false },
56
+ { id: "2", description: "Find time for learning", complete: false },
57
+ { id: "3", description: "Pick stocks", complete: false },
58
+ { id: "4", description: "Pick stocks", complete: false },
59
+ ],
60
+ metadata: {},
61
+ };
62
+ },
63
+ };
64
+
65
+ const Item = React.memo(function Item(props: {
66
+ item: ItemWithState<Todo>;
67
+ update: (item: ItemWithState<Todo>, updater: Updater<Todo>, isOptimistic?: boolean) => void;
68
+ }) {
69
+ const { update, item } = props;
70
+ const { data } = item;
71
+
72
+ const markComplete = useCallback(() => {
73
+ update(item, (draft) => {
74
+ draft.complete = !item.data.complete;
75
+ });
76
+ }, [update, item]);
77
+
78
+ return (
79
+ <div key={data.id} className="flex gap-2 mb-1">
80
+ <div className={data.complete ? "line-through" : ""}>{data.description}</div>
81
+ <button
82
+ className="bg-blue-300 px-2 rounded active:bg-blue-400 cursor-pointer font-bold"
83
+ onClick={markComplete}
84
+ >
85
+ Complete
86
+ </button>
87
+ </div>
88
+ );
89
+ });
90
+
91
+ export const Todo = React.memo(function Todo() {
92
+ const { items, isLoading, update } = useCrud(TodoCrud);
93
+
94
+ if (isLoading) {
95
+ return <div>Loading...</div>;
96
+ }
67
97
 
68
98
  return (
69
- <div>
70
- <button onClick={() => create({ title: "New Todo", completed: false })}>Add Todo</button>
71
- {items.map((item) => (
72
- <div key={item.data.id}>
73
- <span>{item.data.title}</span>
74
- <button
75
- onClick={() =>
76
- update(item.data, (draft) => {
77
- draft.completed = !draft.completed;
78
- })
79
- }
80
- >
81
- Toggle
82
- </button>
83
- <button onClick={() => remove(item.data)}>Delete</button>
84
- </div>
99
+ <div className="p-2">
100
+ {map(items, (item) => (
101
+ <Item key={item.data.id} item={item} update={update} />
85
102
  ))}
86
103
  </div>
87
104
  );
88
- }
105
+ });
89
106
  ```
90
107
 
91
108
  > **Note**: This is a single-file library with a focused scope. Please read the source code for a deeper understanding of its implementation and capabilities.
package/dist/useCrud.d.ts CHANGED
@@ -98,14 +98,7 @@ export declare type Updater<T> = (updatable: T) => void;
98
98
  */
99
99
  export declare function useCrud<T extends Item = Item, C extends Record<string, any> = any>(config: CrudConfig<T, C>): {
100
100
  itemsById: Map<string, ItemWithState<T>>;
101
- items: {
102
- id: string;
103
- data: T;
104
- state: TransitionStates;
105
- optimistic: boolean;
106
- errors: string[];
107
- action?: [TransitionStates, T];
108
- }[];
101
+ items: ItemWithState<T>[];
109
102
  metadata: unknown;
110
103
  isLoading: boolean;
111
104
  hasError: boolean;