remix-validated-form 1.1.1-beta.0 → 2.0.1-beta.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.
Files changed (97) hide show
  1. package/.turbo/turbo-build.log +9 -0
  2. package/.turbo/turbo-dev.log +0 -0
  3. package/browser/ValidatedForm.d.ts +22 -0
  4. package/browser/ValidatedForm.js +4 -1
  5. package/browser/hooks.d.ts +28 -3
  6. package/browser/hooks.js +12 -1
  7. package/browser/index.d.ts +2 -0
  8. package/browser/index.js +1 -0
  9. package/browser/{flatten.d.ts → internal/flatten.d.ts} +2 -3
  10. package/browser/{flatten.js → internal/flatten.js} +2 -8
  11. package/browser/internal/formContext.d.ts +18 -0
  12. package/browser/internal/formContext.js +0 -0
  13. package/browser/internal/util.d.ts +0 -0
  14. package/browser/internal/util.js +0 -0
  15. package/browser/server.d.ts +5 -0
  16. package/browser/server.js +5 -0
  17. package/browser/test-data/testFormData.d.ts +15 -0
  18. package/browser/test-data/testFormData.js +46 -0
  19. package/browser/validation/createValidator.d.ts +5 -1
  20. package/browser/validation/createValidator.js +15 -4
  21. package/browser/validation/types.d.ts +9 -0
  22. package/browser/validation/types.js +0 -0
  23. package/browser/validation/validation.test.d.ts +0 -0
  24. package/browser/validation/validation.test.js +65 -0
  25. package/browser/validation/withYup.d.ts +3 -0
  26. package/browser/validation/withYup.js +3 -0
  27. package/browser/validation/withZod.d.ts +3 -0
  28. package/browser/validation/withZod.js +3 -0
  29. package/build/ValidatedForm.d.ts +22 -0
  30. package/build/ValidatedForm.js +4 -1
  31. package/build/hooks.d.ts +28 -3
  32. package/build/hooks.js +12 -1
  33. package/build/index.d.ts +2 -0
  34. package/build/index.js +1 -0
  35. package/build/{flatten.d.ts → internal/flatten.d.ts} +2 -3
  36. package/build/{flatten.js → internal/flatten.js} +4 -10
  37. package/build/internal/formContext.d.ts +18 -0
  38. package/build/internal/formContext.js +0 -0
  39. package/build/internal/util.d.ts +0 -0
  40. package/build/internal/util.js +0 -0
  41. package/build/server.d.ts +5 -0
  42. package/build/server.js +5 -0
  43. package/build/test-data/testFormData.d.ts +15 -0
  44. package/build/test-data/testFormData.js +50 -0
  45. package/build/validation/createValidator.d.ts +5 -1
  46. package/build/validation/createValidator.js +15 -4
  47. package/build/validation/types.d.ts +9 -0
  48. package/build/validation/types.js +0 -0
  49. package/build/validation/validation.test.d.ts +0 -0
  50. package/build/validation/validation.test.js +65 -0
  51. package/build/validation/withYup.d.ts +3 -0
  52. package/build/validation/withYup.js +3 -0
  53. package/build/validation/withZod.d.ts +3 -0
  54. package/build/validation/withZod.js +3 -0
  55. package/jest.config.js +5 -0
  56. package/package.json +7 -32
  57. package/src/ValidatedForm.tsx +151 -0
  58. package/src/hooks.ts +60 -0
  59. package/src/index.ts +8 -0
  60. package/src/internal/flatten.ts +48 -0
  61. package/src/internal/formContext.ts +36 -0
  62. package/src/internal/util.ts +23 -0
  63. package/src/server.ts +10 -0
  64. package/src/test-data/testFormData.ts +55 -0
  65. package/src/validation/createValidator.ts +24 -0
  66. package/src/validation/types.ts +26 -0
  67. package/src/validation/validation.test.ts +317 -0
  68. package/src/validation/withYup.ts +43 -0
  69. package/src/validation/withZod.ts +51 -0
  70. package/tsconfig.json +5 -0
  71. package/.eslintcache +0 -1
  72. package/.eslintignore +0 -1
  73. package/.prettierignore +0 -10
  74. package/LICENSE +0 -21
  75. package/README.md +0 -230
  76. package/sample-app/.env +0 -7
  77. package/sample-app/README.md +0 -53
  78. package/sample-app/app/components/ErrorBox.tsx +0 -34
  79. package/sample-app/app/components/FormInput.tsx +0 -40
  80. package/sample-app/app/components/FormSelect.tsx +0 -37
  81. package/sample-app/app/components/SubjectForm.tsx +0 -150
  82. package/sample-app/app/entry.client.tsx +0 -4
  83. package/sample-app/app/entry.server.tsx +0 -21
  84. package/sample-app/app/root.tsx +0 -92
  85. package/sample-app/app/routes/index.tsx +0 -5
  86. package/sample-app/app/routes/subjects/$id.edit.tsx +0 -100
  87. package/sample-app/app/routes/subjects/index.tsx +0 -112
  88. package/sample-app/app/routes/subjects/new.tsx +0 -48
  89. package/sample-app/app/services/db.server.ts +0 -23
  90. package/sample-app/app/types.ts +0 -6
  91. package/sample-app/package-lock.json +0 -4617
  92. package/sample-app/package.json +0 -36
  93. package/sample-app/prisma/dev.db +0 -0
  94. package/sample-app/prisma/schema.prisma +0 -34
  95. package/sample-app/public/favicon.ico +0 -0
  96. package/sample-app/remix.config.js +0 -10
  97. package/sample-app/remix.env.d.ts +0 -2
package/README.md DELETED
@@ -1,230 +0,0 @@
1
- # Remix Validated Form
2
-
3
- A form library built for [remix](https://remix.run) to make validation easy.
4
-
5
- - Client-side, field-by-field validation (e.g. validate on blur) and form-level validation
6
- - Set default values for the entire form in one place
7
- - Re-use validation on the server
8
- - Show validation errors from the server even without JS
9
- - Detect if the current form is submitting when there are multiple forms on the page
10
- - Supports nested objects and arrays
11
- - Validation library agnostic
12
-
13
- # Demo
14
-
15
- https://user-images.githubusercontent.com/2811287/145734901-700a5085-a10b-4d89-88e1-5de9142b1e85.mov
16
-
17
- To run `sample-app`:
18
- ```
19
- git clone https://github.com/airjp73/remix-validated-form
20
- cd remix-validated-form
21
- npm i
22
- cd sample-app
23
- npm i
24
- cd ..
25
- npm run sample-app
26
- ```
27
-
28
- # Getting started
29
-
30
- ## Install
31
-
32
- ```bash
33
- npm install remix-validated-form
34
- ```
35
-
36
- ## Create an input component
37
-
38
- In order to display field errors or do field-by-field validation,
39
- it's recommended to incorporate this library into an input component using `useField`.
40
-
41
- ```tsx
42
- import { useField } from "remix-validated-form";
43
-
44
- type MyInputProps = {
45
- name: string;
46
- label: string;
47
- };
48
-
49
- export const MyInput = ({ name, label }: InputProps) => {
50
- const { validate, clearError, defaultValue, error } = useField(name);
51
- return (
52
- <div>
53
- <label htmlFor={name}>{label}</label>
54
- <input
55
- id={name}
56
- name={name}
57
- onBlur={validate}
58
- onChange={clearError}
59
- defaultValue={defaultValue}
60
- />
61
- {error && <span className="my-error-class">{error}</span>}
62
- </div>
63
- );
64
- };
65
- ```
66
-
67
- ## Create a submit button component
68
-
69
- To best take advantage of the per-form submission detection, we can create a submit button component.
70
-
71
- ```tsx
72
- import { useIsSubmitting } from "remix-validated-form";
73
-
74
- export const MySubmitButton = () => {
75
- const isSubmitting = useIsSubmitting();
76
- return (
77
- <button type="submit" disabled={isSubmitting}>
78
- {isSubmitting ? "Submitting..." : "Submit"}
79
- </button>
80
- );
81
- };
82
- ```
83
-
84
- ## Use the form!
85
-
86
- Now that we have our components, making a form is easy!
87
-
88
- ```tsx
89
- import { ActionFunction, LoaderFunction, redirect, useLoaderData } from "remix";
90
- import * as yup from "yup";
91
- import { validationError, ValidatedForm, withYup } from "remix-validated-form";
92
- import { MyInput, MySubmitButton } from "~/components/Input";
93
-
94
- // Using yup in this example, but you can use anything
95
- const validator = withYup(
96
- yup.object({
97
- firstName: yup.string().label("First Name").required(),
98
- lastName: yup.string().label("Last Name").required(),
99
- email: yup.string().email().label("Email").required(),
100
- })
101
- );
102
-
103
- export const action: ActionFunction = async ({ request }) => {
104
- const fieldValues = validator.validate(
105
- Object.fromEntries(await request.formData())
106
- );
107
- if (fieldValues.error) return validationError(fieldValues.error);
108
- const { firstName, lastName, email } = fieldValues.data;
109
-
110
- // Do something with correctly typed values;
111
-
112
- return redirect("/");
113
- };
114
-
115
- export const loader: LoaderFunction = () => {
116
- return {
117
- defaultValues: {
118
- firstName: "Jane",
119
- lastName: "Doe",
120
- email: "jane.doe@example.com",
121
- },
122
- };
123
- };
124
-
125
- export default function MyForm() {
126
- const { defaultValues } = useLoaderData();
127
- return (
128
- <ValidatedForm
129
- validator={validator}
130
- method="post"
131
- defaultValues={defaultValues}
132
- >
133
- <MyInput name="firstName" label="First Name" />
134
- <MyInput name="lastName" label="Last Name" />
135
- <MyInput name="email" label="Email" />
136
- <MySubmitButton />
137
- </ValidatedForm>
138
- );
139
- }
140
- ```
141
-
142
- ## Nested objects and arrays
143
-
144
- You can use nested objects and arrays by using a period (`.`) or brackets (`[]`) for the field names.
145
-
146
- ```tsx
147
- export default function MyForm() {
148
- const { defaultValues } = useLoaderData();
149
- return (
150
- <ValidatedForm
151
- validator={validator}
152
- method="post"
153
- defaultValues={defaultValues}
154
- >
155
- <MyInput name="firstName" label="First Name" />
156
- <MyInput name="lastName" label="Last Name" />
157
- <MyInput name="address.street" label="Street" />
158
- <MyInput name="address.city" label="City" />
159
- <MyInput name="phones[0].type" label="Phone 1 Type" />
160
- <MyInput name="phones[0].number" label="Phone 1 Number" />
161
- <MyInput name="phones[1].type" label="Phone 2 Type" />
162
- <MyInput name="phones[1].number" label="Phone 2 Number" />
163
- <MySubmitButton />
164
- </ValidatedForm>
165
- );
166
- }
167
- ```
168
-
169
- # Validation Library Support
170
-
171
- This library currently includes an out-of-the-box adapter for `yup` and `zod`,
172
- but you can easily support whatever library you want by creating your own adapter.
173
-
174
- And if you create an adapter for a library, feel free to make a PR on this library to add official support 😊
175
-
176
- ## Creating an adapter
177
-
178
- Any object that conforms to the `Validator` type can be passed into the the `ValidatedForm`'s `validator` prop.
179
-
180
- ```ts
181
- type FieldErrors = Record<string, string>;
182
-
183
- type ValidationResult<DataType> =
184
- | { data: DataType; error: undefined }
185
- | { error: FieldErrors; data: undefined };
186
-
187
- type ValidateFieldResult = { error?: string };
188
-
189
- type Validator<DataType> = {
190
- validate: (unvalidatedData: unknown) => ValidationResult<DataType>;
191
- validateField: (
192
- unvalidatedData: unknown,
193
- field: string
194
- ) => ValidateFieldResult;
195
- };
196
- ```
197
-
198
- In order to make an adapter for your validation library of choice,
199
- you can create a function that accepts a schema from the validation library and turns it into a validator.
200
-
201
- Note the use of `createValidator`. It takes care of unflatten the data for nested objects and arrays since the form doesn't know anything about object and arrays and this should be handled by the adapter. For more on this you can check the implementations for `withZod` and `withYup`.
202
-
203
- The out-of-the-box support for `yup` in this library works like this:
204
-
205
- ```ts
206
- export const withYup = <Schema extends AnyObjectSchema>(
207
- validationSchema: Schema
208
- // For best result with Typescript, we should type the `Validator` we return based on the provided schema
209
- ): Validator<InferType<Schema>> => createValidator({
210
- validate: (unvalidatedData) => {
211
- // Validate with yup and return the validated & typed data or the error
212
-
213
- if (isValid) return { data: { field1: "someValue" }, error: undefined };
214
- else return { error: { field1: "Some error!" }, data: undefined };
215
- },
216
- validateField: (unvalidatedData, field) => {
217
- // Validate the specific field with yup
218
-
219
- if (isValid) return { error: undefined };
220
- else return { error: "Some error" };
221
- },
222
- });
223
- ```
224
-
225
- # Frequenty Asked Questions
226
-
227
- ## Why are my fields triggering the native HTML validations before `remix-validated-form` ones?
228
- This is happening because you or the library you are using are passing the `required` attribute to the fields. This library doesn't take care of eliminating them and it's up to the user how they want to manage the validation errors. If you wan't to disable all native HTML validations you can add `noValidate` to `<ValidatedForm>`. We recommend this approach since the validation will still work even if JS is disabled.
229
-
230
-
package/sample-app/.env DELETED
@@ -1,7 +0,0 @@
1
- # Environment variables declared in this file are automatically made available to Prisma.
2
- # See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables
3
-
4
- # Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server and MongoDB (Preview).
5
- # See the documentation for all the connection string options: https://pris.ly/d/connection-strings
6
-
7
- DATABASE_URL="file:./dev.db"
@@ -1,53 +0,0 @@
1
- # Welcome to Remix!
2
-
3
- - [Remix Docs](https://remix.run/docs)
4
-
5
- ## Development
6
-
7
- From your terminal:
8
-
9
- ```sh
10
- npm run dev
11
- ```
12
-
13
- This starts your app in development mode, rebuilding assets on file changes.
14
-
15
- ## Deployment
16
-
17
- First, build your app for production:
18
-
19
- ```sh
20
- npm run build
21
- ```
22
-
23
- Then run the app in production mode:
24
-
25
- ```sh
26
- npm start
27
- ```
28
-
29
- Now you'll need to pick a host to deploy it to.
30
-
31
- ### DIY
32
-
33
- If you're familiar with deploying node applications, the built-in Remix app server is production-ready.
34
-
35
- Make sure to deploy the output of `remix build`
36
-
37
- - `build/`
38
- - `public/build/`
39
-
40
- ### Using a Template
41
-
42
- When you ran `npx create-remix@latest` there were a few choices for hosting. You can run that again to create a new project, then copy over your `app/` folder to the new project that's pre-configured for your target server.
43
-
44
- ```sh
45
- cd ..
46
- # create a new project, and pick a pre-configured host
47
- npx create-remix@latest
48
- cd my-new-remix-app
49
- # remove the new project's app (not the old one!)
50
- rm -rf app
51
- # copy your app over
52
- cp -R ../my-old-remix-app/app app
53
- ```
@@ -1,34 +0,0 @@
1
- import {
2
- Alert,
3
- AlertDescription,
4
- AlertIcon,
5
- AlertTitle,
6
- } from "@chakra-ui/react";
7
-
8
- type ErrorProps = {
9
- title?: React.ReactNode;
10
- description?: React.ReactNode;
11
- };
12
- export function ErrorBox({ title, description }: ErrorProps) {
13
- return (
14
- <Alert
15
- status="error"
16
- variant="subtle"
17
- flexDirection="column"
18
- alignItems="center"
19
- justifyContent="center"
20
- textAlign="center"
21
- height="200px"
22
- >
23
- <AlertIcon boxSize="40px" mr={0} />
24
- {title && (
25
- <AlertTitle mt={4} mb={1} fontSize="lg">
26
- {title}
27
- </AlertTitle>
28
- )}
29
- {description && (
30
- <AlertDescription maxWidth="sm">{description}</AlertDescription>
31
- )}
32
- </Alert>
33
- );
34
- }
@@ -1,40 +0,0 @@
1
- import {
2
- FormControl,
3
- FormErrorMessage,
4
- FormLabel,
5
- Input,
6
- InputProps,
7
- } from "@chakra-ui/react";
8
- import { useField } from "../../remix-validated-form";
9
-
10
- type FormInputProps = {
11
- name: string;
12
- label: string;
13
- isRequired?: boolean;
14
- };
15
-
16
- export const FormInput = ({
17
- name,
18
- label,
19
- isRequired,
20
- ...rest
21
- }: FormInputProps & InputProps) => {
22
- const { validate, clearError, defaultValue, error } = useField(name);
23
-
24
- return (
25
- <>
26
- <FormControl isInvalid={!!error} isRequired={isRequired}>
27
- <FormLabel htmlFor={name}>{label}</FormLabel>
28
- <Input
29
- id={name}
30
- name={name}
31
- onBlur={validate}
32
- onChange={clearError}
33
- defaultValue={defaultValue}
34
- {...rest}
35
- />
36
- {error && <FormErrorMessage>{error}</FormErrorMessage>}
37
- </FormControl>
38
- </>
39
- );
40
- };
@@ -1,37 +0,0 @@
1
- import {
2
- FormControl,
3
- FormErrorMessage,
4
- FormLabel,
5
- Select,
6
- SelectProps,
7
- } from "@chakra-ui/react";
8
- import { useField } from "../../remix-validated-form";
9
-
10
- type FormSelectProps = {
11
- name: string;
12
- label: string;
13
- isRequired?: boolean;
14
- };
15
-
16
- export const FormSelect = ({
17
- name,
18
- label,
19
- isRequired,
20
- ...rest
21
- }: FormSelectProps & SelectProps) => {
22
- const { validate, clearError, defaultValue, error } = useField(name);
23
- return (
24
- <FormControl isInvalid={!!error} isRequired={isRequired}>
25
- <FormLabel htmlFor={name}>{label}</FormLabel>
26
- <Select
27
- id={name}
28
- name={name}
29
- onBlur={validate}
30
- onChange={clearError}
31
- defaultValue={defaultValue}
32
- {...rest}
33
- />
34
- {error && <FormErrorMessage>{error}</FormErrorMessage>}
35
- </FormControl>
36
- );
37
- };
@@ -1,150 +0,0 @@
1
- import {
2
- Box,
3
- Button,
4
- Stack,
5
- HStack,
6
- Container,
7
- VStack,
8
- } from "@chakra-ui/react";
9
- import { useState } from "react";
10
- import { useNavigate } from "remix";
11
- import * as z from "zod";
12
- import {
13
- ValidatedForm,
14
- withZod,
15
- useIsSubmitting,
16
- } from "../../remix-validated-form";
17
- import { FormInput } from "./FormInput";
18
- import { FormSelect } from "./FormSelect";
19
-
20
- const subjectSchema = z.object({
21
- name: z.string().nonempty("Subject Name can't be empty"),
22
- description: z.string().nonempty("Subject Description can't be empty"),
23
- teacher: z.object({
24
- name: z.string().nonempty("Teacher Name can't be empty"),
25
- email: z
26
- .string()
27
- .email("Teacher Email is invalid")
28
- .nonempty("Teacher Email can't be empty"),
29
- }),
30
- subjectDays: z
31
- .object({
32
- day: z.string().nonempty("Day can't be empty"),
33
- })
34
- .array(),
35
- });
36
-
37
- export const subjectFormValidator = withZod(subjectSchema);
38
-
39
- export function SubjectForm({
40
- defaultValues,
41
- }: {
42
- defaultValues?: Partial<z.infer<typeof subjectSchema>>;
43
- }) {
44
- let navigate = useNavigate();
45
- const isSubmitting = useIsSubmitting();
46
- const [daysKeys, setDaysKeys] = useState(
47
- defaultValues?.subjectDays && defaultValues.subjectDays.length > 0
48
- ? Array.from(Array(defaultValues.subjectDays.length).keys())
49
- : [0]
50
- );
51
-
52
- return (
53
- <Box as="main" py="8" flex="1">
54
- <Container maxW="7xl" id="xxx">
55
- <Box bg="white" p="6" rounded="lg" shadow="base">
56
- <Box px="10" maxWidth="7xl">
57
- <ValidatedForm
58
- validator={subjectFormValidator}
59
- defaultValues={defaultValues}
60
- method="post"
61
- noValidate
62
- >
63
- <Stack spacing="6" direction="column">
64
- <Stack direction="row" spacing="6" align="center" width="full">
65
- <FormInput name="name" label="Name" isRequired />
66
- <FormInput
67
- name="description"
68
- label="Description"
69
- isRequired
70
- />
71
- </Stack>
72
- <Stack direction="row" spacing="6" align="center" width="full">
73
- <FormInput
74
- name="teacher.name"
75
- label="Teacher Name"
76
- isRequired
77
- />
78
- <FormInput
79
- name="teacher.email"
80
- label="Teacher Email"
81
- isRequired
82
- />
83
- </Stack>
84
- <VStack width="full" spacing="6" alignItems="flex-start">
85
- {daysKeys.map((key, index) => (
86
- <Stack direction="row" width="full" key={key}>
87
- <FormSelect
88
- name={`subjectDays[${index}].day`}
89
- label="Subject Day"
90
- isRequired
91
- placeholder="Select Day"
92
- >
93
- <option value="Monday">Monday</option>
94
- <option value="Tuesday">Tuesday</option>
95
- <option value="Wednesday">Wednesday</option>
96
- <option value="Thursday">Thursday</option>
97
- <option value="Friday">Friday</option>
98
- <option value="Saturday">Saturday</option>
99
- <option value="Sunday">Sunday</option>
100
- </FormSelect>
101
- {daysKeys.length > 1 && (
102
- <Box pt="8">
103
- <Button
104
- colorScheme="red"
105
- onClick={() =>
106
- setDaysKeys(
107
- daysKeys.filter(
108
- (key2, index2) => index !== index2
109
- )
110
- )
111
- }
112
- >
113
- Delete
114
- </Button>
115
- </Box>
116
- )}
117
- </Stack>
118
- ))}
119
- <Button
120
- colorScheme="blue"
121
- size="xs"
122
- onClick={() =>
123
- setDaysKeys([...daysKeys, Math.max(...daysKeys) + 1])
124
- }
125
- >
126
- Add Day
127
- </Button>
128
- </VStack>
129
-
130
- <HStack width="full" justifyContent="center" mt="8">
131
- <Button
132
- type="submit"
133
- colorScheme="blue"
134
- disabled={isSubmitting}
135
- isLoading={isSubmitting}
136
- >
137
- {isSubmitting ? "Sending..." : "Send"}
138
- </Button>
139
- <Button variant="outline" onClick={() => navigate(-1)}>
140
- Cancel
141
- </Button>
142
- </HStack>
143
- </Stack>
144
- </ValidatedForm>
145
- </Box>
146
- </Box>
147
- </Container>
148
- </Box>
149
- );
150
- }
@@ -1,4 +0,0 @@
1
- import { hydrate } from "react-dom";
2
- import { RemixBrowser } from "remix";
3
-
4
- hydrate(<RemixBrowser />, document);
@@ -1,21 +0,0 @@
1
- import { renderToString } from "react-dom/server";
2
- import { RemixServer } from "remix";
3
- import type { EntryContext } from "remix";
4
-
5
- export default function handleRequest(
6
- request: Request,
7
- responseStatusCode: number,
8
- responseHeaders: Headers,
9
- remixContext: EntryContext
10
- ) {
11
- let markup = renderToString(
12
- <RemixServer context={remixContext} url={request.url} />
13
- );
14
-
15
- responseHeaders.set("Content-Type", "text/html");
16
-
17
- return new Response("<!DOCTYPE html>" + markup, {
18
- status: responseStatusCode,
19
- headers: responseHeaders,
20
- });
21
- }
@@ -1,92 +0,0 @@
1
- import { ChakraProvider } from "@chakra-ui/react";
2
- import {
3
- Links,
4
- LiveReload,
5
- Meta,
6
- Outlet,
7
- Scripts,
8
- ScrollRestoration,
9
- useCatch,
10
- } from "remix";
11
- import { ErrorBox } from "./components/ErrorBox";
12
-
13
- // https://remix.run/api/conventions#default-export
14
- // https://remix.run/api/conventions#route-filenames
15
- export default function App() {
16
- return (
17
- <Document>
18
- <Outlet />
19
- </Document>
20
- );
21
- }
22
-
23
- // https://remix.run/docs/en/v1/api/conventions#errorboundary
24
- export function ErrorBoundary({ error }: { error: Error }) {
25
- console.error(error);
26
- return (
27
- <Document title="Error!">
28
- <ErrorBox title={`There was an error`} description={error.message} />
29
- </Document>
30
- );
31
- }
32
-
33
- // https://remix.run/docs/en/v1/api/conventions#catchboundary
34
- export function CatchBoundary() {
35
- let caught = useCatch();
36
-
37
- let message;
38
- switch (caught.status) {
39
- case 401:
40
- message = (
41
- <p>
42
- Oops! Looks like you tried to visit a page that you do not have access
43
- to.
44
- </p>
45
- );
46
- break;
47
- case 404:
48
- message = (
49
- <p>Oops! Looks like you tried to visit a page that does not exist.</p>
50
- );
51
- break;
52
-
53
- default:
54
- throw new Error(caught.data || caught.statusText);
55
- }
56
-
57
- return (
58
- <Document title={`${caught.status} ${caught.statusText}`}>
59
- <ErrorBox
60
- title={`${caught.status}: ${caught.statusText}`}
61
- description={message}
62
- />
63
- </Document>
64
- );
65
- }
66
-
67
- function Document({
68
- children,
69
- title,
70
- }: {
71
- children: React.ReactNode;
72
- title?: string;
73
- }) {
74
- return (
75
- <html lang="en">
76
- <head>
77
- <meta charSet="utf-8" />
78
- <meta name="viewport" content="width=device-width,initial-scale=1" />
79
- {title ? <title>{title}</title> : null}
80
- <Meta />
81
- <Links />
82
- </head>
83
- <body>
84
- <ChakraProvider>{children}</ChakraProvider>
85
-
86
- <ScrollRestoration />
87
- <Scripts />
88
- {process.env.NODE_ENV === "development" && <LiveReload />}
89
- </body>
90
- </html>
91
- );
92
- }
@@ -1,5 +0,0 @@
1
- import { LoaderFunction, redirect } from "remix";
2
-
3
- export let loader: LoaderFunction = () => {
4
- return redirect("/subjects");
5
- };