use-abcd 0.0.2 → 0.1.1

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,59 +29,73 @@ 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 { map } from "lodash-es";
34
+ import { useCrud, useCrudOperations, type CrudConfig, type ItemWithState } from "use-abcd";
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: "one", description: "Shop for electronics", complete: false },
56
+ { id: "two", description: "Find time for learning", complete: false },
57
+ { id: "three", description: "Pick stocks", complete: false },
58
+ ],
59
+ metadata: {},
60
+ };
61
+ },
62
+ };
63
+
64
+ const Item = React.memo((props: { item: ItemWithState<Todo> }) => {
65
+ const item = props.item;
66
+ const data = item.data;
67
+ const { update } = useCrudOperations(TodoCrud);
68
+
69
+ const markComplete = useCallback(() => {
70
+ update(item, (draft) => {
71
+ draft.complete = !item.data.complete;
72
+ });
73
+ }, [update, item]);
74
+
75
+ return (
76
+ <div key={data.id} className="flex gap-2 mb-1">
77
+ <div className={data.complete ? "line-through" : ""}>{data.description}</div>
78
+ <button
79
+ className="bg-blue-300 px-2 rounded active:bg-blue-400 cursor-pointer font-bold"
80
+ onClick={markComplete}
81
+ >
82
+ Complete
83
+ </button>
84
+ </div>
85
+ );
86
+ });
87
+
88
+ export function Todo() {
89
+ const { items, isLoading } = useCrud(TodoCrud);
90
+
91
+ if (isLoading) {
92
+ return <div>Loading...</div>;
93
+ }
67
94
 
68
95
  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>
96
+ <div className="p-2">
97
+ {map(items, (item) => (
98
+ <Item item={item} />
85
99
  ))}
86
100
  </div>
87
101
  );
package/dist/useCrud.d.ts CHANGED
@@ -52,14 +52,14 @@ export declare type FetchFn<T extends Item = Item> = (option: QueryOption) => Pr
52
52
 
53
53
  export declare type Item = {
54
54
  id: string;
55
- } & Record<string, unknown>;
55
+ } & Record<string, any>;
56
56
 
57
57
  export declare type ItemWithState<T extends Item = Item> = {
58
58
  data: T;
59
- state: "create" | "update" | "delete" | "idle" | "error";
59
+ state: TransitionStates;
60
60
  optimistic: boolean;
61
61
  errors: string[];
62
- input?: T;
62
+ action?: [TransitionStates, T];
63
63
  };
64
64
 
65
65
  export declare type QueryOption = {
@@ -84,6 +84,8 @@ export declare type TransitionFn<T extends Item = Item> = (item: Partial<T>, opt
84
84
  id: string;
85
85
  }>;
86
86
 
87
+ export declare type TransitionStates = "create" | "update" | "delete" | "idle" | "error" | "changed";
88
+
87
89
  export declare type Updater<T> = (updatable: T) => void;
88
90
 
89
91
  /**
@@ -99,18 +101,21 @@ export declare function useCrud<T extends Item = Item, C extends Record<string,
99
101
  items: {
100
102
  id: string;
101
103
  data: T;
102
- state: "create" | "update" | "delete" | "idle" | "error";
104
+ state: TransitionStates;
103
105
  optimistic: boolean;
104
106
  errors: string[];
105
- input?: T;
107
+ action?: [TransitionStates, T];
106
108
  }[];
107
109
  metadata: unknown;
108
110
  isLoading: boolean;
109
111
  hasError: boolean;
110
112
  errors: string[];
111
113
  create: (item: Omit<T, "id">) => void;
112
- update: (item: T, updater: Updater<T>, isOptimistic?: boolean) => void;
113
- remove: (item: T) => void;
114
+ update: (item: ItemWithState<T>, updater: Updater<T>, isOptimistic?: boolean) => void;
115
+ change: (item: ItemWithState<T>, updater: Updater<T>) => void;
116
+ retry: (item: ItemWithState<T>) => void;
117
+ save: (item: ItemWithState<T>) => void;
118
+ remove: (item: ItemWithState<T>) => void;
114
119
  cancelFetch: () => void;
115
120
  cancelOperation: (id: string) => void;
116
121
  };
@@ -126,8 +131,11 @@ export declare function useCrudOperations<T extends Item = Item, C extends Recor
126
131
  fetch: () => void;
127
132
  refetch: () => void;
128
133
  create: (item: Omit<T, "id">) => void;
129
- update: (item: T, updater: Updater<T>, isOptimistic?: boolean) => void;
130
- remove: (item: T) => void;
134
+ update: (item: ItemWithState<T>, updater: Updater<T>, isOptimistic?: boolean) => void;
135
+ change: (item: ItemWithState<T>, updater: Updater<T>) => void;
136
+ save: (item: ItemWithState<T>) => void;
137
+ retry: (item: ItemWithState<T>) => void;
138
+ remove: (item: ItemWithState<T>) => void;
131
139
  cancelFetch: () => void;
132
140
  cancelOperation: (id: string) => void;
133
141
  };