Files
bishengWeb/src/pages/SkillPage/components/CreateAssistant.tsx

131 lines
5.0 KiB
TypeScript
Raw Normal View History

2024-06-05 14:27:06 +08:00
import { useState } from "react";
import { useNavigate } from "react-router-dom";
import { LoadIcon } from "../../../components/bs-icons/loading";
import { Button } from "../../../components/bs-ui/button";
import { DialogClose, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "../../../components/bs-ui/dialog";
import { Input, Textarea } from "../../../components/bs-ui/input";
import { createAssistantsApi } from "../../../controllers/API/assistant";
import { captureAndAlertRequestErrorHoc } from "../../../controllers/request";
import { useTranslation } from "react-i18next";
export default function CreateAssistant() {
const { t } = useTranslation()
// State for form fields
const [formData, setFormData] = useState({
name: '',
2024-06-13 15:11:41 +08:00
roleAndTasks: `示例:
2024-06-05 14:27:06 +08:00
1. XX
2. XX
3. `
});
const [loading, setLoading] = useState(false);
// State for errors
const [errors, setErrors] = useState<any>({});
// Validate form fields
const validateField = (name, value) => {
switch (name) {
case 'name':
if (!value) return t('build.nameRequired');
if (value.length > 50) return t('build.nameMaxLength');
return '';
case 'roleAndTasks':
if (value.length < 20) return t('build.forBetter');
return '';
default:
return '';
}
};
// Handle field change
const handleChange = (e) => {
const { name, value } = e.target;
const error = validateField(name, value);
setFormData(prev => ({ ...prev, [name]: value }));
setErrors(prev => ({ ...prev, [name]: error }));
};
// Validate entire form
const validateForm = () => {
const formErrors = {};
let isValid = true;
Object.keys(formData).forEach(key => {
const error = validateField(key, formData[key]);
if (error) {
formErrors[key] = error;
isValid = false;
}
});
setErrors(formErrors);
return isValid;
};
// Handle form submission
const navigate = useNavigate()
const handleSubmit = async (e) => {
e.preventDefault();
const isValid = validateForm();
if (isValid) {
console.log('Form data:', formData);
setLoading(true)
const res = await captureAndAlertRequestErrorHoc(createAssistantsApi(formData.name, formData.roleAndTasks))
if (res) {
window.assistantCreate = true // 标记新建助手
navigate('/assistant/' + res.id)
}
setLoading(false)
}
};
2024-06-13 15:11:41 +08:00
return <DialogContent className="sm:max-w-[625px] bg-[#000000]">
2024-06-05 14:27:06 +08:00
<DialogHeader>
2024-06-13 15:11:41 +08:00
<DialogTitle className="text-[#fff]">NPC</DialogTitle>
2024-06-05 14:27:06 +08:00
</DialogHeader>
<div className="flex flex-col gap-8 py-6">
<div className="">
2024-06-13 15:11:41 +08:00
<label htmlFor="name" className="bisheng-label text-[#999999]"><span className="bisheng-tip text-[#FF6060]">* </span>NPC名称</label>
<Input id="name" name="name" placeholder="给NPC取一个名字" className="mt-2 npcInput" value={formData.name} onChange={handleChange} />
{errors.name && <p className="bisheng-tip mt-1 text-[#999999]"></p>}
2024-06-05 14:27:06 +08:00
</div>
<div className="">
2024-06-13 15:11:41 +08:00
<label htmlFor="roleAndTasks" className="bisheng-label text-[#999999]">NPC的角色是什么</label>
2024-06-05 14:27:06 +08:00
<Textarea
id="roleAndTasks"
name="roleAndTasks"
2024-06-13 15:11:41 +08:00
placeholder=""
2024-06-05 14:27:06 +08:00
maxLength={1000}
2024-06-13 15:11:41 +08:00
className="mt-2 min-h-32 npcInput overflow-auto scrollbar-hide"
2024-06-05 14:27:06 +08:00
value={formData.roleAndTasks}
onChange={handleChange}
/>
{errors.roleAndTasks && <p className="bisheng-tip mt-1">{errors.roleAndTasks}</p>}
</div>
</div>
2024-06-13 15:11:41 +08:00
{/* <DialogFooter>
2024-06-05 14:27:06 +08:00
<DialogClose>
2024-06-13 15:11:41 +08:00
<Button variant="outline" className="px-11 baogao-btn baogao-btn2" type="button" onClick={() => setFormData({ name: '', roleAndTasks: '' })}> </Button>
2024-06-05 14:27:06 +08:00
</DialogClose>
2024-06-13 15:11:41 +08:00
<Button disabled={loading} type="submit" className="px-11 baogao-btn baogao-btn2" onClick={handleSubmit}>
2024-06-05 14:27:06 +08:00
{loading && <LoadIcon className="mr-2" />}
2024-06-13 15:11:41 +08:00
</Button>
</DialogFooter> */}
<div className="flex justify-center ">
<DialogClose>
<Button variant="outline" className="px-11 baogao-btn baogao-btn2" type="button" onClick={() => setFormData({ name: '', roleAndTasks: '' })}> </Button>
</DialogClose>
<Button disabled={loading} type="submit" className="px-11 baogao-btn baogao-btn2" onClick={handleSubmit}>
{loading && <LoadIcon className="mr-2" />}
</Button>
</div>
2024-06-05 14:27:06 +08:00
</DialogContent>
};