Skip to content

Commit 7676974

Browse files
committed
feat(api respone types): add api response and request structure
1 parent b2c604c commit 7676974

3 files changed

Lines changed: 33 additions & 12 deletions

File tree

web/src/features/basicGen/BasicGen.tsx

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import { Button, HStack, Stack } from "@chakra-ui/react";
66
import useGptStore from "../../utils/store";
77
import ChatBox from "../../components/internal-ui/ChatBox";
88

9-
import { BasicGenFormData } from "../model";
9+
import { ApiRequestBasicGen, BasicGenFormData } from "../model";
1010
import { useEffect, useState } from "react";
1111
import { IoSendOutline } from "react-icons/io5";
12+
import { v4 as uuid } from "uuid";
1213

1314
const BasicGen = () => {
1415
const formMethods = useForm<BasicGenFormData>({
@@ -18,30 +19,29 @@ const BasicGen = () => {
1819
service: "terraform",
1920
input: undefined,
2021
},
21-
mode: "all",
22+
mode: "onSubmit",
2223
});
2324

2425
const { handleSubmit } = formMethods;
2526

2627
const messages = useGptStore((s) => s.messages);
2728
const addMessage = useGptStore((s) => s.addMessage);
28-
const [isDisabled, setIsDisabled] = useState(true);
29-
const [req, setReq] = useState<any>();
29+
const [req, setReq] = useState<ApiRequestBasicGen | null>(null);
3030

3131
const onSubmit = (data: BasicGenFormData) => {
32-
addMessage(UserType.USER, data.input);
33-
const request = {
32+
addMessage(UserType.USER, data.input, uuid());
33+
const request: ApiRequestBasicGen = {
3434
min_token: data.minToken,
3535
max_token: data.maxToken,
3636
service: data.service,
3737
input: data.input,
3838
};
39-
setReq(request);
39+
if (data.input) setReq(request);
4040
};
4141

4242
useEffect(() => {
43-
setIsDisabled(!!formMethods.getFieldState(BasicGenFields.INPUT).isDirty);
44-
}, [formMethods]);
43+
return () => setReq(null);
44+
}, []);
4545

4646
return (
4747
<div>
@@ -60,13 +60,19 @@ const BasicGen = () => {
6060
/>
6161
<HStack
6262
mt="3"
63-
alignItems="end"
64-
alignContent={"center"}
63+
alignItems="center"
64+
alignContent="center"
6565
justifyContent="center"
6666
bottom="5"
6767
>
6868
<Input placeholder="Text" fieldName={BasicGenFields.INPUT} />
69-
<Button type="submit" bg="orange.800" disabled={isDisabled}>
69+
<Button
70+
type="submit"
71+
bg="orange.800"
72+
disabled={
73+
formMethods.getFieldState(BasicGenFields.INPUT).invalid
74+
}
75+
>
7076
<IoSendOutline />
7177
</Button>
7278
</HStack>

web/src/features/model.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { UserType } from "./constants";
33
export interface Message {
44
user: UserType;
55
content: string;
6+
id: string;
67
}
78

89
export interface BasicGenFormData {
@@ -11,3 +12,10 @@ export interface BasicGenFormData {
1112
service: string;
1213
input: string;
1314
}
15+
16+
export interface ApiRequestBasicGen {
17+
min_token: number;
18+
max_token: number;
19+
service: string;
20+
input: string;
21+
}

web/src/utils/formValidator.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ export const validateForm = (fieldName: BasicGenFields) => {
2828
},
2929
};
3030
break;
31+
case BasicGenFields.INPUT:
32+
validationRules = {
33+
required: {
34+
value: true,
35+
message: "Input can not be empty",
36+
},
37+
};
3138
}
3239
return validationRules;
3340
};

0 commit comments

Comments
 (0)