AddCollection.jsx 4.89 KB
Newer Older
wyuer's avatar
wyuer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
import React from "react";
import {
  Modal,
  ModalContent,
  ModalHeader,
  ModalBody,
  ModalFooter,
  Button,
  useDisclosure,
  Input,
  Avatar,
  Textarea,
} from "@nextui-org/react";
import { AiFillPlusSquare } from "react-icons/ai";
import { useForm } from "react-hook-form";
import { doFetch } from "@/lib/doFetch";
import UploadImage from "./UploadImage";

export default function AddCollection({ refresh, parentId, type }) {
  const { isOpen, onOpen, onOpenChange } = useDisclosure();
  const { register, handleSubmit, control, setValue } = useForm();

  const handleButtonClick = async (close) => {
    // 手动触发表单提交
    handleSubmit(onSubmit)();
    await refresh();
    close();
  };

  const onSubmit = async (data) => {
    return await doFetch({
      url: "/api/collection",
      params: { ...data, parentId:parentId ?parseInt(parentId): null },
    });
  };

  return (
    <>
      {type === "icon" ? (
        <Button
          isIconOnly
          color="primary"
          variant="faded"
          aria-label="Take a add"
          onPress={onOpen}
        >
          <AiFillPlusSquare style={{ fontSize: "20px" }} />
        </Button>
      ) : (
        <Button
          radius="full"
          className="bg-gradient-to-tr from-green-500 to-blue-500 text-white shadow-lg mb-4"
          onPress={onOpen}
        >
          <AiFillPlusSquare />
          添加知识库
        </Button>
      )}
      <Modal
        isOpen={isOpen}
        backdrop={"blur"}
        onOpenChange={onOpenChange}
        motionProps={{
          variants: {
            enter: {
              y: 0,
              opacity: 1,
              transition: {
                duration: 0.3,
                ease: "easeOut",
              },
            },
            exit: {
              y: -20,
              opacity: 0,
              transition: {
                duration: 0.2,
                ease: "easeIn",
              },
            },
          },
        }}
      >
        <ModalContent>
          {(onClose) => (
            <>
              <ModalHeader className="flex flex-col gap-1">
                添加知识库
              </ModalHeader>
              <ModalBody>
                <form action="" className="flex flex-col gap-4">
                  <Input
                    type="text"
                    label="知识库名称"
                    name="name"
                    isRequired
                    {...register("name", { required: true, maxLength: 20 })}
                  />
                  <Input
                    type="text"
                    label="编码"
                    name="code"
                    {...register("code", { required: true, maxLength: 20 })}
                  />

                  <Input
                    type="text"
                    label="故障类型"
                    name="faultType"
                    {...register("faultType", {
                      required: true,
                      maxLength: 20,
                    })}
                  />

                  <Input
                    type="text"
                    label="故障"
                    name="fault"
                    {...register("fault", { required: true, maxLength: 20 })}
                  />
                  <Textarea
                    type="text"
                    label="判断条件"
                    name="faultJudge"
                    {...register("faultJudge", {
                      required: true,
                      maxLength: 20,
                    })}
                  />
                  <Textarea
                    type="text"
                    label="故障原因"
                    name="faultReason"
                    {...register("faultReason", {
                      required: true,
                      maxLength: 20,
                    })}
                  />

                  <Textarea
                    type="text"
                    label="解决方案"
                    name="faultFn"
                    {...register("faultFn", { required: true, maxLength: 20 })}
                  />
                  <Textarea
                    type="text"
                    label="故障描述"
                    name="faultMessage"
                    {...register("faultMessage", {
                      required: true,
                      maxLength: 20,
                    })}
                  />
                </form>
              </ModalBody>
              <ModalFooter>
                <Button color="danger" variant="light" onPress={onClose}>
                  取消
                </Button>
                <Button
                  color="primary"
                  onPress={() => {
                    handleButtonClick(onClose);
                  }}
                >
                  提交
                </Button>
              </ModalFooter>
            </>
          )}
        </ModalContent>
      </Modal>
    </>
  );
}