@jsenv/navi 0.0.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/index.js +51 -0
- package/package.json +38 -0
- package/src/action_private_properties.js +11 -0
- package/src/action_proxy_test.html +353 -0
- package/src/action_run_states.js +5 -0
- package/src/actions.js +1377 -0
- package/src/browser_integration/browser_integration.js +191 -0
- package/src/browser_integration/document_back_and_forward.js +17 -0
- package/src/browser_integration/document_loading_signal.js +100 -0
- package/src/browser_integration/document_state_signal.js +9 -0
- package/src/browser_integration/document_url_signal.js +9 -0
- package/src/browser_integration/use_is_visited.js +19 -0
- package/src/browser_integration/via_history.js +199 -0
- package/src/browser_integration/via_navigation.js +168 -0
- package/src/components/action_execution/form_context.js +8 -0
- package/src/components/action_execution/render_actionable_component.jsx +27 -0
- package/src/components/action_execution/use_action.js +330 -0
- package/src/components/action_execution/use_execute_action.js +161 -0
- package/src/components/action_renderer.jsx +136 -0
- package/src/components/collect_form_element_values.js +79 -0
- package/src/components/demos/0_button_demo.html +155 -0
- package/src/components/demos/1_checkbox_demo.html +257 -0
- package/src/components/demos/2_input_textual_demo.html +354 -0
- package/src/components/demos/3_radio_demo.html +222 -0
- package/src/components/demos/4_select_demo.html +104 -0
- package/src/components/demos/5_list_scrollable_demo.html +153 -0
- package/src/components/demos/action/0_button_demo.html +204 -0
- package/src/components/demos/action/10_shortcuts_demo.html +189 -0
- package/src/components/demos/action/11_nested_shortcuts_demo.html +401 -0
- package/src/components/demos/action/1_input_text_demo.html +461 -0
- package/src/components/demos/action/2_form_multiple.html +303 -0
- package/src/components/demos/action/3_details_demo.html +172 -0
- package/src/components/demos/action/4_input_checkbox_demo.html +611 -0
- package/src/components/demos/action/6_checkbox_list_demo.html +109 -0
- package/src/components/demos/action/7_radio_list_demo.html +217 -0
- package/src/components/demos/action/8_editable_text_demo.html +442 -0
- package/src/components/demos/action/9_link_demo.html +172 -0
- package/src/components/demos/demo.md +0 -0
- package/src/components/demos/route/basic/basic.html +14 -0
- package/src/components/demos/route/basic/basic_route_demo.jsx +224 -0
- package/src/components/demos/route/multi/multi.html +14 -0
- package/src/components/demos/route/multi/multi_route_demo.jsx +277 -0
- package/src/components/details/details.jsx +248 -0
- package/src/components/details/summary_marker.jsx +141 -0
- package/src/components/editable_text/editable_text.jsx +96 -0
- package/src/components/error_boundary_context.js +9 -0
- package/src/components/form.jsx +144 -0
- package/src/components/input/button.jsx +333 -0
- package/src/components/input/checkbox_list.jsx +294 -0
- package/src/components/input/field.jsx +61 -0
- package/src/components/input/field_css.js +118 -0
- package/src/components/input/input.jsx +15 -0
- package/src/components/input/input_checkbox.jsx +370 -0
- package/src/components/input/input_radio.jsx +299 -0
- package/src/components/input/input_textual.jsx +338 -0
- package/src/components/input/radio_list.jsx +283 -0
- package/src/components/input/select.jsx +273 -0
- package/src/components/input/use_form_event.js +20 -0
- package/src/components/input/use_on_change.js +12 -0
- package/src/components/link/link.jsx +291 -0
- package/src/components/loader/loader_background.jsx +324 -0
- package/src/components/loader/loading_spinner.jsx +68 -0
- package/src/components/loader/network_speed.js +83 -0
- package/src/components/loader/rectangle_loading.jsx +225 -0
- package/src/components/route.jsx +15 -0
- package/src/components/selection/selection.js +5 -0
- package/src/components/selection/selection_context.jsx +262 -0
- package/src/components/shortcut/os.js +9 -0
- package/src/components/shortcut/shortcut_context.jsx +390 -0
- package/src/components/use_action_events.js +37 -0
- package/src/components/use_auto_focus.js +43 -0
- package/src/components/use_debounce_true.js +31 -0
- package/src/components/use_focus_group.js +19 -0
- package/src/components/use_initial_value.js +104 -0
- package/src/components/use_is_visited.js +19 -0
- package/src/components/use_ref_array.js +38 -0
- package/src/components/use_signal_sync.js +50 -0
- package/src/components/use_state_array.js +40 -0
- package/src/docs/actions.md +228 -0
- package/src/docs/demos/resource/action_status.jsx +42 -0
- package/src/docs/demos/resource/demo.md +1 -0
- package/src/docs/demos/resource/resource_demo_0.html +84 -0
- package/src/docs/demos/resource/resource_demo_10_post_gc.html +364 -0
- package/src/docs/demos/resource/resource_demo_11_describe_many.html +362 -0
- package/src/docs/demos/resource/resource_demo_2.html +173 -0
- package/src/docs/demos/resource/resource_demo_3_filtered_users.html +415 -0
- package/src/docs/demos/resource/resource_demo_4_details.html +284 -0
- package/src/docs/demos/resource/resource_demo_5_renderer_lazy.html +115 -0
- package/src/docs/demos/resource/resource_demo_6_gc.html +217 -0
- package/src/docs/demos/resource/resource_demo_7_child_gc.html +240 -0
- package/src/docs/demos/resource/resource_demo_8_proxy_gc.html +319 -0
- package/src/docs/demos/resource/resource_demo_9_describe_one.html +472 -0
- package/src/docs/demos/resource/tata.jsx +3 -0
- package/src/docs/demos/resource/toto.jsx +3 -0
- package/src/docs/demos/user_nav/user_nav.html +12 -0
- package/src/docs/demos/user_nav/user_nav.jsx +330 -0
- package/src/docs/resource_dependencies.md +103 -0
- package/src/docs/resource_with_params.md +80 -0
- package/src/notes.md +13 -0
- package/src/route/route.js +518 -0
- package/src/route/route.test.html +228 -0
- package/src/store/array_signal_store.js +537 -0
- package/src/store/local_storage_signal.js +17 -0
- package/src/store/resource_graph.js +1303 -0
- package/src/store/tests/resource_graph_autoreload_demo.html +12 -0
- package/src/store/tests/resource_graph_autoreload_demo.jsx +964 -0
- package/src/store/tests/resource_graph_dependencies.test.js +95 -0
- package/src/store/value_in_local_storage.js +187 -0
- package/src/symbol_object_signal.js +1 -0
- package/src/use_action_data.js +10 -0
- package/src/use_action_status.js +47 -0
- package/src/utils/add_many_event_listeners.js +15 -0
- package/src/utils/array_add_remove.js +61 -0
- package/src/utils/array_signal.js +15 -0
- package/src/utils/compare_two_js_values.js +172 -0
- package/src/utils/execute_with_cleanup.js +21 -0
- package/src/utils/get_caller_info.js +85 -0
- package/src/utils/iterable_weak_set.js +62 -0
- package/src/utils/js_value_weak_map.js +162 -0
- package/src/utils/js_value_weak_map_demo.html +690 -0
- package/src/utils/merge_two_js_values.js +53 -0
- package/src/utils/stringify_for_display.js +150 -0
- package/src/utils/weak_effect.js +48 -0
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<link rel="icon" href="data:," />
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
7
|
+
<title>Resource demo describe many</title>
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<div id="root" style="position: relative; width: 600px"></div>
|
|
11
|
+
|
|
12
|
+
<script type="module" jsenv-type="module/jsx">
|
|
13
|
+
import { render } from "preact";
|
|
14
|
+
import { useState, useEffect, useRef } from "preact/hooks";
|
|
15
|
+
import {
|
|
16
|
+
resource,
|
|
17
|
+
// eslint-disable-next-line no-unused-vars
|
|
18
|
+
ActionRenderer,
|
|
19
|
+
} from "@jsenv/navi";
|
|
20
|
+
import {
|
|
21
|
+
// eslint-disable-next-line no-unused-vars
|
|
22
|
+
ActionStatus,
|
|
23
|
+
} from "./action_status.jsx";
|
|
24
|
+
import { localStorageSignal } from "../../../store/local_storage_signal.js";
|
|
25
|
+
|
|
26
|
+
// Mock data
|
|
27
|
+
const users = [
|
|
28
|
+
{ id: "user_1", name: "Alice" },
|
|
29
|
+
{ id: "user_2", name: "Bob" },
|
|
30
|
+
{ id: "user_3", name: "Charlie" },
|
|
31
|
+
{ id: "user_4", name: "Diana" },
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
// Mock friendships data (userId -> array of friend userIds)
|
|
35
|
+
const friendships = {
|
|
36
|
+
user_1: ["user_2", "user_3"],
|
|
37
|
+
user_2: ["user_1"],
|
|
38
|
+
user_3: ["user_1", "user_4"],
|
|
39
|
+
user_4: ["user_3"],
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const getUser = ({ userId }) => {
|
|
43
|
+
const user = users.find((user) => user.id === userId);
|
|
44
|
+
if (!user) {
|
|
45
|
+
throw new Error(`User with id "${userId}" not found`);
|
|
46
|
+
}
|
|
47
|
+
return user;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const USER = resource("user", {
|
|
51
|
+
idKey: "id",
|
|
52
|
+
mutableIdKey: "name",
|
|
53
|
+
GET: getUser,
|
|
54
|
+
GET_MANY: () => users,
|
|
55
|
+
PUT: ({ userId, field, value }) => {
|
|
56
|
+
let userToUpdate = users.find((u) => u.id === userId);
|
|
57
|
+
userToUpdate[field] = value;
|
|
58
|
+
return { id: userId, [field]: value };
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// ✅ One-to-many relationship: User can have many friends
|
|
63
|
+
const USER_FRIENDS = USER.many("friends", USER, {
|
|
64
|
+
// GET - Retrieve all friends of a user
|
|
65
|
+
GET_MANY: ({ userId }) => {
|
|
66
|
+
const friendIds = friendships[userId] || [];
|
|
67
|
+
return {
|
|
68
|
+
id: userId,
|
|
69
|
+
friends: friendIds.map((friendId) =>
|
|
70
|
+
users.find((u) => u.id === friendId),
|
|
71
|
+
),
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
// POST - Add a friend relationship
|
|
76
|
+
POST: ({ userId, friendId }) => {
|
|
77
|
+
if (!friendships[userId]) {
|
|
78
|
+
friendships[userId] = [];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Add friend if not already added
|
|
82
|
+
if (!friendships[userId].includes(friendId)) {
|
|
83
|
+
friendships[userId].push(friendId);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Return the added friend
|
|
87
|
+
return users.find((u) => u.id === friendId);
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
// DELETE - Remove a friend relationship
|
|
91
|
+
DELETE: ({ userId, friendId }) => {
|
|
92
|
+
if (friendships[userId]) {
|
|
93
|
+
friendships[userId] = friendships[userId].filter(
|
|
94
|
+
(id) => id !== friendId,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
return [userId, friendId];
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
USER.RENAME = USER.PUT.bindParams({ field: "name" });
|
|
102
|
+
|
|
103
|
+
// Signal for current user
|
|
104
|
+
const currentUserIdSignal = localStorageSignal("current_user_id");
|
|
105
|
+
const currentUserAction = USER.GET.bindParams({
|
|
106
|
+
userId: currentUserIdSignal,
|
|
107
|
+
});
|
|
108
|
+
if (currentUserIdSignal.value) {
|
|
109
|
+
currentUserAction.load();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Load initial data
|
|
113
|
+
USER.GET_MANY.load();
|
|
114
|
+
|
|
115
|
+
// eslint-disable-next-line no-unused-vars
|
|
116
|
+
const App = () => {
|
|
117
|
+
return (
|
|
118
|
+
<div style={{ display: "flex", gap: "20px" }}>
|
|
119
|
+
<div style={{ flex: 1 }}>
|
|
120
|
+
<h2>Users</h2>
|
|
121
|
+
<UsersList />
|
|
122
|
+
</div>
|
|
123
|
+
|
|
124
|
+
<div style={{ flex: 1 }}>
|
|
125
|
+
<h2>Current User Friends</h2>
|
|
126
|
+
<CurrentUserFriends />
|
|
127
|
+
</div>
|
|
128
|
+
</div>
|
|
129
|
+
);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// eslint-disable-next-line no-unused-vars
|
|
133
|
+
const UsersList = () => {
|
|
134
|
+
return (
|
|
135
|
+
<ActionRenderer action={USER.GET_MANY}>
|
|
136
|
+
{(users) => (
|
|
137
|
+
<div>
|
|
138
|
+
{users.map((user) => (
|
|
139
|
+
<UserItem key={user.id} user={user} />
|
|
140
|
+
))}
|
|
141
|
+
</div>
|
|
142
|
+
)}
|
|
143
|
+
</ActionRenderer>
|
|
144
|
+
);
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
// eslint-disable-next-line no-unused-vars
|
|
148
|
+
const UserItem = ({ user }) => {
|
|
149
|
+
const [isEditing, setIsEditing] = useState(false);
|
|
150
|
+
const [editName, setEditName] = useState(user.name);
|
|
151
|
+
|
|
152
|
+
const handleSave = async () => {
|
|
153
|
+
await USER.RENAME.bindParams({
|
|
154
|
+
userId: user.id,
|
|
155
|
+
value: editName,
|
|
156
|
+
}).reload();
|
|
157
|
+
setIsEditing(false);
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const handleCancel = () => {
|
|
161
|
+
setEditName(user.name);
|
|
162
|
+
setIsEditing(false);
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const inputRef = useRef();
|
|
166
|
+
useEffect(() => {
|
|
167
|
+
if (isEditing) {
|
|
168
|
+
inputRef.current.focus();
|
|
169
|
+
inputRef.current.select();
|
|
170
|
+
}
|
|
171
|
+
}, [isEditing]);
|
|
172
|
+
|
|
173
|
+
// Count friends
|
|
174
|
+
const friendCount = friendships[user.id]?.length || 0;
|
|
175
|
+
|
|
176
|
+
return (
|
|
177
|
+
<div
|
|
178
|
+
style={{
|
|
179
|
+
border: "1px solid #ccc",
|
|
180
|
+
padding: "10px",
|
|
181
|
+
margin: "5px 0",
|
|
182
|
+
backgroundColor:
|
|
183
|
+
currentUserIdSignal.value === user.id ? "#e6f3ff" : "white",
|
|
184
|
+
}}
|
|
185
|
+
>
|
|
186
|
+
<div style={{ display: "flex", alignItems: "center", gap: "10px" }}>
|
|
187
|
+
{isEditing ? (
|
|
188
|
+
<>
|
|
189
|
+
<input
|
|
190
|
+
ref={inputRef}
|
|
191
|
+
value={editName}
|
|
192
|
+
onChange={(e) => setEditName(e.target.value)}
|
|
193
|
+
style={{ flex: 1 }}
|
|
194
|
+
/>
|
|
195
|
+
<button onClick={handleSave}>Save</button>
|
|
196
|
+
<button onClick={handleCancel}>Cancel</button>
|
|
197
|
+
</>
|
|
198
|
+
) : (
|
|
199
|
+
<>
|
|
200
|
+
<strong style={{ flex: 1 }}>{user.name}</strong>
|
|
201
|
+
<button onClick={() => setIsEditing(true)}>Rename</button>
|
|
202
|
+
<button
|
|
203
|
+
onClick={() => {
|
|
204
|
+
currentUserIdSignal.value = user.id;
|
|
205
|
+
currentUserAction.reload();
|
|
206
|
+
}}
|
|
207
|
+
disabled={currentUserIdSignal.value === user.id}
|
|
208
|
+
>
|
|
209
|
+
Select
|
|
210
|
+
</button>
|
|
211
|
+
</>
|
|
212
|
+
)}
|
|
213
|
+
</div>
|
|
214
|
+
|
|
215
|
+
<div style={{ marginTop: "10px", fontSize: "12px", color: "#666" }}>
|
|
216
|
+
ID: {user.id} | Friends: {friendCount}
|
|
217
|
+
</div>
|
|
218
|
+
</div>
|
|
219
|
+
);
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
// eslint-disable-next-line no-unused-vars
|
|
223
|
+
const CurrentUserFriends = () => {
|
|
224
|
+
if (!currentUserIdSignal.value) {
|
|
225
|
+
return <p>No user selected</p>;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return (
|
|
229
|
+
<div>
|
|
230
|
+
<ActionStatus action={currentUserAction} />
|
|
231
|
+
<ActionRenderer action={currentUserAction}>
|
|
232
|
+
{(user) => <UserFriends user={user} />}
|
|
233
|
+
</ActionRenderer>
|
|
234
|
+
</div>
|
|
235
|
+
);
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
// eslint-disable-next-line no-unused-vars
|
|
239
|
+
const UserFriends = ({ user }) => {
|
|
240
|
+
const [isAddingFriend, setIsAddingFriend] = useState(false);
|
|
241
|
+
const [selectedFriendId, setSelectedFriendId] = useState("");
|
|
242
|
+
|
|
243
|
+
const userFriendsAction = USER_FRIENDS.GET_MANY.bindParams({
|
|
244
|
+
userId: user.id,
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
useEffect(() => {
|
|
248
|
+
userFriendsAction.load();
|
|
249
|
+
}, [user.id]);
|
|
250
|
+
|
|
251
|
+
const handleAddFriend = async () => {
|
|
252
|
+
if (!selectedFriendId) return;
|
|
253
|
+
|
|
254
|
+
await USER_FRIENDS.POST.bindParams({
|
|
255
|
+
userId: user.id,
|
|
256
|
+
friendId: selectedFriendId,
|
|
257
|
+
}).reload();
|
|
258
|
+
|
|
259
|
+
setSelectedFriendId("");
|
|
260
|
+
setIsAddingFriend(false);
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const handleRemoveFriend = async (friendId) => {
|
|
264
|
+
await USER_FRIENDS.DELETE.bindParams({
|
|
265
|
+
userId: user.id,
|
|
266
|
+
friendId,
|
|
267
|
+
}).reload();
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
// Get available users to add as friends (excluding self and existing friends)
|
|
271
|
+
const currentFriendIds = friendships[user.id] || [];
|
|
272
|
+
const availableUsers = users.filter(
|
|
273
|
+
(u) => u.id !== user.id && !currentFriendIds.includes(u.id),
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
return (
|
|
277
|
+
<div style={{ border: "2px solid #007acc", padding: "15px" }}>
|
|
278
|
+
<h3>{user.name}'s Friends</h3>
|
|
279
|
+
|
|
280
|
+
<div style={{ marginBottom: "15px" }}>
|
|
281
|
+
{isAddingFriend ? (
|
|
282
|
+
<div
|
|
283
|
+
style={{ display: "flex", gap: "10px", alignItems: "center" }}
|
|
284
|
+
>
|
|
285
|
+
<select
|
|
286
|
+
value={selectedFriendId}
|
|
287
|
+
onChange={(e) => setSelectedFriendId(e.target.value)}
|
|
288
|
+
>
|
|
289
|
+
<option value="">Select a friend to add...</option>
|
|
290
|
+
{availableUsers.map((u) => (
|
|
291
|
+
<option key={u.id} value={u.id}>
|
|
292
|
+
{u.name}
|
|
293
|
+
</option>
|
|
294
|
+
))}
|
|
295
|
+
</select>
|
|
296
|
+
<button
|
|
297
|
+
onClick={handleAddFriend}
|
|
298
|
+
disabled={!selectedFriendId}
|
|
299
|
+
>
|
|
300
|
+
Add
|
|
301
|
+
</button>
|
|
302
|
+
<button onClick={() => setIsAddingFriend(false)}>
|
|
303
|
+
Cancel
|
|
304
|
+
</button>
|
|
305
|
+
</div>
|
|
306
|
+
) : (
|
|
307
|
+
<button
|
|
308
|
+
onClick={() => setIsAddingFriend(true)}
|
|
309
|
+
disabled={availableUsers.length === 0}
|
|
310
|
+
>
|
|
311
|
+
Add Friend
|
|
312
|
+
</button>
|
|
313
|
+
)}
|
|
314
|
+
</div>
|
|
315
|
+
|
|
316
|
+
<ActionRenderer action={userFriendsAction}>
|
|
317
|
+
{(friends) => (
|
|
318
|
+
<div>
|
|
319
|
+
{friends.length === 0 ? (
|
|
320
|
+
<p style={{ color: "#666" }}>No friends yet</p>
|
|
321
|
+
) : (
|
|
322
|
+
friends.map((friend) => (
|
|
323
|
+
<div
|
|
324
|
+
key={friend.id}
|
|
325
|
+
style={{
|
|
326
|
+
display: "flex",
|
|
327
|
+
alignItems: "center",
|
|
328
|
+
justifyContent: "space-between",
|
|
329
|
+
padding: "8px",
|
|
330
|
+
margin: "5px 0",
|
|
331
|
+
backgroundColor: "#f0f0f0",
|
|
332
|
+
borderRadius: "4px",
|
|
333
|
+
}}
|
|
334
|
+
>
|
|
335
|
+
<span>{friend.name}</span>
|
|
336
|
+
<button
|
|
337
|
+
onClick={() => handleRemoveFriend(friend.id)}
|
|
338
|
+
style={{
|
|
339
|
+
background: "#ff4444",
|
|
340
|
+
color: "white",
|
|
341
|
+
border: "none",
|
|
342
|
+
padding: "4px 8px",
|
|
343
|
+
borderRadius: "4px",
|
|
344
|
+
cursor: "pointer",
|
|
345
|
+
}}
|
|
346
|
+
>
|
|
347
|
+
Remove
|
|
348
|
+
</button>
|
|
349
|
+
</div>
|
|
350
|
+
))
|
|
351
|
+
)}
|
|
352
|
+
</div>
|
|
353
|
+
)}
|
|
354
|
+
</ActionRenderer>
|
|
355
|
+
</div>
|
|
356
|
+
);
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
render(<App />, document.querySelector("#root"));
|
|
360
|
+
</script>
|
|
361
|
+
</body>
|
|
362
|
+
</html>
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<link rel="icon" href="data:," />
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
7
|
+
<title>Action demo 2</title>
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<div id="root" style="position: relative; width: 400px"></div>
|
|
11
|
+
|
|
12
|
+
<script type="module" jsenv-type="module/jsx">
|
|
13
|
+
/**
|
|
14
|
+
* Ici on test une action ayant un param qui est lu dans le local storage
|
|
15
|
+
* On peut utiliser deux actions dérivées pour charger deux users différents
|
|
16
|
+
* on peut aussi savoir qui est l'utilisateur courant
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { render } from "preact";
|
|
20
|
+
import { effect, signal } from "@preact/signals";
|
|
21
|
+
import {
|
|
22
|
+
useActionStatus,
|
|
23
|
+
resource,
|
|
24
|
+
// eslint-disable-next-line no-unused-vars
|
|
25
|
+
ActionRenderer,
|
|
26
|
+
} from "@jsenv/navi";
|
|
27
|
+
import {
|
|
28
|
+
// eslint-disable-next-line no-unused-vars
|
|
29
|
+
ActionStatus,
|
|
30
|
+
} from "./action_status.jsx";
|
|
31
|
+
|
|
32
|
+
const USER = resource("user", {
|
|
33
|
+
idKey: "name",
|
|
34
|
+
GET_MANY: () => {
|
|
35
|
+
return [{ name: "dam" }, { name: "seb" }, { name: "john" }];
|
|
36
|
+
},
|
|
37
|
+
GET: ({ name }) => {
|
|
38
|
+
return { name, greeting: `Hello ${name}` };
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const activeUsernameSignal = signal(
|
|
43
|
+
localStorage.getItem("active_user_name"),
|
|
44
|
+
);
|
|
45
|
+
effect(() => {
|
|
46
|
+
const activeUsername = activeUsernameSignal.value;
|
|
47
|
+
if (activeUsername) {
|
|
48
|
+
localStorage.setItem("active_user_name", activeUsername);
|
|
49
|
+
} else {
|
|
50
|
+
localStorage.removeItem("active_user_name");
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
USER.ACTIVE = {
|
|
55
|
+
GET: USER.GET.bindParams({
|
|
56
|
+
name: activeUsernameSignal,
|
|
57
|
+
}),
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
if (activeUsernameSignal.peek()) {
|
|
61
|
+
USER.ACTIVE.GET.load();
|
|
62
|
+
}
|
|
63
|
+
USER.GET_MANY.load();
|
|
64
|
+
|
|
65
|
+
// eslint-disable-next-line no-unused-vars
|
|
66
|
+
const App = () => {
|
|
67
|
+
const { data: users } = useActionStatus(USER.GET_MANY);
|
|
68
|
+
|
|
69
|
+
const [dam, seb, john] = users;
|
|
70
|
+
const getDamAction = USER.GET.bindParams({
|
|
71
|
+
name: dam.name,
|
|
72
|
+
});
|
|
73
|
+
const getSebAction = USER.GET.bindParams({
|
|
74
|
+
name: seb.name,
|
|
75
|
+
});
|
|
76
|
+
const getJohnAction = USER.GET.bindParams({
|
|
77
|
+
name: john.name,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<div>
|
|
82
|
+
<button
|
|
83
|
+
onClick={() => {
|
|
84
|
+
getDamAction.load();
|
|
85
|
+
activeUsernameSignal.value = dam.name;
|
|
86
|
+
}}
|
|
87
|
+
>
|
|
88
|
+
Load dam
|
|
89
|
+
</button>
|
|
90
|
+
<button
|
|
91
|
+
onClick={() => {
|
|
92
|
+
getDamAction.unload();
|
|
93
|
+
}}
|
|
94
|
+
>
|
|
95
|
+
Unload dam
|
|
96
|
+
</button>
|
|
97
|
+
<button
|
|
98
|
+
onClick={() => {
|
|
99
|
+
getDamAction.preload();
|
|
100
|
+
}}
|
|
101
|
+
>
|
|
102
|
+
Preload dam
|
|
103
|
+
</button>
|
|
104
|
+
<br />
|
|
105
|
+
<button
|
|
106
|
+
onClick={() => {
|
|
107
|
+
getSebAction.load();
|
|
108
|
+
activeUsernameSignal.value = seb.name;
|
|
109
|
+
}}
|
|
110
|
+
>
|
|
111
|
+
Load seb
|
|
112
|
+
</button>
|
|
113
|
+
<button
|
|
114
|
+
onClick={() => {
|
|
115
|
+
getSebAction.unload();
|
|
116
|
+
}}
|
|
117
|
+
>
|
|
118
|
+
Unload seb
|
|
119
|
+
</button>
|
|
120
|
+
<button
|
|
121
|
+
onClick={() => {
|
|
122
|
+
getSebAction.preload();
|
|
123
|
+
}}
|
|
124
|
+
>
|
|
125
|
+
Preload seb
|
|
126
|
+
</button>
|
|
127
|
+
<br />
|
|
128
|
+
<button
|
|
129
|
+
onClick={() => {
|
|
130
|
+
getJohnAction.load();
|
|
131
|
+
activeUsernameSignal.value = john.name;
|
|
132
|
+
}}
|
|
133
|
+
>
|
|
134
|
+
Load john
|
|
135
|
+
</button>
|
|
136
|
+
<button
|
|
137
|
+
onClick={() => {
|
|
138
|
+
getJohnAction.unload();
|
|
139
|
+
}}
|
|
140
|
+
>
|
|
141
|
+
Unload john
|
|
142
|
+
</button>
|
|
143
|
+
<button
|
|
144
|
+
onClick={() => {
|
|
145
|
+
getJohnAction.preload();
|
|
146
|
+
}}
|
|
147
|
+
>
|
|
148
|
+
Preload john
|
|
149
|
+
</button>
|
|
150
|
+
|
|
151
|
+
<ActionStatus action={getDamAction} />
|
|
152
|
+
<ActionStatus action={getSebAction} />
|
|
153
|
+
<ActionStatus action={getJohnAction} />
|
|
154
|
+
<ActionStatus action={USER.ACTIVE.GET} />
|
|
155
|
+
|
|
156
|
+
<ActionRenderer action={USER.ACTIVE.GET}>
|
|
157
|
+
{{
|
|
158
|
+
loading: () => <div>Loading user...</div>,
|
|
159
|
+
error: (error) => <div>Error: {error.message}</div>,
|
|
160
|
+
unloaded: () => <div>User not loaded</div>,
|
|
161
|
+
loaded: (user) => {
|
|
162
|
+
return <div>{user.greeting}</div>;
|
|
163
|
+
},
|
|
164
|
+
}}
|
|
165
|
+
</ActionRenderer>
|
|
166
|
+
</div>
|
|
167
|
+
);
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
render(<App />, document.querySelector("#root"));
|
|
171
|
+
</script>
|
|
172
|
+
</body>
|
|
173
|
+
</html>
|