forked from refinedev/refine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-side-validation-mantine.tsx
178 lines (166 loc) · 5.34 KB
/
server-side-validation-mantine.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { Sandpack } from "@site/src/components/sandpack";
import React from "react";
export default function ServerSideValidationMantine() {
return (
<Sandpack
height={460}
showOpenInCodeSandbox={false}
dependencies={{
"@refinedev/mantine": "^2.28.21",
"@refinedev/core": "^4.45.1",
"@refinedev/react-router-v6": "^4.5.4",
"@refinedev/simple-rest": "^4.5.4",
"@refinedev/react-table": "^5.6.4",
"@tanstack/react-table": "^8.2.6",
"@tabler/icons-react": "^3.1.0",
"@emotion/react": "^11.8.2",
"@mantine/core": "^7.5.1",
"@mantine/hooks": "^7.5.1",
"@mantine/form": "^7.5.1",
"@mantine/notifications": "^7.5.1",
"react-router": "latest",
"react-router-dom": "^6.8.1",
}}
startRoute="/products/create"
files={{
"/App.tsx": {
code: AppTsxCode,
hidden: true,
},
"/data-provider.tsx": {
code: DataProviderTsxCode,
active: true,
},
"/create.tsx": {
code: CreateTsxCode,
},
}}
/>
);
}
const DataProviderTsxCode = /* jsx */ `
import type { HttpError } from "@refinedev/core";
import baseDataProvider from "@refinedev/simple-rest";
const dataProvider = {
...baseDataProvider("https://api.fake-rest.refine.dev"),
create: async () => {
// For demo purposes, we're hardcoding the error response.
// In a real-world application, the error of the server should match the \`HttpError\` interface
// or should be transformed to match it.
return Promise.reject({
message: "This is an error from the server",
statusCode: 400,
errors: {
name: "Name should be at least 3 characters long",
material: "Material should start with a capital letter",
description: "Description should be at least 10 characters long",
},
} as HttpError);
}
};
export default dataProvider;
`.trim();
const AppTsxCode = /* jsx */ `
import { Refine, Authenticated } from "@refinedev/core";
import {
ErrorComponent,
ThemedLayoutV2,
RefineThemes,
useNotificationProvider,
AuthPage
} from "@refinedev/mantine";
import { NotificationsProvider } from "@mantine/notifications";
import { MantineProvider, Global } from "@mantine/core";
import routerProvider, {
NavigateToResource,
} from "@refinedev/react-router-v6";
import { BrowserRouter, Routes, Route, Outlet, Navigate } from "react-router-dom";
import dataProvider from "./data-provider";
import { ProductCreate } from "./create";
const App: React.FC = () => {
return (
<BrowserRouter>
<MantineProvider
theme={RefineThemes.Blue}
withNormalizeCSS
withGlobalStyles
>
<Global styles={{ body: { WebkitFontSmoothing: "auto" } }} />
<NotificationsProvider position="top-right">
<Refine
notificationProvider={useNotificationProvider}
routerProvider={routerProvider}
dataProvider={dataProvider}
resources={[
{
name: "products",
create: "/products/create",
},
]}
options={{ mutationMode: "pessimistic", syncWithLocation: true, redirect: { afterCreate: false } }}
>
<Routes>
<Route
element={
<ThemedLayoutV2>
<Outlet />
</ThemedLayoutV2>
}
>
<Route path="/products" element={<Outlet />}>
<Route path="create" element={<ProductCreate />} />
</Route>
<Route path="*" element={<ErrorComponent />} />
</Route>
</Routes>
</Refine>
</NotificationsProvider>
</MantineProvider>
</BrowserRouter>
);
};
export default App;
`.trim();
const CreateTsxCode = /* jsx */ `
import { Create, useForm } from "@refinedev/mantine";
import { TextInput, Textarea, NumberInput } from "@mantine/core";
export const ProductCreate = () => {
const {
saveButtonProps,
getInputProps,
errors,
} = useForm({
initialValues: {
name: "",
material: "",
},
});
return (
<Create saveButtonProps={saveButtonProps}>
<form>
<TextInput
mt={8}
id="name"
label="Name"
placeholder="Name"
{...getInputProps("name")}
/>
<TextInput
mt={8}
id="material"
label="Material"
placeholder="Material"
{...getInputProps("material")}
/>
<Textarea
mt={8}
id="description"
label="Description"
placeholder="Description"
{...getInputProps("description")}
/>
</form>
</Create>
);
};
`.trim();