Skip to content

Commit adae0d8

Browse files
authored
Merge pull request #70 from mehdi-parvizi/bug-fix-section
Bug fix section
2 parents 6661b29 + 56ea20e commit adae0d8

File tree

16 files changed

+279
-129
lines changed

16 files changed

+279
-129
lines changed

web/src/components/internal-ui/ChatBox.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ENDPOINTS, UserType } from "../../features/constants";
66
import useGptStore from "../../utils/store";
77
import { useEffect } from "react";
88
import { v4 as uuid } from "uuid";
9+
import { useLocation } from "react-router-dom";
910

1011
interface Props {
1112
messageData: Message[];
@@ -14,24 +15,30 @@ interface Props {
1415
id: string;
1516
}
1617

17-
interface BasicGenApiResponse {
18+
interface BasicApiResponse {
1819
output: string;
1920
}
2021

2122
const ChatBox = ({ messageData, endpoint, request, id }: Props) => {
2223
const addMessage = useGptStore((s) => s.addMessage);
24+
const resetMessages = useGptStore((s) => s.resetMessages);
2325

24-
const { data, error, isLoading } = usePost<BasicGenApiResponse>(
26+
const location = useLocation();
27+
28+
const { data, error, isLoading } = usePost<BasicApiResponse>(
2529
endpoint,
2630
request,
2731
id
2832
);
2933

3034
useEffect(() => {
3135
if (data?.data.output) addMessage(UserType.BOT, data?.data.output!, uuid());
32-
console.log(messageData);
3336
}, [request, data]);
3437

38+
useEffect(() => {
39+
return () => resetMessages();
40+
}, [location.pathname]);
41+
3542
return (
3643
<Box
3744
w="100%"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Box, Button, Center } from "@chakra-ui/react";
2+
import { MdOutlineNoEncryptionGmailerrorred } from "react-icons/md";
3+
import { Link } from "react-router-dom";
4+
5+
const ErrorPage = () => {
6+
return (
7+
<Center>
8+
<Box mt="6rem" alignItems="center" className="flex flex-col">
9+
<div className="text-red-700 mb-5 flex items-center">
10+
<MdOutlineNoEncryptionGmailerrorred className="" size="2rem" />
11+
<p className="text-3xl">Unathorized Route</p>
12+
</div>
13+
<Link to="/">
14+
<Button bg="orange.700" w="5rem">
15+
Go back
16+
</Button>
17+
</Link>
18+
</Box>
19+
</Center>
20+
);
21+
};
22+
23+
export default ErrorPage;

web/src/components/internal-ui/Input.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import { useFormContext } from "react-hook-form";
2-
import { BasicGenFields } from "../../features/constants";
2+
import { BasicGenFields, BugFixFields } from "../../features/constants";
33
import { validateForm } from "../../utils/formValidator";
44

55
interface Props {
6-
fieldName: BasicGenFields;
6+
fieldName: string;
77
label?: string;
88
placeholder?: string;
99
}
1010

11-
const Input = ({ fieldName, label, placeholder }: Props) => {
11+
const Input = ({ fieldName, label, placeholder = "Placeholder" }: Props) => {
1212
const {
1313
register,
1414
formState: { errors },
1515
} = useFormContext();
1616

17+
const showTextArea =
18+
fieldName === BasicGenFields.INPUT ||
19+
fieldName === BugFixFields.BUG_DESCRIPTION;
20+
1721
const defaultStyle =
1822
"border border-stone-600 bg-stone-900 rounded-md flex h-auto p-4";
1923

@@ -23,7 +27,7 @@ const Input = ({ fieldName, label, placeholder }: Props) => {
2327
<div className="label">
2428
<span className="label-text">{label}</span>
2529
</div>
26-
{fieldName !== BasicGenFields.INPUT ? (
30+
{!showTextArea ? (
2731
<input
2832
className={defaultStyle}
2933
{...register(fieldName, validateForm(fieldName))}

web/src/components/internal-ui/Landing.tsx

Lines changed: 0 additions & 41 deletions
This file was deleted.

web/src/features/basicGen/BasicGen.tsx

Lines changed: 45 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,64 @@
1-
import { FormProvider, useForm } from "react-hook-form";
1+
import { FormProvider } from "react-hook-form";
22
import Input from "../../components/internal-ui/Input";
3-
import { BasicGenFields, ENDPOINTS, UserType } from "../constants";
3+
import { basicGenDefaultValues, BasicGenFields, ENDPOINTS } from "../constants";
44
import { Button, HStack, Stack } from "@chakra-ui/react";
55

66
import useGptStore from "../../utils/store";
77
import ChatBox from "../../components/internal-ui/ChatBox";
88

99
import { ApiRequestBasicGen, BasicGenFormData } from "../model";
10-
import { useEffect, useState } from "react";
10+
1111
import { IoSendOutline } from "react-icons/io5";
12-
import { v4 as uuid } from "uuid";
12+
13+
import useFormHandler from "../../hooks/useFormHandler";
14+
import { basicGenMapper } from "../../utils/mapperFunctions";
1315

1416
const BasicGen = () => {
15-
const formMethods = useForm<BasicGenFormData>({
16-
defaultValues: {
17-
minToken: 100,
18-
maxToken: 500,
19-
service: "terraform",
20-
input: undefined,
21-
},
22-
mode: "onSubmit",
23-
});
17+
const { formMethods, handleSubmit, onSubmit, request } = useFormHandler<
18+
BasicGenFormData,
19+
ApiRequestBasicGen
20+
>(basicGenDefaultValues);
2421

25-
const { handleSubmit } = formMethods;
22+
const handleFormSubmit = handleSubmit((data) => {
23+
onSubmit(basicGenMapper(data), data.input);
24+
});
2625

2726
const messages = useGptStore((s) => s.messages);
28-
const addMessage = useGptStore((s) => s.addMessage);
29-
const [req, setReq] = useState<ApiRequestBasicGen | null>(null);
30-
31-
const onSubmit = (data: BasicGenFormData) => {
32-
addMessage(UserType.USER, data.input, uuid());
33-
const request: ApiRequestBasicGen = {
34-
min_token: data.minToken,
35-
max_token: data.maxToken,
36-
service: data.service,
37-
input: data.input,
38-
requestId: uuid(),
39-
};
40-
if (data.input) setReq(request);
41-
};
42-
43-
useEffect(() => {
44-
return () => setReq(null);
45-
}, []);
4627

4728
return (
48-
<div>
49-
<FormProvider {...formMethods}>
50-
<form onSubmit={(e) => void handleSubmit(onSubmit)(e)}>
51-
<Stack gap="3" justifyContent="center" alignItems="center">
52-
<div className="flex gap-2">
53-
<Input fieldName={BasicGenFields.MIN_TOKEN} label="Min token" />
54-
<Input fieldName={BasicGenFields.MAX_TOKEN} label="Max token" />
55-
<Input fieldName={BasicGenFields.SERVICE} label="Service" />
56-
</div>
57-
<ChatBox
58-
endpoint={ENDPOINTS.postBasic}
59-
request={req}
60-
messageData={messages}
61-
id={req?.requestId ?? ""}
62-
/>
63-
<HStack
64-
mt="3"
65-
alignItems="center"
66-
alignContent="center"
67-
justifyContent="center"
68-
bottom="5"
29+
<FormProvider {...formMethods}>
30+
<form onSubmit={handleFormSubmit}>
31+
<Stack gap="3" justifyContent="center" alignItems="center">
32+
<div className="flex gap-2">
33+
<Input fieldName={BasicGenFields.MIN_TOKEN} label="Min token" />
34+
<Input fieldName={BasicGenFields.MAX_TOKEN} label="Max token" />
35+
<Input fieldName={BasicGenFields.SERVICE} label="Service" />
36+
</div>
37+
<ChatBox
38+
endpoint={ENDPOINTS.postBasic}
39+
request={request}
40+
messageData={messages}
41+
id={request?.requestId ?? ""}
42+
/>
43+
<HStack
44+
mt="3"
45+
alignItems="center"
46+
alignContent="center"
47+
justifyContent="center"
48+
bottom="5"
49+
>
50+
<Input placeholder="Text" fieldName={BasicGenFields.INPUT} />
51+
<Button
52+
type="submit"
53+
bg="orange.800"
54+
disabled={formMethods.getFieldState(BasicGenFields.INPUT).invalid}
6955
>
70-
<Input placeholder="Text" fieldName={BasicGenFields.INPUT} />
71-
<Button
72-
type="submit"
73-
bg="orange.800"
74-
disabled={
75-
formMethods.getFieldState(BasicGenFields.INPUT).invalid
76-
}
77-
>
78-
<IoSendOutline />
79-
</Button>
80-
</HStack>
81-
</Stack>
82-
</form>
83-
</FormProvider>
84-
</div>
56+
<IoSendOutline />
57+
</Button>
58+
</HStack>
59+
</Stack>
60+
</form>
61+
</FormProvider>
8562
);
8663
};
8764

web/src/features/bugFix/BugFix.tsx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { Button, HStack, Stack } from "@chakra-ui/react";
2+
import { FormProvider } from "react-hook-form";
3+
import { IoSendOutline } from "react-icons/io5";
4+
import ChatBox from "../../components/internal-ui/ChatBox";
5+
import Input from "../../components/internal-ui/Input";
6+
import useFormHandler from "../../hooks/useFormHandler";
7+
import { bugFixMapper } from "../../utils/mapperFunctions";
8+
import useGptStore from "../../utils/store";
9+
import { bugFixDefaultValues, BugFixFields, ENDPOINTS } from "../constants";
10+
import { ApiRequestBugFix, BugFixFormData } from "../model";
11+
12+
const BugFix = () => {
13+
const { request, handleSubmit, onSubmit, formMethods } = useFormHandler<
14+
BugFixFormData,
15+
ApiRequestBugFix
16+
>(bugFixDefaultValues);
17+
18+
const messages = useGptStore((s) => s.messages);
19+
20+
const handleFormSubmit = handleSubmit((data) => {
21+
onSubmit(bugFixMapper(data), data.bugDescription);
22+
});
23+
24+
return (
25+
<FormProvider {...formMethods}>
26+
<form onSubmit={handleFormSubmit}>
27+
<Stack gap="3" justifyContent="center" alignItems="center">
28+
<div className="flex gap-2">
29+
<Input fieldName={BugFixFields.MIN_TOKEN} label="Min token" />
30+
<Input fieldName={BugFixFields.MAX_TOKEN} label="Max token" />
31+
<Input fieldName={BugFixFields.SERVICE} label="Service" />
32+
<Input fieldName={BugFixFields.VERSION} label="Version" />
33+
</div>
34+
<ChatBox
35+
endpoint={ENDPOINTS.postFix}
36+
request={request}
37+
messageData={messages}
38+
id={request?.requestId ?? ""}
39+
/>
40+
<HStack
41+
mt="3"
42+
alignItems="center"
43+
alignContent="center"
44+
justifyContent="center"
45+
bottom="5"
46+
>
47+
<Input
48+
placeholder="Text"
49+
fieldName={BugFixFields.BUG_DESCRIPTION}
50+
/>
51+
<Button
52+
type="submit"
53+
bg="orange.800"
54+
disabled={
55+
formMethods.getFieldState(BugFixFields.BUG_DESCRIPTION).invalid
56+
}
57+
>
58+
<IoSendOutline />
59+
</Button>
60+
</HStack>
61+
</Stack>
62+
</form>
63+
</FormProvider>
64+
);
65+
};
66+
67+
export default BugFix;

web/src/features/constants.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
export enum BasicGenFields {
22
MIN_TOKEN = "minToken",
33
MAX_TOKEN = "maxToken",
4+
SERVICE = "service",
45
INPUT = "input",
6+
}
7+
8+
export enum BugFixFields {
9+
MIN_TOKEN = "minToken",
10+
MAX_TOKEN = "maxToken",
511
SERVICE = "service",
12+
VERSION = "version",
13+
BUG_DESCRIPTION = "bugDescription",
614
}
715

816
export enum UserType {
@@ -19,3 +27,18 @@ export enum ENDPOINTS {
1927
getDonwload = "/download",
2028
getDirectory = "/list-directory",
2129
}
30+
31+
export const basicGenDefaultValues = {
32+
minToken: 100,
33+
maxToken: 500,
34+
service: "terraform",
35+
input: undefined,
36+
};
37+
38+
export const bugFixDefaultValues = {
39+
minToken: 100,
40+
maxToken: 500,
41+
service: "terraform",
42+
version: "latest",
43+
bugDescription: undefined,
44+
};

0 commit comments

Comments
 (0)