|
1 | 1 | import { FormContext } from "../hooks/form-context"; |
2 | 2 | import type { FormFieldContext } from "../hooks/form-field-context"; |
3 | 3 | import { |
4 | | - ConfigurableProp, ConfigurableProps, |
| 4 | + ConfigurableProp, ConfigurablePropAlert, ConfigurableProps, |
5 | 5 | } from "@pipedream/sdk"; |
6 | | -import { useCustomize } from "../hooks/customization-context"; |
7 | | -import type { CSSProperties } from "react"; |
| 6 | +import { Alert } from "./Alert"; |
8 | 7 |
|
9 | 8 | export type ErrorsProps<T extends ConfigurableProps, U extends ConfigurableProp> = { |
10 | | - errors: string[]; |
11 | 9 | field: FormFieldContext<U>; |
12 | 10 | form: FormContext<T>; |
13 | 11 | }; |
14 | 12 |
|
15 | 13 | export function Errors<T extends ConfigurableProps, U extends ConfigurableProp>(props: ErrorsProps<T, U>) { |
16 | | - const { errors } = props; |
17 | | - |
| 14 | + const { field } = props; |
18 | 15 | const { |
19 | | - getProps, theme, |
20 | | - } = useCustomize(); |
21 | | - |
22 | | - const baseStyles: CSSProperties = { |
23 | | - color: theme.colors.danger, |
24 | | - gridArea: "errors", |
25 | | - }; |
| 16 | + errors, prop, |
| 17 | + } = field |
26 | 18 |
|
27 | | - if (!errors.length) { |
| 19 | + if (!Object.keys(errors || {}).length) { |
28 | 20 | return null; |
29 | 21 | } |
30 | 22 |
|
| 23 | + if (!errors[prop.name]) { |
| 24 | + return null |
| 25 | + } |
| 26 | + |
31 | 27 | // TODO depending on type does different shit... we might need async loader around the label, etc.? |
32 | 28 | // maybe that should just be handled by FormFieldContext instead of container? |
| 29 | + |
| 30 | + const formattedErrors: ConfigurablePropAlert[] = [] |
| 31 | + // for (const key in fieldErrors) { |
| 32 | + const key = "xxx" |
| 33 | + if (key === "sdkErrors") { |
| 34 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 35 | + // @ts-expect-error |
| 36 | + formattedErrors.push(...errors[key].map((e) => { |
| 37 | + const o = JSON.parse(e) |
| 38 | + return { |
| 39 | + type: "alert", |
| 40 | + alertType: "error", |
| 41 | + content: `#${o.name}\n${o.message}`, |
| 42 | + } |
| 43 | + })) |
| 44 | + } else { |
| 45 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 46 | + // @ts-expect-error |
| 47 | + formattedErrors.push(...errors[prop.name].map((e) => { |
| 48 | + return { |
| 49 | + type: "alert", |
| 50 | + alertType: "error", |
| 51 | + content: e, |
| 52 | + } |
| 53 | + })) |
| 54 | + } |
| 55 | + // } |
| 56 | + |
33 | 57 | return ( |
34 | | - <ul {...getProps("errors", baseStyles, props)}> |
35 | | - {errors.map((msg) => <li key={msg} {...getProps("error", baseStyles, props)}>{msg}</li>)} |
36 | | - </ul> |
| 58 | + // <ul {...getProps("errors", baseStyles, props)}> |
| 59 | + // {errors.map((msg) => <li key={msg} {...getProps("error", baseStyles, props)}>{msg}</li>)} |
| 60 | + // </ul> |
| 61 | + <>{formattedErrors.map((fe, idx: number) => <Alert prop={fe} key={idx}/>)}</> |
37 | 62 | ); |
38 | 63 | } |
0 commit comments