diff --git a/app/main.wasp b/app/main.wasp index 0e67378..25f83b0 100644 --- a/app/main.wasp +++ b/app/main.wasp @@ -270,6 +270,11 @@ action getModels { entities: [] } +action validateForm { + fn: import { validateForm } from "@src/server/actions.js", + entities: [] +} + // 📚 Queries query getDailyStats { diff --git a/app/src/client/app/ModelsPage.tsx b/app/src/client/app/ModelsPage.tsx index 1ab7879..4315dd5 100644 --- a/app/src/client/app/ModelsPage.tsx +++ b/app/src/client/app/ModelsPage.tsx @@ -12,6 +12,7 @@ import NotificationBox from '../components/NotificationBox'; const ModelsPage = () => { const [modelsSchema, setModelsSchema] = useState(null); const [initialModelSchema, setInitialModelSchema] = useState(null); + const [selectedModel, setSelectedModel] = useState(''); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); @@ -21,6 +22,7 @@ const ModelsPage = () => { const response = await getModels(); setModelsSchema(response); setInitialModelSchema(response.schemas[0].json_schema); + setSelectedModel(response.schemas[0].name); } catch (error: any) { setError('Something went wrong. Please try again later.'); console.log(error); @@ -36,6 +38,7 @@ const ModelsPage = () => { const foundSchema = modelsSchema?.schemas.find((schema) => schema.name === newModel); if (foundSchema) { setInitialModelSchema(foundSchema.json_schema); + setSelectedModel(foundSchema.name); } }; @@ -53,7 +56,12 @@ const ModelsPage = () => { {modelsSchema && ( <> - {initialModelSchema && } + {initialModelSchema && ( + + )} )} {error && setError(null)} message={error} />} diff --git a/app/src/client/components/DynamicFormBuilder.tsx b/app/src/client/components/DynamicFormBuilder.tsx index 99e42c0..954622e 100644 --- a/app/src/client/components/DynamicFormBuilder.tsx +++ b/app/src/client/components/DynamicFormBuilder.tsx @@ -3,18 +3,21 @@ import { useForm } from '../hooks/useForm'; import { JsonSchema } from '../interfaces/models'; import { TextInput } from './form/TextInput'; import { SelectInput } from './form/SelectInput'; +import { validateForm } from '../services/commonService'; interface DynamicFormBuilderProps { jsonSchema: JsonSchema; + validationURL: string; } -const DynamicFormBuilder: React.FC = ({ jsonSchema }) => { +const DynamicFormBuilder: React.FC = ({ jsonSchema, validationURL }) => { const { formData, handleChange } = useForm(jsonSchema); - const handleSubmit = (event: React.FormEvent) => { + const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); console.log('Submitted Data:', formData); - // Add submission logic here + const response = await validateForm(formData, validationURL); + console.log('Validation Response:', response); }; return ( diff --git a/app/src/client/services/commonService.ts b/app/src/client/services/commonService.ts new file mode 100644 index 0000000..abcd124 --- /dev/null +++ b/app/src/client/services/commonService.ts @@ -0,0 +1,11 @@ +import { validateForm as apiValidateForm } from 'wasp/client/operations'; + +export const validateForm = async (data: any, validationURL: string) => { + try { + const response = await apiValidateForm({ data, validationURL }); + return response; + } catch (error) { + console.error('Failed to fetch models:', error); + throw error; + } +}; diff --git a/app/src/client/tests/DynamicFormBuilder.test.tsx b/app/src/client/tests/DynamicFormBuilder.test.tsx index 033f79c..ac03f1c 100644 --- a/app/src/client/tests/DynamicFormBuilder.test.tsx +++ b/app/src/client/tests/DynamicFormBuilder.test.tsx @@ -43,7 +43,7 @@ const jsonSchema: JsonSchema = { describe('DynamicFormBuilder', () => { test('renders form fields correctly', () => { - render(); + render(); expect(screen.getByLabelText('Model')).toBeInTheDocument(); expect(screen.getByLabelText('API Key')).toBeInTheDocument(); diff --git a/app/src/server/actions.ts b/app/src/server/actions.ts index e0147e4..86b95c4 100644 --- a/app/src/server/actions.ts +++ b/app/src/server/actions.ts @@ -5,6 +5,7 @@ import { type UpdateCurrentUser, type UpdateUserById, type GetModels, + type ValidateForm, } from 'wasp/server/operations'; import Stripe from 'stripe'; import type { StripePaymentResult } from '../shared/types'; @@ -113,3 +114,31 @@ export const getModels: GetModels = async (user, context) => { throw new HttpError(500, error.message); } }; + +export const validateForm: ValidateForm<{ data: any; validationURL: string }, any> = async ( + { data, validationURL }: { data: any; validationURL: string }, + context: any +) => { + if (!context.user) { + throw new HttpError(401); + } + + try { + const response = await fetch(`${FASTAGENCY_SERVER_URL}/${validationURL}`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(data), + }); + const json: any = (await response.json()) as { detail?: string }; // Parse JSON once + + if (!response.ok) { + const errorMsg = json.detail || `HTTP error with status code ${response.status}`; + console.error('Server Error:', errorMsg); + throw new Error(errorMsg); + } + + return json; + } catch (error: any) { + throw new HttpError(500, error.message); + } +};