Skip to content

Commit e65b3c1

Browse files
committed
feat(form): refactor error message handling in form components and update HelmTemplate schema
1 parent 7c01953 commit e65b3c1

4 files changed

Lines changed: 60 additions & 33 deletions

File tree

web/src/components/form/form-input.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ export const FormInput = ({ name, label, error, ...props }: FormFieldProps) => {
1717
{label && (
1818
<div className="mb-2 flex items-baseline justify-between">
1919
<Form.Label className="form-label">{label} :</Form.Label>
20-
{errorMessage && (
21-
<Form.Message className="form-message text-sm text-red-500">
22-
{errorMessage}
23-
</Form.Message>
24-
)}
2520
</div>
2621
)}
2722
<Form.Control asChild>
@@ -31,6 +26,11 @@ export const FormInput = ({ name, label, error, ...props }: FormFieldProps) => {
3126
{...props}
3227
/>
3328
</Form.Control>
29+
{errorMessage && (
30+
<Form.Message className="form-message ml-auto text-sm text-red-500">
31+
{errorMessage}
32+
</Form.Message>
33+
)}
3434
</Form.Field>
3535
);
3636
};

web/src/components/form/form-select.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import * as Form from '@radix-ui/react-form';
33
import { Controller, useFormContext } from 'react-hook-form';
44
import { FormFieldProps } from '../../types/form.types';
55
import Select from 'react-select';
6+
import { getNestedValue } from '@/lib/helper';
7+
import { selectStyle } from '@/pages/helm-template/styles/helm-template.style';
8+
import { useStyle } from '@/hooks';
69

710
interface OptionType {
811
value: string;
@@ -29,19 +32,16 @@ export const FormSelect = ({
2932
formState: { errors },
3033
} = useFormContext();
3134

32-
const fieldError = errors[name];
35+
const { darkMode } = useStyle();
36+
37+
const fieldError = getNestedValue(errors, name);
3338
const errorMessage = fieldError?.message as string;
3439

3540
return (
3641
<Form.Field className="form-field" name={name}>
3742
{label && (
3843
<div className="mb-2 flex items-baseline justify-between">
3944
<Form.Label className="form-label">{label} :</Form.Label>
40-
{errorMessage && (
41-
<Form.Message className="form-message text-red-500">
42-
{errorMessage}
43-
</Form.Message>
44-
)}
4545
</div>
4646
)}
4747
<Form.Control asChild>
@@ -55,10 +55,16 @@ export const FormSelect = ({
5555
placeholder={placeholder}
5656
className="w-full"
5757
{...props}
58+
styles={selectStyle(darkMode)}
5859
/>
5960
)}
6061
/>
6162
</Form.Control>
63+
{errorMessage && (
64+
<Form.Message className="form-message mt-1 text-red-500">
65+
{errorMessage}
66+
</Form.Message>
67+
)}
6268
</Form.Field>
6369
);
6470
};

web/src/pages/helm-template/helm-template.tsx

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { API } from '@/enums/api.enums';
66
import {
77
HelmTemplateBody,
88
HelmTemplateResponse,
9+
HelmTemplateSchema,
910
helmTemplateSchema,
1011
helmTemplateValidationError,
1112
} from './helm-template.types';
@@ -60,7 +61,10 @@ const HelmTemplate: FC = () => {
6061
defaultValues,
6162
});
6263

63-
const { control } = methods;
64+
const { control, getFieldState, watch } = methods;
65+
66+
console.log(getFieldState('pods'));
67+
console.log(watch());
6468

6569
const {
6670
fields: pods,
@@ -81,13 +85,23 @@ const HelmTemplate: FC = () => {
8185
remove(index);
8286
};
8387

84-
const handleSubmit = async (data) => {
88+
const handleSubmit = async (data: HelmTemplateSchema) => {
8589
try {
86-
console.log(data);
90+
const body_data = data.pods.map((data) => {
91+
const { mode, accessModes, size } = data.persistance;
92+
93+
return {
94+
...data,
95+
persistance: {
96+
size: `${size}${mode.value}`,
97+
accessModes: accessModes.value,
98+
},
99+
};
100+
});
87101

88102
const body: HelmTemplateBody = {
89-
api_version: parseInt(data.apiVersion),
90-
pods: data.pods,
103+
api_version: data.api_version,
104+
pods: body_data,
91105
};
92106

93107
await helmTemplateMutate(body);
@@ -202,15 +216,15 @@ const HelmTemplate: FC = () => {
202216
<p className="mb-2 mt-6 text-base font-bold">Persistence</p>
203217
<div className="mb-2 flex flex-col">
204218
<p className="mb-1">Size</p>
205-
<div className="flex items-center gap-3 [&>div]:flex-1">
219+
<div className="flex gap-3 [&>div]:flex-1">
206220
<FormInput
207221
name={`pods.${index}.persistance.size`}
208222
label=""
209223
placeholder="Value"
210224
/>
211225

212226
<FormSelect
213-
name={`pods.${index}.persistance.size`}
227+
name={`pods.${index}.persistance.mode`}
214228
label=""
215229
placeholder="Select..."
216230
options={sizeOptions}
@@ -252,7 +266,7 @@ const HelmTemplate: FC = () => {
252266
<div className="mb-2 mt-3 flex flex-col">
253267
<FormInput
254268
id="pods_ingress_host"
255-
name="ingress.host"
269+
name={`pods.${index}.ingress.host`}
256270
label="Host"
257271
placeholder="www.example.com"
258272
/>
@@ -308,25 +322,25 @@ export const PodEnvironmentFields: React.FC<PodEnvironmentFieldsProps> = ({
308322
</div>
309323
<div className="grid grid-cols-2 gap-4">
310324
{fields.map((field, envIdx) => (
311-
<div
312-
className="flex items-center divide-x divide-gray-200 rounded-md border border-gray-200 dark:divide-gray-500 dark:border-gray-500"
313-
key={field.id}
314-
>
325+
<div className="flex" key={field.id}>
315326
<FormInput
316327
id={`env_name_${envIdx}`}
317328
name={`pods.${podIndex}.environment.${envIdx}.name`}
318329
label=""
319330
placeholder="Env"
320-
className="h-12 rounded-s-md"
331+
className="h-12 divide-gray-200 rounded-md rounded-s-md border border-gray-200 dark:divide-gray-500 dark:border-gray-500"
321332
/>
322333
<FormInput
323334
id={`env_value_${envIdx}`}
324335
name={`pods.${podIndex}.environment.${envIdx}.value`}
325336
label=""
326337
placeholder="Hi"
327-
className={cn('h-12 w-full px-2 outline-none dark:bg-black-1', {
328-
'rounded-e-md': envIdx === 0,
329-
})}
338+
className={cn(
339+
'h-12 w-full divide-gray-200 rounded-md border border-gray-200 px-2 outline-none dark:divide-gray-500 dark:border-gray-500 dark:bg-black-1',
340+
{
341+
'rounded-e-md': envIdx === 0,
342+
},
343+
)}
330344
/>
331345
{envIdx > 0 && (
332346
<button

web/src/pages/helm-template/helm-template.types.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export interface helmTemplateValidationError {
2323
export interface Pod {
2424
name: string;
2525
image: string;
26-
target_port: number | null;
27-
replicas: number | null;
26+
target_port: string | null;
27+
replicas: string | null;
2828
persistance: {
2929
size: string;
3030
accessModes: string;
@@ -46,21 +46,27 @@ const environmentSchema = zod.object({
4646
value: zod.string().min(1, 'Value is required'),
4747
});
4848

49+
const labelValueSchema = zod.object({
50+
label: zod.string(),
51+
value: zod.string(),
52+
});
53+
4954
const persistanceSchema = zod.object({
55+
mode: labelValueSchema,
5056
size: zod.string().min(1, 'Size is required'),
51-
accessModes: zod.string().min(1, 'Access mode is required'),
57+
accessModes: labelValueSchema,
5258
});
5359

5460
const ingressSchema = zod.object({
5561
enabled: zod.boolean(),
56-
host: zod.string().url('Must be a valid URL'),
62+
host: zod.string(),
5763
});
5864

5965
const podSchema = zod.object({
6066
name: zod.string().min(1, 'Name is required'),
6167
image: zod.string().min(1, 'Image is required'),
62-
target_port: zod.number().nullable(),
63-
replicas: zod.number().nullable(),
68+
target_port: zod.string().nullable(),
69+
replicas: zod.string().nullable(),
6470
persistance: persistanceSchema,
6571
environment: zod
6672
.array(environmentSchema)
@@ -73,3 +79,4 @@ export const helmTemplateSchema = zod.object({
7379
api_version: zod.number().min(1, 'API version is required'),
7480
pods: zod.array(podSchema).min(1, 'At least one pod is required'),
7581
});
82+
export type HelmTemplateSchema = zod.infer<typeof helmTemplateSchema>;

0 commit comments

Comments
 (0)