react-smart-crud 0.1.5 → 0.1.8

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/package.json +1 -1
  2. package/src/actions.js +46 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-smart-crud",
3
- "version": "0.1.5",
3
+ "version": "0.1.8",
4
4
  "description": "Minimal optimistic CRUD helper for React without useState, useEffect, or prop drilling",
5
5
  "type": "./src/index.d.ts",
6
6
  "main": "./src/index.js",
package/src/actions.js CHANGED
@@ -3,7 +3,6 @@ import { request } from "./http";
3
3
  import { notify } from "./notify";
4
4
 
5
5
  /* ================= CREATE ================= */
6
-
7
6
  export function createItem(url, data, options = {}) {
8
7
  const entry = getEntry(url);
9
8
 
@@ -30,19 +29,30 @@ export function createItem(url, data, options = {}) {
30
29
  entry.data = entry.data.map((item) =>
31
30
  item._temp ? { ...item, ...serverData, _temp: false } : item
32
31
  );
33
-
32
+ if (options.onSuccess) {
33
+ options.onSuccess(serverData);
34
+ } else {
35
+ notify("success", "Created", { url, data: serverData });
36
+ }
34
37
  options.onSuccess?.(serverData);
35
38
  })
36
39
  .catch((error) => {
37
40
  // 🔴 rollback
38
41
  entry.data = entry.data.filter((i) => !i._temp);
39
- options.onError?.(error);
42
+
43
+ if (options.onError) {
44
+ options.onError?.(error);
45
+ } else {
46
+ notify("error", error.message || "Create failed", {
47
+ url,
48
+ error,
49
+ });
50
+ }
40
51
  })
41
52
  .finally(() => {
42
53
  entry.subscribers.forEach((fn) => fn());
43
54
  });
44
55
  }
45
-
46
56
  /* ================= UPDATE ================= */
47
57
  export function updateItem(url, id, data, options = {}) {
48
58
  const entry = getEntry(url);
@@ -72,32 +82,57 @@ export function updateItem(url, id, data, options = {}) {
72
82
  entry.data = entry.data.map((item) =>
73
83
  item.id === id ? { ...item, ...serverData, _updating: false } : item
74
84
  );
75
-
76
- options.onSuccess?.(serverData);
85
+ if (options.onSuccess) {
86
+ options.onSuccess(serverData);
87
+ } else {
88
+ notify("success", "Updated", { url, id, data: serverData });
89
+ }
77
90
  })
78
91
  .catch((error) => {
79
92
  entry.data = backup;
80
- options.onError?.(error);
93
+ if (options.onError) {
94
+ options.onError?.(error);
95
+ } else {
96
+ notify("error", error.message || "Update failed", {
97
+ url,
98
+ id,
99
+ error,
100
+ });
101
+ }
81
102
  })
82
103
  .finally(() => entry.subscribers.forEach((fn) => fn()));
83
104
  }
84
-
85
105
  /* ================= DELETE ================= */
86
106
  export function deleteItem(url, id, options = {}) {
87
107
  const entry = getEntry(url);
88
108
  const backup = [...entry.data];
89
109
 
110
+ // 🟢 Optimistic delete
90
111
  entry.data = entry.data.filter((i) => i.id !== id);
91
112
  entry.subscribers.forEach((fn) => fn());
92
113
 
93
114
  request(`${url}/${id}`, { method: "DELETE" })
94
115
  .then(() => {
95
- options.onSuccess?.();
96
- notify("success", "Deleted");
116
+ if (options.onSuccess) {
117
+ options.onSuccess();
118
+ } else {
119
+ notify("success", "Deleted", { action: "delete", url, id });
120
+ }
97
121
  })
98
122
  .catch((error) => {
123
+ // 🔴 rollback
99
124
  entry.data = backup;
100
125
  entry.subscribers.forEach((fn) => fn());
101
- options.onError?.(error);
126
+
127
+ if (options.onError) {
128
+ options.onError(error);
129
+ } else {
130
+ notify("error", error.message || "Delete failed", {
131
+ action: "delete",
132
+ url,
133
+ id,
134
+ error,
135
+ });
136
+ }
102
137
  });
103
138
  }