use-abcd 0.1.0 → 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.
Files changed (2) hide show
  1. package/README.md +60 -46
  2. package/package.json +1 -1
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/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "repository": {
5
5
  "url": "https://github.com/smtrd3/use-abcd"
6
6
  },
7
- "version": "0.1.0",
7
+ "version": "0.1.1",
8
8
  "type": "module",
9
9
  "files": [
10
10
  "dist"