|
@@ -1,15 +1,63 @@
|
|
|
import { FormPageFooter, MemoSpinLoding } from "@/components";
|
|
|
-import { Button, DatePicker, Form, FormInstance, Input, Select } from "antd";
|
|
|
-import { useCallback, useRef, useState } from "react";
|
|
|
+import {
|
|
|
+ App,
|
|
|
+ Button,
|
|
|
+ DatePicker,
|
|
|
+ Form,
|
|
|
+ FormInstance,
|
|
|
+ Input,
|
|
|
+ Switch,
|
|
|
+ Table,
|
|
|
+} from "antd";
|
|
|
+import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
|
import { useNavigate, useParams } from "react-router-dom";
|
|
|
-import { TopicDrawer } from "../components/topicDrawer";
|
|
|
+import { QuestionType, TopicDrawer } from "../components/topicDrawer";
|
|
|
+import { dayjs, formatDate } from "@dage/utils";
|
|
|
+import { questionaireApi } from "@/api";
|
|
|
+import { DageTableActions } from "@dage/pc-components";
|
|
|
|
|
|
export default function QuestionnaireCreateOrEditPage() {
|
|
|
const formRef = useRef<FormInstance>(null);
|
|
|
const navigate = useNavigate();
|
|
|
const params = useParams();
|
|
|
+ const { message } = App.useApp();
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
+ const [optsLoading, setOptsLoading] = useState(false);
|
|
|
const [drawerVisible, setDrawerVisible] = useState(false);
|
|
|
+ const [list, setList] = useState<QuestionType[]>([]);
|
|
|
+ const [editQuestion, setEditQuestion] = useState<QuestionType | null>(null);
|
|
|
+
|
|
|
+ const getList = useCallback(async () => {
|
|
|
+ try {
|
|
|
+ setOptsLoading(true);
|
|
|
+ const data = await questionaireApi.getQuestions(params.id as string);
|
|
|
+ setList(data);
|
|
|
+ } finally {
|
|
|
+ setOptsLoading(false);
|
|
|
+ }
|
|
|
+ }, [params.id]);
|
|
|
+
|
|
|
+ const getDetail = useCallback(async () => {
|
|
|
+ setLoading(true);
|
|
|
+ try {
|
|
|
+ const { publishDate, ...rest } = await questionaireApi.getDetail(
|
|
|
+ params.id as string
|
|
|
+ );
|
|
|
+
|
|
|
+ formRef.current?.setFieldsValue({
|
|
|
+ publishDate: dayjs(publishDate),
|
|
|
+ ...rest,
|
|
|
+ });
|
|
|
+
|
|
|
+ await getList();
|
|
|
+ } finally {
|
|
|
+ setLoading(false);
|
|
|
+ }
|
|
|
+ }, [params.id, getList]);
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ !!params.id && getDetail();
|
|
|
+ }, [getDetail, params.id]);
|
|
|
|
|
|
const handleCancel = useCallback(() => {
|
|
|
navigate(-1);
|
|
@@ -18,16 +66,51 @@ export default function QuestionnaireCreateOrEditPage() {
|
|
|
const handleSubmit = useCallback(async () => {
|
|
|
if (!(await formRef.current?.validateFields())) return;
|
|
|
|
|
|
- const { ...rest } = formRef.current?.getFieldsValue();
|
|
|
+ if (!list.length) {
|
|
|
+ message.error("题目不能为空");
|
|
|
+ return Promise.reject(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ const { publishDate, display, ...rest } = formRef.current?.getFieldsValue();
|
|
|
|
|
|
if (params.id) {
|
|
|
rest.id = params.id;
|
|
|
}
|
|
|
|
|
|
+ const data = await questionaireApi.save({
|
|
|
+ ...rest,
|
|
|
+ display: display ? 1 : 0,
|
|
|
+ publishDate: formatDate(publishDate),
|
|
|
+ });
|
|
|
+
|
|
|
+ const newList = list
|
|
|
+ .filter((i) => i._isNew)
|
|
|
+ .map((i) => ({
|
|
|
+ ...i,
|
|
|
+ id: undefined,
|
|
|
+ _isNew: undefined,
|
|
|
+ _isEdit: undefined,
|
|
|
+ questionnaireId: data.id,
|
|
|
+ }));
|
|
|
+
|
|
|
+ const editList = list
|
|
|
+ .filter((i) => i._isEdit)
|
|
|
+ .map((i) => ({
|
|
|
+ ...i,
|
|
|
+ _isNew: undefined,
|
|
|
+ _isEdit: undefined,
|
|
|
+ }));
|
|
|
+
|
|
|
+ if (editList.length || newList.length) {
|
|
|
+ // @ts-ignore
|
|
|
+ await questionaireApi.saveQuestions(editList.concat(newList));
|
|
|
+ }
|
|
|
+
|
|
|
handleCancel();
|
|
|
- }, [handleCancel, params]);
|
|
|
+ }, [handleCancel, params, list, message]);
|
|
|
|
|
|
const handleAddTopic = () => {
|
|
|
+ setEditQuestion(null);
|
|
|
setDrawerVisible(true);
|
|
|
};
|
|
|
|
|
@@ -35,14 +118,89 @@ export default function QuestionnaireCreateOrEditPage() {
|
|
|
setDrawerVisible(false);
|
|
|
};
|
|
|
|
|
|
+ const handleDelete = useCallback(
|
|
|
+ async (item: QuestionType, idx: number) => {
|
|
|
+ const temp = [...list];
|
|
|
+
|
|
|
+ setList((list) => {
|
|
|
+ const newList = [...list];
|
|
|
+ newList.splice(idx, 1);
|
|
|
+ return newList;
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!item._isNew) {
|
|
|
+ try {
|
|
|
+ await questionaireApi.deleteQuestion(item.id);
|
|
|
+ } catch (err) {
|
|
|
+ setList(temp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ [list]
|
|
|
+ );
|
|
|
+
|
|
|
+ const handleEditQues = useCallback((item: QuestionType) => {
|
|
|
+ setEditQuestion(item);
|
|
|
+ setDrawerVisible(true);
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ const COLUMNS = useMemo(() => {
|
|
|
+ return [
|
|
|
+ {
|
|
|
+ title: "题目序号",
|
|
|
+ render: (text: any, record: any, index: number) => {
|
|
|
+ return <span>{index + 1}</span>;
|
|
|
+ },
|
|
|
+ },
|
|
|
+ {
|
|
|
+ title: "题目描述",
|
|
|
+ dataIndex: "question",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ title: "选项",
|
|
|
+ dataIndex: "answer",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ title: "类型",
|
|
|
+ render: (item: QuestionType) => {
|
|
|
+ return <span>{item.type === 1 ? "单选" : "多选"}</span>;
|
|
|
+ },
|
|
|
+ },
|
|
|
+ {
|
|
|
+ title: "允许自定义答案",
|
|
|
+ render: (item: QuestionType) => {
|
|
|
+ return <span>{item.hasDiy ? "是" : "否"}</span>;
|
|
|
+ },
|
|
|
+ },
|
|
|
+ {
|
|
|
+ title: "操作",
|
|
|
+ render: (idx: number, item: QuestionType) => {
|
|
|
+ return (
|
|
|
+ <DageTableActions
|
|
|
+ onEdit={handleEditQues.bind(undefined, item)}
|
|
|
+ onDelete={handleDelete.bind(undefined, item, idx)}
|
|
|
+ />
|
|
|
+ );
|
|
|
+ },
|
|
|
+ },
|
|
|
+ ];
|
|
|
+ }, [handleDelete, handleEditQues]);
|
|
|
+
|
|
|
return (
|
|
|
- <div style={{ position: "relative" }}>
|
|
|
+ <App style={{ position: "relative" }}>
|
|
|
{loading && <MemoSpinLoding />}
|
|
|
|
|
|
- <Form ref={formRef} labelCol={{ span: 2 }}>
|
|
|
+ <Form
|
|
|
+ ref={formRef}
|
|
|
+ labelCol={{ span: 2 }}
|
|
|
+ initialValues={{
|
|
|
+ display: 1,
|
|
|
+ publishDate: dayjs(),
|
|
|
+ }}
|
|
|
+ >
|
|
|
<Form.Item
|
|
|
label="标题"
|
|
|
- name="title"
|
|
|
+ name="name"
|
|
|
rules={[{ required: true, message: "请输入内容" }]}
|
|
|
>
|
|
|
<Input
|
|
@@ -53,11 +211,7 @@ export default function QuestionnaireCreateOrEditPage() {
|
|
|
/>
|
|
|
</Form.Item>
|
|
|
|
|
|
- <Form.Item
|
|
|
- label="摘要"
|
|
|
- name="content"
|
|
|
- rules={[{ required: true, message: "请输入内容" }]}
|
|
|
- >
|
|
|
+ <Form.Item label="摘要" name="description">
|
|
|
<Input.TextArea
|
|
|
className="w450"
|
|
|
placeholder="请输入内容,最多200字"
|
|
@@ -67,35 +221,30 @@ export default function QuestionnaireCreateOrEditPage() {
|
|
|
/>
|
|
|
</Form.Item>
|
|
|
|
|
|
- <Form.Item
|
|
|
- label="题目"
|
|
|
- name="content"
|
|
|
- rules={[{ required: true, message: "请新增题目" }]}
|
|
|
- >
|
|
|
+ <Form.Item label="题目" required>
|
|
|
<Button type="primary" onClick={handleAddTopic}>
|
|
|
新增
|
|
|
</Button>
|
|
|
+
|
|
|
+ <Table
|
|
|
+ loading={optsLoading}
|
|
|
+ className="page-table"
|
|
|
+ dataSource={list}
|
|
|
+ columns={COLUMNS}
|
|
|
+ rowKey="id"
|
|
|
+ />
|
|
|
</Form.Item>
|
|
|
|
|
|
<Form.Item
|
|
|
label="发布日期"
|
|
|
- name="date"
|
|
|
+ name="publishDate"
|
|
|
rules={[{ required: true, message: "请选择日期" }]}
|
|
|
>
|
|
|
<DatePicker className="w220" />
|
|
|
</Form.Item>
|
|
|
|
|
|
- <Form.Item label="展示状态" name="type">
|
|
|
- <Select
|
|
|
- defaultValue="lucy"
|
|
|
- style={{ width: 220 }}
|
|
|
- options={[
|
|
|
- { value: "jack", label: "Jack" },
|
|
|
- { value: "lucy", label: "Lucy" },
|
|
|
- { value: "Yiminghe", label: "yiminghe" },
|
|
|
- { value: "disabled", label: "Disabled", disabled: true },
|
|
|
- ]}
|
|
|
- />
|
|
|
+ <Form.Item label="展示状态" name="display" valuePropName="checked">
|
|
|
+ <Switch />
|
|
|
</Form.Item>
|
|
|
</Form>
|
|
|
|
|
@@ -103,7 +252,22 @@ export default function QuestionnaireCreateOrEditPage() {
|
|
|
<FormPageFooter onSubmit={handleSubmit} onCancel={handleCancel} />
|
|
|
)}
|
|
|
|
|
|
- <TopicDrawer open={drawerVisible} close={handleCloseDrawer} />
|
|
|
- </div>
|
|
|
+ <TopicDrawer
|
|
|
+ item={editQuestion}
|
|
|
+ open={drawerVisible}
|
|
|
+ close={handleCloseDrawer}
|
|
|
+ add={(val) => setList((list) => [...list, val])}
|
|
|
+ edit={(val) => {
|
|
|
+ const index = list.findIndex((i) => i.id === val.id);
|
|
|
+ if (index > -1) {
|
|
|
+ setList((list) => {
|
|
|
+ const newList = [...list];
|
|
|
+ newList.splice(index, 1, val);
|
|
|
+ return newList;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }}
|
|
|
+ />
|
|
|
+ </App>
|
|
|
);
|
|
|
}
|