Skip to content
This repository has been archived by the owner on Jan 5, 2025. It is now read-only.

Commit

Permalink
Refactor action form to transform parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
faltawy committed Jan 12, 2024
1 parent 420c906 commit 5dc7c09
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
45 changes: 29 additions & 16 deletions dashboard/components/domain/action-form/ActionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,35 @@ import { FieldArray } from "@/components/ui/FieldArray";
import { FormErrorMessage } from "@/components/ui/FieldError";
import { Tooltip } from "../Tooltip";
import { Plus, Wand2, X } from "lucide-react";
import { produce } from "immer";
import { JsonInput } from "../JsonInput";

type ParameterType = {
value: string | null;
name: string;
in: "query" | "path";
schema: {
type: "string";
}
}

export type ActionWithModifiedParameters = { payload: { parameters: ParameterType[], headers: ActionType['headers'] } } & Omit<ActionType, 'parameters' | 'headers'>;

function transformAction(action: ActionType): ActionWithModifiedParameters {
const parameters: ParameterType[] = [];
_.pick(action, ['parameters']).parameters?.forEach((parameter) => {
const value = parameter.is_magic ? null : parameter.value ?? null;
parameters.push({
name: parameter.key,
in: 'path',
value: value,
schema: {
type: 'string'
}
})
})
return _.merge(_.omit(action, ['parameters', 'headers']), { payload: { parameters, headers: action.headers } })
}

function isValidField<
TValues extends FieldValues = FieldValues,
TName extends FieldPath<TValues> = FieldPath<TValues>
Expand All @@ -41,7 +67,7 @@ export function ActionForm({
}: {
defaultValues?: ActionType,
// eslint-disable-next-line no-unused-vars
onSubmit?: (data: ActionType, defaultValues?: ActionType) => void
onSubmit?: (data: ActionWithModifiedParameters, defaultValues?: ActionType) => void
// eslint-disable-next-line no-unused-vars
footer?: (form: ReturnType<typeof useForm<ActionType>>) => React.ReactNode
className?: string
Expand All @@ -56,21 +82,8 @@ export function ActionForm({


function handleSubmit(_data: ActionType) {
const data = produce(_data, (draft) => {
draft.parameters?.forEach((param) => {
if (param.is_magic) {
param.value = `{{magic_field}}`
}
})
draft.headers?.forEach((header) => {
if (header.is_magic) {
header.value = `{{magic_field}}`
}
})
})
onSubmit?.(data, defaultValues);
onSubmit?.(transformAction(_data), defaultValues);
}

return (
<Form {...form}>
<form className={className} onSubmit={form.handleSubmit(handleSubmit)}>
Expand Down
4 changes: 2 additions & 2 deletions dashboard/data/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";
import { baseUrl } from "./base-url";
import { ActionType } from "@/components/domain/action-form/schema";
import { apiMethods } from "@/types/utils";
import type { ActionWithModifiedParameters } from "@/components/domain/action-form/ActionForm";

const instance = axios.create({
baseURL: baseUrl + "/backend/actions",
Expand Down Expand Up @@ -44,7 +44,7 @@ export function getActionById(action_id: string) {
}

// {{backend_base}}/actions/bot/:bot_id
export function createActionByBotId(bot_id: string, data: ActionType) {
export function createActionByBotId(bot_id: string, data: ActionWithModifiedParameters) {
return instance.post<ActionResponseType>(`/bot/${bot_id}`, data);
}

Expand Down

0 comments on commit 5dc7c09

Please sign in to comment.