import React, { useCallback, useEffect, useState } from "react"; import styles from "./index.module.scss"; import { A2QuestionResType, A2QuestionType } from "@/types"; import { CheckOutlined, CloseOutlined } from "@ant-design/icons"; import classNames from "classnames"; import { A2_APIgoodsSaveAnswer } from "@/store/action/A2Main"; const objC = { 0: "A", 1: "B", 2: "C", 3: "D", 4: "E", }; type Props = { list: A2QuestionType[]; }; function Left3({ list }: Props) { const [listRes, setListRes] = useState([]); useEffect(() => { const arr = [] as A2QuestionResType[]; list.forEach((v) => { const temp = JSON.parse(v.answer); arr.push({ id: v.id, txt: v.description, goodsId: v.goodsId, title: v.question, ok: temp.correct, answer: temp.answer, done: false, mySelect: "", }); }); setListRes(arr); }, [list]); // 选择答案 const selecttFu = useCallback( async (item: A2QuestionResType, val: string) => { if (!item.done) { const arr = listRes.map((v) => { return { ...v, done: v.id === item.id ? true : v.done, mySelect: v.id === item.id ? val : v.mySelect, }; }); setListRes(arr); // 发送请求 await A2_APIgoodsSaveAnswer(item.id, val === item.ok ? 1 : 0); } }, [listRes] ); return (
单选题
{listRes.map((v1, i1) => (
{i1 + 1}.{v1.title}
{v1.answer.map((v2, i2) => (
selecttFu(v1, v2.val)} > {Reflect.get(objC, i2)}.{v2.name}
))} {/* 回答情况 */} {/* 说明 */}
))}
); } const MemoLeft3 = React.memo(Left3); export default MemoLeft3;