@aventoapp/framework 0.1.0
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/dist/index.js +6 -0
- package/dist/index.js.map +7 -0
- package/dist/native/index.js +2522 -0
- package/dist/native/index.js.map +7 -0
- package/dist/server/index.js +2776 -0
- package/dist/server/index.js.map +7 -0
- package/dist/server/workflow/index.js +537 -0
- package/dist/server/workflow/index.js.map +7 -0
- package/dist/shared/index.js +62 -0
- package/dist/shared/index.js.map +7 -0
- package/dist/web/index.js +7323 -0
- package/dist/web/index.js.map +7 -0
- package/package.json +81 -0
- package/schemas/Workflow.json +14 -0
- package/schemas/WorkflowHistory.json +22 -0
- package/schemas/WorkflowInstance.json +16 -0
- package/schemas/WorkflowState.json +18 -0
- package/schemas/WorkflowTransition.json +27 -0
- package/src/index.js +8 -0
- package/src/native/components/NativeAuthProvider.jsx +249 -0
- package/src/native/components/NativeDetail.jsx +270 -0
- package/src/native/components/NativeForm.jsx +543 -0
- package/src/native/components/NativeGrid.jsx +90 -0
- package/src/native/components/NativeJsonGrid.jsx +213 -0
- package/src/native/components/NativeJsonView.jsx +454 -0
- package/src/native/components/NativeJsonViewContext.js +17 -0
- package/src/native/components/NativeShell.jsx +68 -0
- package/src/native/components/NativeViewComponent.jsx +351 -0
- package/src/native/extensions/NativeExtensionRegistry.js +81 -0
- package/src/native/hooks/useNativeStore.js +72 -0
- package/src/native/index.js +15 -0
- package/src/native/layouts/NativeStackLayout.jsx +30 -0
- package/src/native/layouts/NativeTabLayout.jsx +96 -0
- package/src/native/storage.js +13 -0
- package/src/native/stores/NativeBaseStore.js +348 -0
- package/src/native/stores/NativeStoreContext.jsx +53 -0
- package/src/server/ai/capabilities-routes.js +106 -0
- package/src/server/ai/index.js +1 -0
- package/src/server/cache/MemoryCache.js +153 -0
- package/src/server/cache/index.js +2 -0
- package/src/server/cache/middleware.js +12 -0
- package/src/server/db/index.js +14 -0
- package/src/server/db/mongo.js +137 -0
- package/src/server/db/postgres.js +144 -0
- package/src/server/index.js +38 -0
- package/src/server/middleware/auth.js +682 -0
- package/src/server/middleware/can-flags.js +132 -0
- package/src/server/middleware/entityRoles.js +105 -0
- package/src/server/middleware/index.js +58 -0
- package/src/server/middleware/permissions.js +240 -0
- package/src/server/middleware/tenant.js +203 -0
- package/src/server/realtime/EventBus.js +45 -0
- package/src/server/realtime/WebSocketServer.js +105 -0
- package/src/server/realtime/index.js +3 -0
- package/src/server/realtime/middleware.js +18 -0
- package/src/server/services/email-templates.js +157 -0
- package/src/server/services/email.js +78 -0
- package/src/server/services/index.js +15 -0
- package/src/server/services/storage.js +119 -0
- package/src/server/utils/index.js +16 -0
- package/src/server/utils/paginate.js +65 -0
- package/src/server/utils/query-builder.js +293 -0
- package/src/server/utils/route-loader.js +61 -0
- package/src/server/workflow/WorkflowEngine.js +494 -0
- package/src/server/workflow/index.js +2 -0
- package/src/server/workflow/workflow-routes.js +174 -0
- package/src/shared/index.js +10 -0
- package/src/shared/utils/field-accessor.js +108 -0
- package/src/shared/utils/index.js +7 -0
- package/src/shared/utils/validation-utils.js +83 -0
- package/src/web/components/AppRouter/AppRouter.jsx +130 -0
- package/src/web/components/AppRouter/index.js +1 -0
- package/src/web/components/AppShell/AppShell.jsx +101 -0
- package/src/web/components/AppShell/DefaultUserMenu.jsx +54 -0
- package/src/web/components/AppShell/ShellConfig.js +53 -0
- package/src/web/components/AppShell/index.js +3 -0
- package/src/web/components/AuthProvider.jsx +426 -0
- package/src/web/components/CardList/CardList.jsx +272 -0
- package/src/web/components/CardList/index.js +2 -0
- package/src/web/components/DetailView/DetailView.jsx +281 -0
- package/src/web/components/DetailView/index.js +2 -0
- package/src/web/components/Form/Form.jsx +300 -0
- package/src/web/components/Form/FormField.jsx +226 -0
- package/src/web/components/Form/index.js +3 -0
- package/src/web/components/Grid/Grid.jsx +304 -0
- package/src/web/components/Grid/index.js +2 -0
- package/src/web/components/IconRegistry/IconRegistry.jsx +127 -0
- package/src/web/components/IconRegistry/index.js +1 -0
- package/src/web/components/JsonView/JsonView.jsx +647 -0
- package/src/web/components/JsonView/ViewComponent.jsx +1045 -0
- package/src/web/components/JsonView/ViewToolbar.jsx +141 -0
- package/src/web/components/JsonView/index.js +14 -0
- package/src/web/components/JsonView/layouts/SingleLayout.jsx +41 -0
- package/src/web/components/JsonView/layouts/SplitLayout.jsx +122 -0
- package/src/web/components/JsonView/layouts/TabLayout.jsx +112 -0
- package/src/web/components/JsonView/layouts/index.js +8 -0
- package/src/web/components/Layout.jsx +265 -0
- package/src/web/components/MobileLayout/MobileLayout.jsx +385 -0
- package/src/web/components/MobileLayout/index.js +1 -0
- package/src/web/components/Navigation/Navigation.jsx +248 -0
- package/src/web/components/Navigation/index.js +1 -0
- package/src/web/components/NotificationProvider.jsx +55 -0
- package/src/web/components/Pagination/InfiniteScroll.jsx +82 -0
- package/src/web/components/Pagination/Pagination.jsx +73 -0
- package/src/web/components/Pagination/index.js +8 -0
- package/src/web/components/SimpleList/SimpleList.jsx +177 -0
- package/src/web/components/SimpleList/index.js +2 -0
- package/src/web/components/TenantProvider.jsx +121 -0
- package/src/web/components/WorkflowActions/WorkflowActions.jsx +71 -0
- package/src/web/components/WorkflowActions/index.js +1 -0
- package/src/web/components/WorkflowField/WorkflowField.jsx +176 -0
- package/src/web/components/WorkflowField/index.js +2 -0
- package/src/web/components/WorkflowField/workflowColors.js +32 -0
- package/src/web/components/auth/DevTenantSelector.jsx +90 -0
- package/src/web/components/auth/ForgotPasswordForm.jsx +94 -0
- package/src/web/components/auth/RegisterForm.jsx +168 -0
- package/src/web/components/auth/ResetPasswordForm.jsx +121 -0
- package/src/web/components/auth/VerifyEmailView.jsx +85 -0
- package/src/web/components/auth/index.js +5 -0
- package/src/web/components/index.js +39 -0
- package/src/web/components/layouts/CardListLayout.jsx +68 -0
- package/src/web/components/layouts/FullscreenLayout.jsx +30 -0
- package/src/web/components/layouts/MobileStackLayout.jsx +28 -0
- package/src/web/components/layouts/WizardLayout.jsx +86 -0
- package/src/web/components/layouts/index.js +4 -0
- package/src/web/components/mobile/Badge.jsx +105 -0
- package/src/web/components/mobile/BadgeThemeEditor.jsx +104 -0
- package/src/web/components/mobile/EmptyState.jsx +30 -0
- package/src/web/components/mobile/QuickActionButton.jsx +77 -0
- package/src/web/components/mobile/ServerAutocomplete.jsx +123 -0
- package/src/web/components/mobile/StarRating.jsx +42 -0
- package/src/web/components/mobile/StatCard.jsx +65 -0
- package/src/web/components/mobile/index.js +7 -0
- package/src/web/extensions/ExtensionRegistry.js +79 -0
- package/src/web/extensions/index.js +7 -0
- package/src/web/hooks/index.js +15 -0
- package/src/web/hooks/useApi.js +76 -0
- package/src/web/hooks/useAuth.js +37 -0
- package/src/web/hooks/useDebouncedSave.js +158 -0
- package/src/web/hooks/useIsMobile.js +14 -0
- package/src/web/hooks/useOfflineQueue.js +99 -0
- package/src/web/hooks/useSelectionModel.js +143 -0
- package/src/web/hooks/useServerAutocomplete.js +146 -0
- package/src/web/hooks/useStore.js +107 -0
- package/src/web/hooks/useSubscription.js +130 -0
- package/src/web/hooks/useTenant.js +39 -0
- package/src/web/hooks/useTenantResolver.js +65 -0
- package/src/web/hooks/useUndoRedo.js +77 -0
- package/src/web/hooks/useViews.js +113 -0
- package/src/web/hooks/useWorkflow.js +190 -0
- package/src/web/index.js +117 -0
- package/src/web/stores/BaseStore.js +439 -0
- package/src/web/stores/StoreContext.jsx +93 -0
- package/src/web/stores/index.js +3 -0
- package/src/web/theme/index.js +3 -0
- package/src/web/theme/mui-theme.js +228 -0
- package/src/web/utils/formatTimeAgo.js +22 -0
- package/src/web/utils/index.js +1 -0
- package/src/web/views/ViewRegistry.js +235 -0
- package/src/web/views/index.js +2 -0
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BaseStore
|
|
3
|
+
*
|
|
4
|
+
* Base class for entity stores with CRUD operations and reactive state.
|
|
5
|
+
* Automatically integrates with auth and tenant contexts.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Convert camelCase to snake_case
|
|
10
|
+
*/
|
|
11
|
+
function toSnakeCase(str) {
|
|
12
|
+
return str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Convert snake_case to camelCase
|
|
17
|
+
*/
|
|
18
|
+
function toCamelCase(str) {
|
|
19
|
+
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Transform object keys from camelCase to snake_case
|
|
24
|
+
*/
|
|
25
|
+
function toSnakeCaseKeys(obj) {
|
|
26
|
+
if (obj === null || typeof obj !== 'object') return obj;
|
|
27
|
+
if (Array.isArray(obj)) return obj.map(toSnakeCaseKeys);
|
|
28
|
+
|
|
29
|
+
return Object.keys(obj).reduce((acc, key) => {
|
|
30
|
+
acc[toSnakeCase(key)] = toSnakeCaseKeys(obj[key]);
|
|
31
|
+
return acc;
|
|
32
|
+
}, {});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Transform object keys from snake_case to camelCase
|
|
37
|
+
*/
|
|
38
|
+
function toCamelCaseKeys(obj) {
|
|
39
|
+
if (obj === null || typeof obj !== 'object') return obj;
|
|
40
|
+
if (Array.isArray(obj)) return obj.map(toCamelCaseKeys);
|
|
41
|
+
|
|
42
|
+
return Object.keys(obj).reduce((acc, key) => {
|
|
43
|
+
acc[toCamelCase(key)] = toCamelCaseKeys(obj[key]);
|
|
44
|
+
return acc;
|
|
45
|
+
}, {});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export class BaseStore {
|
|
49
|
+
/**
|
|
50
|
+
* Create a new store
|
|
51
|
+
* @param {string} entityName - Name of the entity (e.g., 'Task')
|
|
52
|
+
* @param {string} endpoint - API endpoint (e.g., '/api/tasks')
|
|
53
|
+
*/
|
|
54
|
+
constructor(entityName, endpoint) {
|
|
55
|
+
this.entityName = entityName;
|
|
56
|
+
this.endpoint = endpoint;
|
|
57
|
+
|
|
58
|
+
// Auth and tenant context injectors (set by StoreProvider)
|
|
59
|
+
this._getAuthHeader = null;
|
|
60
|
+
this._getTenantHeader = null;
|
|
61
|
+
|
|
62
|
+
// Reactive state
|
|
63
|
+
this._state = {
|
|
64
|
+
items: [],
|
|
65
|
+
selectedItem: null,
|
|
66
|
+
loading: false,
|
|
67
|
+
error: null,
|
|
68
|
+
pagination: {
|
|
69
|
+
page: 1,
|
|
70
|
+
pageSize: 25,
|
|
71
|
+
total: 0,
|
|
72
|
+
totalPages: 0,
|
|
73
|
+
hasNext: false,
|
|
74
|
+
hasPrev: false
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// Subscribers for React integration
|
|
79
|
+
this._subscribers = new Set();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Initialize store with auth/tenant context injectors
|
|
84
|
+
* Called by StoreProvider
|
|
85
|
+
*/
|
|
86
|
+
_initialize(getAuthHeader, getTenantHeader) {
|
|
87
|
+
this._getAuthHeader = getAuthHeader;
|
|
88
|
+
this._getTenantHeader = getTenantHeader;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Get combined headers for API requests
|
|
93
|
+
*/
|
|
94
|
+
_getHeaders() {
|
|
95
|
+
const headers = {
|
|
96
|
+
'Content-Type': 'application/json'
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
if (this._getAuthHeader) {
|
|
100
|
+
const authHeader = this._getAuthHeader();
|
|
101
|
+
if (authHeader.Authorization) {
|
|
102
|
+
headers.Authorization = authHeader.Authorization;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (this._getTenantHeader) {
|
|
107
|
+
const tenantHeader = this._getTenantHeader();
|
|
108
|
+
if (tenantHeader['X-Tenant-ID']) {
|
|
109
|
+
headers['X-Tenant-ID'] = tenantHeader['X-Tenant-ID'];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return headers;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Get current state
|
|
118
|
+
*/
|
|
119
|
+
getState() {
|
|
120
|
+
return this._state;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Update state and notify subscribers
|
|
125
|
+
*/
|
|
126
|
+
_setState(updates) {
|
|
127
|
+
this._state = { ...this._state, ...updates };
|
|
128
|
+
this._notifySubscribers();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Subscribe to state changes
|
|
133
|
+
* Returns unsubscribe function
|
|
134
|
+
*/
|
|
135
|
+
subscribe(callback) {
|
|
136
|
+
this._subscribers.add(callback);
|
|
137
|
+
return () => this._subscribers.delete(callback);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Notify all subscribers of state change
|
|
142
|
+
*/
|
|
143
|
+
_notifySubscribers() {
|
|
144
|
+
this._subscribers.forEach(callback => callback(this._state));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Handle API errors
|
|
149
|
+
*/
|
|
150
|
+
async _handleResponse(response) {
|
|
151
|
+
if (!response.ok) {
|
|
152
|
+
const error = await response.json().catch(() => ({ message: 'Request failed' }));
|
|
153
|
+
throw new Error(error.message || `HTTP ${response.status}`);
|
|
154
|
+
}
|
|
155
|
+
return response.json();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* List all items
|
|
160
|
+
* @param {Object} options - Query options (page, pageSize, sort, filter, append, sliceId)
|
|
161
|
+
* @param {boolean} options.append - If true, append items to existing list (for infinite scroll)
|
|
162
|
+
* @param {string} options.sliceId - Slice ID for pre-generated JOIN queries
|
|
163
|
+
*/
|
|
164
|
+
async list(options = {}) {
|
|
165
|
+
this._setState({ loading: true, error: null });
|
|
166
|
+
|
|
167
|
+
try {
|
|
168
|
+
const queryParams = new URLSearchParams();
|
|
169
|
+
if (options.page) queryParams.set('page', options.page);
|
|
170
|
+
if (options.pageSize) queryParams.set('pageSize', options.pageSize);
|
|
171
|
+
if (options.sort) queryParams.set('sort', options.sort);
|
|
172
|
+
if (options.sortDirection) queryParams.set('sortDirection', options.sortDirection);
|
|
173
|
+
if (options.sliceId) queryParams.set('slice', options.sliceId);
|
|
174
|
+
if (options.filters) {
|
|
175
|
+
for (const [key, value] of Object.entries(options.filters)) {
|
|
176
|
+
queryParams.set(key, value);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const url = queryParams.toString()
|
|
181
|
+
? `${this.endpoint}?${queryParams}`
|
|
182
|
+
: this.endpoint;
|
|
183
|
+
|
|
184
|
+
const response = await fetch(url, {
|
|
185
|
+
method: 'GET',
|
|
186
|
+
headers: this._getHeaders()
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
const data = await this._handleResponse(response);
|
|
190
|
+
|
|
191
|
+
// Handle both array responses (legacy) and paginated responses
|
|
192
|
+
let items, paginationInfo;
|
|
193
|
+
if (Array.isArray(data)) {
|
|
194
|
+
// Legacy array response - backward compatible
|
|
195
|
+
items = data;
|
|
196
|
+
paginationInfo = {
|
|
197
|
+
page: options.page || 1,
|
|
198
|
+
pageSize: options.pageSize || this._state.pagination.pageSize,
|
|
199
|
+
total: items.length,
|
|
200
|
+
totalPages: 1,
|
|
201
|
+
hasNext: false,
|
|
202
|
+
hasPrev: false
|
|
203
|
+
};
|
|
204
|
+
} else {
|
|
205
|
+
// New paginated response format { data: [], pagination: {} }
|
|
206
|
+
items = data.data || data.items || [];
|
|
207
|
+
paginationInfo = data.pagination || {
|
|
208
|
+
page: options.page || 1,
|
|
209
|
+
pageSize: options.pageSize || this._state.pagination.pageSize,
|
|
210
|
+
total: data.total || items.length,
|
|
211
|
+
totalPages: Math.ceil((data.total || items.length) / (options.pageSize || this._state.pagination.pageSize)),
|
|
212
|
+
hasNext: false,
|
|
213
|
+
hasPrev: false
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const transformedItems = items.map(toCamelCaseKeys);
|
|
218
|
+
|
|
219
|
+
// Append mode for infinite scroll
|
|
220
|
+
const finalItems = options.append
|
|
221
|
+
? [...this._state.items, ...transformedItems]
|
|
222
|
+
: transformedItems;
|
|
223
|
+
|
|
224
|
+
this._setState({
|
|
225
|
+
items: finalItems,
|
|
226
|
+
loading: false,
|
|
227
|
+
pagination: paginationInfo
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
return this._state.items;
|
|
231
|
+
} catch (error) {
|
|
232
|
+
this._setState({ loading: false, error: error.message });
|
|
233
|
+
throw error;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Load more items for infinite scroll
|
|
239
|
+
* Fetches the next page and appends to existing items
|
|
240
|
+
*/
|
|
241
|
+
async loadMore() {
|
|
242
|
+
const { pagination } = this._state;
|
|
243
|
+
|
|
244
|
+
// Don't load if already loading or no more pages
|
|
245
|
+
if (this._state.loading || !pagination.hasNext) {
|
|
246
|
+
return this._state.items;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return this.list({
|
|
250
|
+
page: pagination.page + 1,
|
|
251
|
+
pageSize: pagination.pageSize,
|
|
252
|
+
append: true
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Get single item by ID
|
|
258
|
+
* @param {string} id - Item ID
|
|
259
|
+
* @param {Object} options - Query options
|
|
260
|
+
* @param {string} options.sliceId - Slice ID for pre-generated JOIN queries
|
|
261
|
+
*/
|
|
262
|
+
async get(id, options = {}) {
|
|
263
|
+
this._setState({ loading: true, error: null });
|
|
264
|
+
|
|
265
|
+
try {
|
|
266
|
+
const queryParams = new URLSearchParams();
|
|
267
|
+
if (options.sliceId) queryParams.set('slice', options.sliceId);
|
|
268
|
+
|
|
269
|
+
const url = queryParams.toString()
|
|
270
|
+
? `${this.endpoint}/${id}?${queryParams}`
|
|
271
|
+
: `${this.endpoint}/${id}`;
|
|
272
|
+
|
|
273
|
+
const response = await fetch(url, {
|
|
274
|
+
method: 'GET',
|
|
275
|
+
headers: this._getHeaders()
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
const data = await this._handleResponse(response);
|
|
279
|
+
const item = toCamelCaseKeys(data);
|
|
280
|
+
|
|
281
|
+
this._setState({
|
|
282
|
+
selectedItem: item,
|
|
283
|
+
loading: false
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
return item;
|
|
287
|
+
} catch (error) {
|
|
288
|
+
this._setState({ loading: false, error: error.message });
|
|
289
|
+
throw error;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Create new item
|
|
295
|
+
* @param {Object} data - Item data
|
|
296
|
+
*/
|
|
297
|
+
async create(data) {
|
|
298
|
+
this._setState({ loading: true, error: null });
|
|
299
|
+
|
|
300
|
+
try {
|
|
301
|
+
const response = await fetch(this.endpoint, {
|
|
302
|
+
method: 'POST',
|
|
303
|
+
headers: this._getHeaders(),
|
|
304
|
+
body: JSON.stringify(toSnakeCaseKeys(data))
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
const result = await this._handleResponse(response);
|
|
308
|
+
const item = toCamelCaseKeys(result);
|
|
309
|
+
|
|
310
|
+
this._setState({
|
|
311
|
+
items: [item, ...this._state.items],
|
|
312
|
+
loading: false
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
return item;
|
|
316
|
+
} catch (error) {
|
|
317
|
+
this._setState({ loading: false, error: error.message });
|
|
318
|
+
throw error;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Update existing item
|
|
324
|
+
* @param {string} id - Item ID
|
|
325
|
+
* @param {Object} data - Updated data
|
|
326
|
+
*/
|
|
327
|
+
async update(id, data) {
|
|
328
|
+
this._setState({ loading: true, error: null });
|
|
329
|
+
|
|
330
|
+
try {
|
|
331
|
+
const response = await fetch(`${this.endpoint}/${id}`, {
|
|
332
|
+
method: 'PUT',
|
|
333
|
+
headers: this._getHeaders(),
|
|
334
|
+
body: JSON.stringify(toSnakeCaseKeys(data))
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
const result = await this._handleResponse(response);
|
|
338
|
+
const item = toCamelCaseKeys(result);
|
|
339
|
+
|
|
340
|
+
this._setState({
|
|
341
|
+
items: this._state.items.map(i => i.id === id ? item : i),
|
|
342
|
+
selectedItem: this._state.selectedItem?.id === id ? item : this._state.selectedItem,
|
|
343
|
+
loading: false
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
return item;
|
|
347
|
+
} catch (error) {
|
|
348
|
+
this._setState({ loading: false, error: error.message });
|
|
349
|
+
throw error;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Delete item
|
|
355
|
+
* @param {string} id - Item ID
|
|
356
|
+
*/
|
|
357
|
+
async delete(id) {
|
|
358
|
+
this._setState({ loading: true, error: null });
|
|
359
|
+
|
|
360
|
+
try {
|
|
361
|
+
const response = await fetch(`${this.endpoint}/${id}`, {
|
|
362
|
+
method: 'DELETE',
|
|
363
|
+
headers: this._getHeaders()
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
if (!response.ok && response.status !== 204) {
|
|
367
|
+
await this._handleResponse(response);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
this._setState({
|
|
371
|
+
items: this._state.items.filter(i => i.id !== id),
|
|
372
|
+
selectedItem: this._state.selectedItem?.id === id ? null : this._state.selectedItem,
|
|
373
|
+
loading: false
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
return true;
|
|
377
|
+
} catch (error) {
|
|
378
|
+
this._setState({ loading: false, error: error.message });
|
|
379
|
+
throw error;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Select an item
|
|
385
|
+
* @param {Object|null} item - Item to select or null to clear
|
|
386
|
+
*/
|
|
387
|
+
select(item) {
|
|
388
|
+
this._setState({ selectedItem: item });
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Clear selection
|
|
393
|
+
*/
|
|
394
|
+
clearSelection() {
|
|
395
|
+
this._setState({ selectedItem: null });
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Clear error state
|
|
400
|
+
*/
|
|
401
|
+
clearError() {
|
|
402
|
+
this._setState({ error: null });
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Reset store to initial state
|
|
407
|
+
*/
|
|
408
|
+
reset() {
|
|
409
|
+
this._setState({
|
|
410
|
+
items: [],
|
|
411
|
+
selectedItem: null,
|
|
412
|
+
loading: false,
|
|
413
|
+
error: null,
|
|
414
|
+
pagination: {
|
|
415
|
+
page: 1,
|
|
416
|
+
pageSize: 25,
|
|
417
|
+
total: 0,
|
|
418
|
+
totalPages: 0,
|
|
419
|
+
hasNext: false,
|
|
420
|
+
hasPrev: false
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Set page size
|
|
427
|
+
* @param {number} pageSize - New page size
|
|
428
|
+
*/
|
|
429
|
+
setPageSize(pageSize) {
|
|
430
|
+
this._setState({
|
|
431
|
+
pagination: {
|
|
432
|
+
...this._state.pagination,
|
|
433
|
+
pageSize
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
export default BaseStore;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* StoreContext
|
|
3
|
+
*
|
|
4
|
+
* React context provider that injects auth/tenant contexts into all stores.
|
|
5
|
+
*/
|
|
6
|
+
import React, { createContext, useContext, useMemo, useRef } from 'react';
|
|
7
|
+
import { useAuth } from '../hooks/useAuth.js';
|
|
8
|
+
import { useTenant } from '../hooks/useTenant.js';
|
|
9
|
+
|
|
10
|
+
export const StoreContext = createContext(null);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* StoreProvider component
|
|
14
|
+
*
|
|
15
|
+
* Wraps the app to provide store access with automatic auth/tenant integration.
|
|
16
|
+
*
|
|
17
|
+
* @param {Object} props
|
|
18
|
+
* @param {Object} props.stores - Object mapping store names to store instances
|
|
19
|
+
* @param {React.ReactNode} props.children
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* import { taskStore } from './stores/task.store';
|
|
23
|
+
*
|
|
24
|
+
* const stores = { task: taskStore };
|
|
25
|
+
*
|
|
26
|
+
* function App() {
|
|
27
|
+
* return (
|
|
28
|
+
* <AuthProvider>
|
|
29
|
+
* <TenantProvider>
|
|
30
|
+
* <StoreProvider stores={stores}>
|
|
31
|
+
* <MyApp />
|
|
32
|
+
* </StoreProvider>
|
|
33
|
+
* </TenantProvider>
|
|
34
|
+
* </AuthProvider>
|
|
35
|
+
* );
|
|
36
|
+
* }
|
|
37
|
+
*/
|
|
38
|
+
export function StoreProvider({ stores = {}, children }) {
|
|
39
|
+
const { getAuthHeader } = useAuth();
|
|
40
|
+
const { getTenantHeader } = useTenant();
|
|
41
|
+
|
|
42
|
+
// Keep stable references
|
|
43
|
+
const storesRef = useRef(stores);
|
|
44
|
+
const getAuthHeaderRef = useRef(getAuthHeader);
|
|
45
|
+
const getTenantHeaderRef = useRef(getTenantHeader);
|
|
46
|
+
|
|
47
|
+
// Update refs when values change
|
|
48
|
+
getAuthHeaderRef.current = getAuthHeader;
|
|
49
|
+
getTenantHeaderRef.current = getTenantHeader;
|
|
50
|
+
|
|
51
|
+
// Initialize all stores with context injectors synchronously so they are
|
|
52
|
+
// ready before child useEffect hooks fire (child effects run before parent effects).
|
|
53
|
+
const initializedRef = useRef(false);
|
|
54
|
+
if (!initializedRef.current) {
|
|
55
|
+
Object.values(storesRef.current).forEach(store => {
|
|
56
|
+
if (store && typeof store._initialize === 'function') {
|
|
57
|
+
store._initialize(
|
|
58
|
+
() => getAuthHeaderRef.current(),
|
|
59
|
+
() => getTenantHeaderRef.current()
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
initializedRef.current = true;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Context value with getStore accessor
|
|
67
|
+
const contextValue = useMemo(() => ({
|
|
68
|
+
stores: storesRef.current,
|
|
69
|
+
getStore: (name) => storesRef.current[name] || null
|
|
70
|
+
}), []);
|
|
71
|
+
|
|
72
|
+
return (
|
|
73
|
+
<StoreContext.Provider value={contextValue}>
|
|
74
|
+
{children}
|
|
75
|
+
</StoreContext.Provider>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Hook to access the store context
|
|
81
|
+
* @returns {Object} Store context with getStore function
|
|
82
|
+
*/
|
|
83
|
+
export function useStoreContext() {
|
|
84
|
+
const context = useContext(StoreContext);
|
|
85
|
+
|
|
86
|
+
if (context === null) {
|
|
87
|
+
throw new Error('useStoreContext must be used within a StoreProvider');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return context;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export default StoreProvider;
|