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.
- package/README.md +60 -46
- 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 {
|
|
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
|
-
|
|
37
|
-
|
|
38
|
+
description: string;
|
|
39
|
+
complete: boolean;
|
|
38
40
|
};
|
|
39
41
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
71
|
-
|
|
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
|
);
|