-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdataProvider.tsx
73 lines (65 loc) · 2.12 KB
/
dataProvider.tsx
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
import { withLifecycleCallbacks } from "react-admin";
import fakeRestProvider from "ra-data-fakerest";
import { addSearchMethod } from "@react-admin/ra-search";
import {
addRealTimeMethodsInLocalBrowser,
addLocksMethodsBasedOnALockResource,
} from "@react-admin/ra-realtime";
import { data } from "./data";
import { simulateRealtime } from "./simulateRealtime";
const baseDataProvider = fakeRestProvider(data, true);
const dataProviderWithSearch = addSearchMethod(baseDataProvider, {
tickets: {
label: "subject",
redirect: "show",
},
customers: {
label: (record: any) => `${record.firstName} ${record.lastName}`,
redirect: "show",
},
products: { label: "model", redirect: "show" },
});
const dataProviderWithRealtime = addLocksMethodsBasedOnALockResource(
addRealTimeMethodsInLocalBrowser(dataProviderWithSearch),
"locks",
true
);
export const dataProvider = withLifecycleCallbacks(dataProviderWithRealtime, [
{
resource: "tickets",
afterSave: async (ticket, dataProvider) => {
// update the related customer
await dataProvider.update("customers", {
id: ticket.customer_id,
data: { updated_at: ticket.updated_at },
previousData: {},
});
return ticket;
},
beforeDelete: async (params, dataProvider) => {
// delete the related messages
const messages = await dataProvider.getList("messages", {
pagination: { page: 1, perPage: 100 },
sort: { field: "id", order: "ASC" },
filter: { ticket_id: params.id },
});
await dataProvider.deleteMany("messages", {
ids: messages.data.map((m) => m.id),
});
return params;
},
beforeDeleteMany: async (params, dataProvider) => {
// delete the related messages
const messages = await dataProvider.getList("messages", {
pagination: { page: 1, perPage: 100 },
sort: { field: "id", order: "ASC" },
filter: { ticket_id: params.ids },
});
await dataProvider.deleteMany("messages", {
ids: messages.data.map((m) => m.id),
});
return params;
},
},
]);
simulateRealtime(dataProvider);