-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dashboard): added react-hook-forms
* added form component * added mark as null component
- Loading branch information
1 parent
4ecb4cf
commit 7ce5bf1
Showing
19 changed files
with
1,188 additions
and
295 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
dashboard/src/app/(authenticated)/(diagram)/components/Forms/WfRunForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React, { forwardRef } from 'react' | ||
import { useForm, FormProvider } from 'react-hook-form' | ||
import { FormFields } from './components/FormFields' | ||
import { ThreadVarDef } from 'littlehorse-client/proto' | ||
import { Input } from '@/components/ui/input' | ||
import { Label } from '@/components/ui/label' | ||
|
||
type Prop = { | ||
wfSpecVariables: ThreadVarDef[] | ||
onSubmit: any | ||
} | ||
ThreadVarDef | ||
|
||
// eslint-disable-next-line react/display-name | ||
export const WfRunForm = forwardRef<HTMLDivElement, Prop>(({ wfSpecVariables, onSubmit }, ref) => { | ||
const methods = useForm() | ||
const { register, handleSubmit, formState } = methods | ||
|
||
const onSubmitForm = data => { | ||
onSubmit(data) | ||
} | ||
|
||
return ( | ||
<FormProvider {...methods}> | ||
<form onSubmit={handleSubmit(onSubmitForm)} ref={ref}> | ||
<div> | ||
<Label htmlFor={'custom-id'}>Id (Custom WfRun Id)</Label> | ||
<Input type="text" className="mb-4 mt-1" id="custom-id" {...register('custom-id-wfRun-flow')} /> | ||
</div> | ||
{!!wfSpecVariables?.length && | ||
wfSpecVariables.map((variable: ThreadVarDef, index: number) => ( | ||
<FormFields key={`form-field-${index}`} variables={variable} register={register} formState={formState} /> | ||
))} | ||
</form> | ||
</FormProvider> | ||
) | ||
}) |
16 changes: 16 additions & 0 deletions
16
dashboard/src/app/(authenticated)/(diagram)/components/Forms/components/FormFields.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React, { FC } from 'react' | ||
import { FormComponent } from './formType' | ||
import { VariableType, ThreadVarDef } from 'littlehorse-client/proto' | ||
import { FieldValues, UseFormRegister } from 'react-hook-form' | ||
|
||
type Prop = { | ||
variables: ThreadVarDef | ||
register: UseFormRegister<FieldValues> | ||
formState: any | ||
} | ||
export const FormFields: FC<Prop> = props => { | ||
const type = props.variables?.varDef?.type as VariableType | ||
if (!type) return | ||
const Component = FormComponent[type] | ||
return <Component {...props} /> | ||
} |
49 changes: 49 additions & 0 deletions
49
dashboard/src/app/(authenticated)/(diagram)/components/Forms/components/FormInput.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React, { FC, useState } from 'react' | ||
import { VariableType } from 'littlehorse-client/proto' | ||
import { FormFieldProp } from '@/types' | ||
import { Input } from '@/components/ui/input' | ||
import { Label } from '@/components/ui/label' | ||
import { MarkFieldNull } from "./MarkFieldNull" | ||
export const FormInput: FC<FormFieldProp> = props => { | ||
const [isDisabled, setIsDisabled] = useState(false) | ||
if (!props.variables?.varDef?.name) return null | ||
|
||
const { | ||
variables: { | ||
varDef: { type, name }, | ||
required, | ||
}, | ||
register, | ||
formState: { errors }, | ||
} = props | ||
|
||
const variableToType = (variable: VariableType) => { | ||
switch (variable) { | ||
case VariableType.INT: | ||
return 'number' | ||
case VariableType.STR: | ||
return 'text' | ||
default: | ||
return 'text' | ||
} | ||
} | ||
|
||
return ( | ||
<div> | ||
<div className="flex justify-between"> | ||
<Label htmlFor={name}> | ||
{name} {required && <span className="text-red-700">*</span>} | ||
</Label> | ||
{!required && <MarkFieldNull name={name} setIsDisabled={setIsDisabled} />} | ||
</div> | ||
<Input | ||
type={variableToType(type)} | ||
className="mb-4 mt-1" | ||
id={name} | ||
disabled={isDisabled} | ||
{...register(name, { required: required ? `${name} is required` : false })} | ||
/> | ||
{errors[name] && <p className="text-sm text-red-700">{errors[name]?.message}</p>} | ||
</div> | ||
) | ||
} |
34 changes: 34 additions & 0 deletions
34
dashboard/src/app/(authenticated)/(diagram)/components/Forms/components/FormSwitch.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React, { FC, useState } from 'react' | ||
import { Label } from '@/components/ui/label' | ||
import { FormFieldProp } from '@/types' | ||
import { SwitchButton } from '@/components/ui/switch' | ||
import { MarkFieldNull } from './MarkFieldNull' | ||
|
||
export const FormSwitch: FC<FormFieldProp> = props => { | ||
const [isDisabled, setIsDisabled] = useState(false) | ||
if (!props.variables?.varDef?.name) return | ||
const { | ||
variables: { | ||
varDef: { type, name }, | ||
required, | ||
}, | ||
register, | ||
formState: { errors }, | ||
} = props | ||
|
||
return ( | ||
<div> | ||
<div className="flex justify-between"> | ||
<Label htmlFor={name}> | ||
{name} {required && <span className="text-red-700">*</span>} | ||
</Label> | ||
{!required && <MarkFieldNull name={name} setIsDisabled={setIsDisabled} />} | ||
</div> | ||
|
||
<div className="mb-4 mt-1"> | ||
<SwitchButton disabled={isDisabled} /> | ||
</div> | ||
{errors[name] && <p className="text-red-700 text-sm">{errors[name]?.message}</p>} | ||
</div> | ||
) | ||
} |
35 changes: 35 additions & 0 deletions
35
dashboard/src/app/(authenticated)/(diagram)/components/Forms/components/FormTextarea.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React, { FC, useState } from 'react' | ||
import { FormFieldProp } from '@/types' | ||
import { Label } from '@/components/ui/label' | ||
import { Textarea } from '@/components/ui/textarea' | ||
import { MarkFieldNull } from './MarkFieldNull' | ||
|
||
export const FormTextarea: FC<FormFieldProp> = props => { | ||
const [isDisabled, setIsDisabled] = useState(false) | ||
if (!props.variables?.varDef?.name) return | ||
const { | ||
variables: { | ||
varDef: { name }, | ||
required, | ||
}, | ||
register, | ||
formState: { errors }, | ||
} = props | ||
return ( | ||
<div> | ||
<div className="flex justify-between"> | ||
<Label htmlFor={name}> | ||
{name} {required && <span className="text-red-700">*</span>} | ||
</Label> | ||
{!required && <MarkFieldNull name={name} setIsDisabled={setIsDisabled} />} | ||
</div> | ||
<Textarea | ||
className="mb-4 mt-1" | ||
id={name} | ||
disabled={isDisabled} | ||
{...register(name, { required: required ? `${name} is required` : false })} | ||
/> | ||
{errors[name] && <p className="text-red-700 text-sm">{errors[name]?.message}</p>} | ||
</div> | ||
) | ||
} |
23 changes: 23 additions & 0 deletions
23
dashboard/src/app/(authenticated)/(diagram)/components/Forms/components/MarkFieldNull.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React, { FC } from 'react' | ||
import { useFormContext } from 'react-hook-form' | ||
import { Label } from '@/components/ui/label' | ||
|
||
type Prop = { name: string; setIsDisabled: (checked: boolean) => void } | ||
export const MarkFieldNull: FC<Prop> = ({ name, setIsDisabled }) => { | ||
const { setValue } = useFormContext() | ||
|
||
const handleCheckboxChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const checked = e.target.checked | ||
setIsDisabled(checked) | ||
if (checked) { | ||
setValue(name, null) | ||
} | ||
} | ||
|
||
return ( | ||
<Label htmlFor="mark-null" className="flex items-center gap-2"> | ||
mark as null | ||
<input id="mark-null" type="checkbox" onChange={handleCheckboxChange} /> | ||
</Label> | ||
) | ||
} |
15 changes: 15 additions & 0 deletions
15
dashboard/src/app/(authenticated)/(diagram)/components/Forms/components/formType.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { FormInput, FormTextarea, FormSwitch } from './' | ||
import { VariableType } from 'littlehorse-client/proto' | ||
|
||
export const FormComponent = { | ||
[VariableType.INT]: FormInput, | ||
[VariableType.STR]: FormInput, | ||
[VariableType.DOUBLE]: FormInput, | ||
[VariableType.BOOL]: FormSwitch, | ||
[VariableType.JSON_OBJ]: FormTextarea, | ||
[VariableType.JSON_ARR]: FormTextarea, | ||
[VariableType.BYTES]: FormInput, | ||
[VariableType.UNRECOGNIZED]: FormInput, | ||
} as const | ||
|
||
export type FormFieldType = keyof typeof FormComponent |
3 changes: 3 additions & 0 deletions
3
dashboard/src/app/(authenticated)/(diagram)/components/Forms/components/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './FormInput' | ||
export * from './FormTextarea' | ||
export * from './FormSwitch' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 4 additions & 7 deletions
11
dashboard/src/app/(authenticated)/(diagram)/wfSpec/[...props]/actions/runWfSpec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,17 @@ | ||
'use server' | ||
import { lhClient } from '@/app/lhClient' | ||
import { WithTenant } from '@/types' | ||
import { WfRun, RunWfRequest, VariableValue } from 'littlehorse-client/proto' | ||
import { WfRun, RunWfRequest } from 'littlehorse-client/proto' | ||
|
||
type RunWf = { | ||
variables?: { [key: string]: VariableValue }; | ||
} & Omit<RunWfRequest, 'variables'> & | ||
WithTenant | ||
export const runWfSpec = async ({ | ||
wfSpecName, | ||
tenantId, | ||
majorVersion, | ||
revision, | ||
parentWfRunId, | ||
id, | ||
}: RunWf): Promise<WfRun> => { | ||
variables | ||
}: RunWfRequest & WithTenant ): Promise<WfRun> => { | ||
const client = await lhClient({ tenantId }) | ||
return client.runWf({ wfSpecName, majorVersion, revision, parentWfRunId, id }) | ||
return client.runWf({ wfSpecName, majorVersion, revision, parentWfRunId, id,variables }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.