|
|
@@ -6,11 +6,25 @@ import React, {
|
|
|
useState,
|
|
|
} from "react";
|
|
|
import styles from "./index.module.scss";
|
|
|
-import { Button, Popconfirm, Table } from "antd";
|
|
|
-import { useSelector } from "react-redux";
|
|
|
-import { RootState } from "@/store";
|
|
|
-import { getTopicByZhuNongKeTangAPI } from "@/store/action/A1Rule";
|
|
|
+import {
|
|
|
+ Button,
|
|
|
+ Form,
|
|
|
+ FormInstance,
|
|
|
+ Input,
|
|
|
+ Modal,
|
|
|
+ Popconfirm,
|
|
|
+ Select,
|
|
|
+ Table,
|
|
|
+} from "antd";
|
|
|
+import {
|
|
|
+ deleteQuestionAPI,
|
|
|
+ getTopicByZhuNongKeTangAPI,
|
|
|
+ saveQuestionAPI,
|
|
|
+} from "@/store/action/A1Rule";
|
|
|
import { QuestionTableType } from "@/types";
|
|
|
+import { MessageFu } from "@/utils/message";
|
|
|
+import encodeStr from "@/utils/pass";
|
|
|
+import { Base64 } from "js-base64";
|
|
|
type Props = {
|
|
|
closeFu: () => void;
|
|
|
};
|
|
|
@@ -33,30 +47,128 @@ function TopicSetting({ closeFu }: Props) {
|
|
|
setTableSelect({ ...tableSelect, pageNum, pageSize });
|
|
|
};
|
|
|
|
|
|
- const results = useSelector((state: RootState) => state.A7Log.tableInfo);
|
|
|
-
|
|
|
// 编辑前数据保存
|
|
|
const [editBeforeData, setEditBeforeData] = useState<any>(
|
|
|
[] as QuestionTableType[]
|
|
|
);
|
|
|
|
|
|
+ const [editShow, setEditShow] = useState(false);
|
|
|
+
|
|
|
// 用于编辑/展示/回显的新数据
|
|
|
const [editShowData, setEditShowData] = useState<any>(
|
|
|
[] as QuestionTableType[]
|
|
|
);
|
|
|
|
|
|
+ const [form] = Form.useForm();
|
|
|
+ const [curEdit, setCurEdit] = useState<any>();
|
|
|
+ const onFinish = useCallback(
|
|
|
+ async (values: any) => {
|
|
|
+ let newList = editShowData;
|
|
|
+ console.log(curEdit, newList[curEdit.index], values.numberVal);
|
|
|
+ if (curEdit) {
|
|
|
+ switch (curEdit.project) {
|
|
|
+ case "题目描述":
|
|
|
+ newList[curEdit.index].question = values.numberVal;
|
|
|
+ break;
|
|
|
+ case "选项1":
|
|
|
+ newList[curEdit.index].answer.answer[0]
|
|
|
+ ? (newList[curEdit.index].answer.answer[0].name =
|
|
|
+ values.numberVal)
|
|
|
+ : newList[curEdit.index].answer.answer.push({
|
|
|
+ name: values.numberVal,
|
|
|
+ val: encodeStr(Base64.encode("A")),
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case "选项2":
|
|
|
+ newList[curEdit.index].answer.answer[1]
|
|
|
+ ? (newList[curEdit.index].answer.answer[1].name =
|
|
|
+ values.numberVal)
|
|
|
+ : newList[curEdit.index].answer.answer.push({
|
|
|
+ name: values.numberVal,
|
|
|
+ val: encodeStr(Base64.encode("B")),
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case "选项3":
|
|
|
+ newList[curEdit.index].answer.answer[2]
|
|
|
+ ? (newList[curEdit.index].answer.answer[2].name =
|
|
|
+ values.numberVal)
|
|
|
+ : newList[curEdit.index].answer.answer.push({
|
|
|
+ name: values.numberVal,
|
|
|
+ val: encodeStr(Base64.encode("C")),
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case "选项4":
|
|
|
+ newList[curEdit.index].answer.answer[3]
|
|
|
+ ? (newList[curEdit.index].answer.answer[3].name =
|
|
|
+ values.numberVal)
|
|
|
+ : newList[curEdit.index].answer.answer.push({
|
|
|
+ name: values.numberVal,
|
|
|
+ val: encodeStr(Base64.encode("D")),
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case "答案解析":
|
|
|
+ newList[curEdit.index].description = values.numberVal;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ setEditShowData([...newList]);
|
|
|
+ setEditShow(false);
|
|
|
+ console.log(curEdit, editShowData[curEdit.index], values.numberVal);
|
|
|
+ },
|
|
|
+ [curEdit, editShowData]
|
|
|
+ );
|
|
|
+
|
|
|
+ const disposeEditShowData = useCallback((data: QuestionTableType[]) => {
|
|
|
+ let newList = [] as QuestionTableType[];
|
|
|
+ data.forEach((element: QuestionTableType) => {
|
|
|
+ newList.push({
|
|
|
+ ...element,
|
|
|
+ answer: element.answer !== "" ? JSON.parse(element.answer) : "",
|
|
|
+ });
|
|
|
+ });
|
|
|
+ setEditShowData(newList);
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ const handleCorrectChange = useCallback(
|
|
|
+ (value: string, index: number) => {
|
|
|
+ let newList = editShowData;
|
|
|
+ newList[index].answer.correct = value;
|
|
|
+ setEditShowData([...newList]);
|
|
|
+ console.log(editShowData);
|
|
|
+ },
|
|
|
+ [editShowData]
|
|
|
+ );
|
|
|
+
|
|
|
const getList = useCallback(async () => {
|
|
|
const res = await getTopicByZhuNongKeTangAPI();
|
|
|
if (res.code === 0) {
|
|
|
+ // 保存编辑前的数据
|
|
|
setEditBeforeData(res.data);
|
|
|
- setEditShowData(res.data);
|
|
|
- res.data.forEach((item:any) => {
|
|
|
- console.log('题目选项解析',JSON.parse(item.answer))
|
|
|
- })
|
|
|
+ // 保存编辑后的数据
|
|
|
+ disposeEditShowData(res.data);
|
|
|
}
|
|
|
- }, []);
|
|
|
+ }, [disposeEditShowData]);
|
|
|
+
|
|
|
+ // 删除列表
|
|
|
+ const [delTopicIdList, setDelTopicIdList] = useState([] as number[]);
|
|
|
+
|
|
|
+ const deleteQuestionFu = useCallback(
|
|
|
+ (id: number, index: any) => {
|
|
|
+ if (id == null) {
|
|
|
+ let newList = editShowData;
|
|
|
+ newList.splice(index, 1);
|
|
|
+ setEditShowData([...newList]);
|
|
|
+ } else {
|
|
|
+ setDelTopicIdList([...delTopicIdList, id]);
|
|
|
+ let newList = editShowData;
|
|
|
+ newList.splice(index, 1);
|
|
|
+ setEditShowData([...newList]);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ [delTopicIdList, editShowData]
|
|
|
+ );
|
|
|
|
|
|
- const deleteQuestionFu = useCallback((id: number) => {}, []);
|
|
|
+ const FormBoxRef = useRef<FormInstance>(null);
|
|
|
|
|
|
const columns = useMemo(() => {
|
|
|
return [
|
|
|
@@ -68,50 +180,279 @@ function TopicSetting({ closeFu }: Props) {
|
|
|
},
|
|
|
{
|
|
|
title: "题目描述",
|
|
|
- dataIndex: "question",
|
|
|
+ render: (item: QuestionTableType, record: any, index: any) => {
|
|
|
+ return (
|
|
|
+ // eslint-disable-next-line jsx-a11y/anchor-is-valid
|
|
|
+ <a
|
|
|
+ onClick={() => {
|
|
|
+ setEditShow(true);
|
|
|
+ setCurEdit({
|
|
|
+ index: index,
|
|
|
+ project: "题目描述",
|
|
|
+ value: item.question,
|
|
|
+ });
|
|
|
+ form.setFieldsValue({
|
|
|
+ numberVal: item.question,
|
|
|
+ });
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ {item.question ? item.question : "请填入"}
|
|
|
+ </a>
|
|
|
+ );
|
|
|
+ },
|
|
|
},
|
|
|
{
|
|
|
title: "选项1",
|
|
|
- dataIndex: "type",
|
|
|
+ render: (item: QuestionTableType, record: any, index: any) => {
|
|
|
+ return (
|
|
|
+ // eslint-disable-next-line jsx-a11y/anchor-is-valid
|
|
|
+ <a
|
|
|
+ onClick={() => {
|
|
|
+ setEditShow(true);
|
|
|
+ setCurEdit({
|
|
|
+ index: index,
|
|
|
+ project: "选项1",
|
|
|
+ value: item.answer.answer[0]
|
|
|
+ ? item.answer.answer[0].name
|
|
|
+ : "",
|
|
|
+ });
|
|
|
+ form.setFieldsValue({
|
|
|
+ numberVal: item.answer.answer[0]
|
|
|
+ ? item.answer.answer[0].name
|
|
|
+ : "",
|
|
|
+ });
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ {item.answer.answer[0] ? item.answer.answer[0].name : "请填入"}
|
|
|
+ </a>
|
|
|
+ );
|
|
|
+ },
|
|
|
},
|
|
|
{
|
|
|
title: "选项2",
|
|
|
- dataIndex: "type",
|
|
|
+ render: (item: QuestionTableType, record: any, index: any) => {
|
|
|
+ return (
|
|
|
+ // eslint-disable-next-line jsx-a11y/anchor-is-valid
|
|
|
+ <a
|
|
|
+ onClick={() => {
|
|
|
+ setEditShow(true);
|
|
|
+ setCurEdit({
|
|
|
+ index: index,
|
|
|
+ project: "选项2",
|
|
|
+ value: item.answer.answer[1]
|
|
|
+ ? item.answer.answer[1].name
|
|
|
+ : "",
|
|
|
+ });
|
|
|
+ form.setFieldsValue({
|
|
|
+ numberVal: item.answer.answer[1]
|
|
|
+ ? item.answer.answer[1].name
|
|
|
+ : "",
|
|
|
+ });
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ {item.answer.answer[1] ? item.answer.answer[1].name : "请填入"}
|
|
|
+ </a>
|
|
|
+ );
|
|
|
+ },
|
|
|
},
|
|
|
{
|
|
|
title: "选项3",
|
|
|
- dataIndex: "description",
|
|
|
+ render: (item: QuestionTableType, record: any, index: any) => {
|
|
|
+ return (
|
|
|
+ // eslint-disable-next-line jsx-a11y/anchor-is-valid
|
|
|
+ <a
|
|
|
+ onClick={() => {
|
|
|
+ setEditShow(true);
|
|
|
+ setCurEdit({
|
|
|
+ index: index,
|
|
|
+ project: "选项3",
|
|
|
+ value: item.answer.answer[2]
|
|
|
+ ? item.answer.answer[2].name
|
|
|
+ : "",
|
|
|
+ });
|
|
|
+ form.setFieldsValue({
|
|
|
+ numberVal: item.answer.answer[2]
|
|
|
+ ? item.answer.answer[2].name
|
|
|
+ : "",
|
|
|
+ });
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ {item.answer.answer[2] ? item.answer.answer[2].name : "请填入"}
|
|
|
+ </a>
|
|
|
+ );
|
|
|
+ },
|
|
|
},
|
|
|
{
|
|
|
title: "选项4",
|
|
|
- dataIndex: "description",
|
|
|
+ render: (item: QuestionTableType, record: any, index: any) => {
|
|
|
+ return (
|
|
|
+ // eslint-disable-next-line jsx-a11y/anchor-is-valid
|
|
|
+ <a
|
|
|
+ onClick={() => {
|
|
|
+ setEditShow(true);
|
|
|
+ setCurEdit({
|
|
|
+ index: index,
|
|
|
+ project: "选项4",
|
|
|
+ value: item.answer.answer[3]
|
|
|
+ ? item.answer.answer[3].name
|
|
|
+ : "",
|
|
|
+ });
|
|
|
+ form.setFieldsValue({
|
|
|
+ numberVal: item.answer.answer[3]
|
|
|
+ ? item.answer.answer[3].name
|
|
|
+ : "",
|
|
|
+ });
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ {item.answer.answer[3] ? item.answer.answer[3].name : "请填入"}
|
|
|
+ </a>
|
|
|
+ );
|
|
|
+ },
|
|
|
},
|
|
|
{
|
|
|
title: "正确答案",
|
|
|
- dataIndex: "description",
|
|
|
+ render: (item: QuestionTableType, record: any, index: any) => {
|
|
|
+ return (
|
|
|
+ <Select
|
|
|
+ style={{ width: 120 }}
|
|
|
+ defaultValue={item.answer.correct ? item.answer.correct : ""}
|
|
|
+ onChange={(value: any) => {
|
|
|
+ handleCorrectChange(value, index);
|
|
|
+ }}
|
|
|
+ options={item.answer.answer.map((item: any) => {
|
|
|
+ return {
|
|
|
+ label: item.name,
|
|
|
+ value: item.val,
|
|
|
+ };
|
|
|
+ })}
|
|
|
+ />
|
|
|
+ );
|
|
|
+ },
|
|
|
},
|
|
|
{
|
|
|
title: "答案解析",
|
|
|
- dataIndex: "description",
|
|
|
+ render: (item: QuestionTableType, record: any, index: any) => {
|
|
|
+ return (
|
|
|
+ // eslint-disable-next-line jsx-a11y/anchor-is-valid
|
|
|
+ <a
|
|
|
+ onClick={() => {
|
|
|
+ setEditShow(true);
|
|
|
+ setCurEdit({
|
|
|
+ index: index,
|
|
|
+ project: "答案解析",
|
|
|
+ value: item.description ? item.description : "",
|
|
|
+ });
|
|
|
+ form.setFieldsValue({
|
|
|
+ numberVal: item.description ? item.description : "",
|
|
|
+ });
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ {item.description ? item.description : "请填入"}
|
|
|
+ </a>
|
|
|
+ );
|
|
|
+ },
|
|
|
},
|
|
|
{
|
|
|
title: "操作",
|
|
|
- render: (item: QuestionTableType) => {
|
|
|
+ render: (item: QuestionTableType, record: any, index: any) => {
|
|
|
return (
|
|
|
<>
|
|
|
- <Button
|
|
|
- size="small"
|
|
|
- type="text"
|
|
|
- onClick={() => deleteQuestionFu(item.id)}
|
|
|
+ <Popconfirm
|
|
|
+ title="删除后无法恢复,是否删除?"
|
|
|
+ okText="删除"
|
|
|
+ cancelText="取消"
|
|
|
+ onConfirm={() => deleteQuestionFu(item.id!, index)}
|
|
|
+ okButtonProps={{ loading: false }}
|
|
|
>
|
|
|
- 删除
|
|
|
- </Button>
|
|
|
+ <Button size="small" type="text">
|
|
|
+ 删除
|
|
|
+ </Button>
|
|
|
+ </Popconfirm>
|
|
|
</>
|
|
|
);
|
|
|
},
|
|
|
},
|
|
|
];
|
|
|
- }, [deleteQuestionFu]);
|
|
|
+ }, [deleteQuestionFu, form, handleCorrectChange]);
|
|
|
+
|
|
|
+ // 新增 (给编辑前数据添加空)、
|
|
|
+ const addTopic = useCallback(() => {
|
|
|
+ if (editShowData.length >= 50) {
|
|
|
+ MessageFu.warning("最多可以设置50道题!");
|
|
|
+ } else {
|
|
|
+ setEditShowData([
|
|
|
+ ...editShowData,
|
|
|
+ {
|
|
|
+ id: null,
|
|
|
+ answer: {
|
|
|
+ answer: [],
|
|
|
+ },
|
|
|
+ createTime: "",
|
|
|
+ creatorId: null,
|
|
|
+ creatorName: "",
|
|
|
+ description: "",
|
|
|
+ question: "",
|
|
|
+ updateTime: "",
|
|
|
+ },
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }, [editShowData]);
|
|
|
+
|
|
|
+ // 提交所有编辑
|
|
|
+
|
|
|
+ const submitFinish = useCallback(async () => {
|
|
|
+ // 先执行删除
|
|
|
+ if (delTopicIdList.length > 0) {
|
|
|
+ delTopicIdList.forEach(async (item: any) => {
|
|
|
+ await deleteQuestionAPI(item);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ // 执行数据提交 新增不用传id 编辑要传id
|
|
|
+ let data = editShowData.map((item: any) => {
|
|
|
+ return item.id == null
|
|
|
+ ? {
|
|
|
+ answer: JSON.stringify(item.answer),
|
|
|
+ description: item.description,
|
|
|
+ question: item.question,
|
|
|
+ }
|
|
|
+ : {
|
|
|
+ answer: JSON.stringify(item.answer),
|
|
|
+ description: item.description,
|
|
|
+ id: item.id,
|
|
|
+ question: item.question,
|
|
|
+ };
|
|
|
+ });
|
|
|
+ const res = await saveQuestionAPI(data);
|
|
|
+ if (res.code === 0) {
|
|
|
+ MessageFu.success("保存成功");
|
|
|
+ closeFu();
|
|
|
+ }
|
|
|
+ }, [closeFu, delTopicIdList, editShowData]);
|
|
|
+ const onFinishEdit = useCallback(async () => {
|
|
|
+ // 校验
|
|
|
+ // 至少提交一题
|
|
|
+ if (editShowData.length === 0) {
|
|
|
+ MessageFu.warning("至少提交一道题目");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ editShowData.forEach((element: any) => {
|
|
|
+ // 题目描述为空
|
|
|
+ if (element.question === "" || !element.question) {
|
|
|
+ MessageFu.warning("题目描述不能为空");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (element.answer.answer.length < 2) {
|
|
|
+ MessageFu.warning("每道题目至少填入两个选项");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!element.answer.correct || element.answer.correct === "") {
|
|
|
+ MessageFu.warning("需设置正确答案且正确答案不能为空");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ submitFinish();
|
|
|
+ }, [editShowData, submitFinish]);
|
|
|
|
|
|
useEffect(() => {
|
|
|
getList();
|
|
|
@@ -124,7 +465,12 @@ function TopicSetting({ closeFu }: Props) {
|
|
|
<div className="pageTitle">
|
|
|
<div>助农课堂 题目设置</div>
|
|
|
</div>
|
|
|
- <Button className="addTopicBtn" size="middle" type="primary">
|
|
|
+ <Button
|
|
|
+ className="addTopicBtn"
|
|
|
+ size="middle"
|
|
|
+ type="primary"
|
|
|
+ onClick={addTopic}
|
|
|
+ >
|
|
|
新增
|
|
|
</Button>
|
|
|
</div>
|
|
|
@@ -148,7 +494,7 @@ function TopicSetting({ closeFu }: Props) {
|
|
|
</div>
|
|
|
{/* 确认/取消按钮 */}
|
|
|
<div style={{ marginTop: "10px" }}>
|
|
|
- <Button type="primary" htmlType="submit">
|
|
|
+ <Button type="primary" htmlType="submit" onClick={onFinishEdit}>
|
|
|
提交
|
|
|
</Button>
|
|
|
 
|
|
|
@@ -164,6 +510,56 @@ function TopicSetting({ closeFu }: Props) {
|
|
|
<Button>取消</Button>
|
|
|
</Popconfirm>
|
|
|
</div>
|
|
|
+ {/* 编辑弹框 */}
|
|
|
+ <Modal
|
|
|
+ destroyOnClose
|
|
|
+ closable={false}
|
|
|
+ maskClosable={false}
|
|
|
+ open={editShow}
|
|
|
+ title=" "
|
|
|
+ onCancel={() => setEditShow(false)}
|
|
|
+ footer={
|
|
|
+ [] // 设置footer为空,去掉 取消 确定默认按钮
|
|
|
+ }
|
|
|
+ >
|
|
|
+ <Form
|
|
|
+ form={form}
|
|
|
+ ref={FormBoxRef}
|
|
|
+ name="basic"
|
|
|
+ labelCol={{ span: 8 }}
|
|
|
+ onFinish={onFinish}
|
|
|
+ autoComplete="off"
|
|
|
+ >
|
|
|
+ <Form.Item
|
|
|
+ label=""
|
|
|
+ name="numberVal"
|
|
|
+ initialValue={curEdit ? curEdit.value : ""}
|
|
|
+ style={{ marginTop: "40px" }}
|
|
|
+ hide-required-asterisk={true}
|
|
|
+ >
|
|
|
+ <Input placeholder="请输入" />
|
|
|
+ </Form.Item>
|
|
|
+ {/* 确定和取消按钮 */}
|
|
|
+ <br />
|
|
|
+ <Form.Item wrapperCol={{ offset: 9, span: 16 }}>
|
|
|
+ <Button type="primary" htmlType="submit">
|
|
|
+ 提交
|
|
|
+ </Button>
|
|
|
+  
|
|
|
+ <Popconfirm
|
|
|
+ title="放弃编辑后,信息将不会保存!"
|
|
|
+ okText="放弃"
|
|
|
+ cancelText="取消"
|
|
|
+ onConfirm={() => {
|
|
|
+ setEditShow(false);
|
|
|
+ }}
|
|
|
+ okButtonProps={{ loading: false }}
|
|
|
+ >
|
|
|
+ <Button>取消</Button>
|
|
|
+ </Popconfirm>
|
|
|
+ </Form.Item>
|
|
|
+ </Form>
|
|
|
+ </Modal>
|
|
|
</div>
|
|
|
|
|
|
{/* <Button className="scoreLimitBtn" size="middle" type="primary">
|