-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform-hooks.js
More file actions
114 lines (97 loc) · 2.76 KB
/
Copy pathplatform-hooks.js
File metadata and controls
114 lines (97 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const React = require('react');
const store = {
users: [
{
id: 'seed-shop-owner',
username: 'owner',
password: 'owner123',
role: 'shop_owner',
created_at: new Date().toISOString()
},
{
id: 'seed-customer',
username: 'customer',
password: 'customer123',
role: 'customer',
created_at: new Date().toISOString()
}
],
customers: [],
transactions: []
};
const subscribers = new Set();
function notifySubscribers() {
subscribers.forEach((callback) => callback());
}
function matchesFilter(doc, filter) {
const entries = Object.entries(filter || {});
if (entries.length === 0) return true;
return entries.every(([key, value]) => doc[key] === value);
}
function getCollectionData(collection, filter) {
const rows = Array.isArray(store[collection]) ? store[collection] : [];
return rows.filter((row) => matchesFilter(row, filter));
}
function useQuery(collection, filter) {
const [, forceRender] = React.useState(0);
React.useEffect(() => {
const onStoreChange = () => forceRender((prev) => prev + 1);
subscribers.add(onStoreChange);
return () => {
subscribers.delete(onStoreChange);
};
}, []);
const data = React.useMemo(() => {
return getCollectionData(collection, filter || {});
}, [collection, JSON.stringify(filter || {}), store[collection]?.length]);
const refetch = React.useCallback(() => {
forceRender((prev) => prev + 1);
}, []);
return {
data,
loading: false,
error: null,
refetch
};
}
function useMutation(collection, operation) {
const mutate = React.useCallback(
async (payload) => {
if (!store[collection]) {
store[collection] = [];
}
if (operation === 'insert') {
store[collection].push(payload);
notifySubscribers();
return payload;
}
if (operation === 'update') {
if (!payload || !payload.id) {
throw new Error('Update requires payload.id');
}
const index = store[collection].findIndex((row) => row.id === payload.id);
if (index === -1) {
throw new Error('Record not found');
}
store[collection][index] = { ...store[collection][index], ...payload };
notifySubscribers();
return store[collection][index];
}
if (operation === 'delete') {
if (!payload || !payload.id) {
throw new Error('Delete requires payload.id');
}
store[collection] = store[collection].filter((row) => row.id !== payload.id);
notifySubscribers();
return { id: payload.id };
}
throw new Error(`Unsupported mutation operation: ${operation}`);
},
[collection, operation]
);
return { mutate };
}
module.exports = {
useQuery,
useMutation
};