import React, { Component } from'react'; import { Form, Input, Button, Radio, Checkbox, Select, message } from 'antd'; import { dictionary } from "api/dict/index"; import {findIndustry, getDetail, update} from 'api/question'; const { Option } = Select; interface QuestionState { id: string|null; questionTypes: string; industryId: string; serviceTypeId: string; questionContent: string; answerOptions: {[ key: string]: string }; answer: string; } interface States { industryDict: any; serviceTypeDict: any; isLoading: boolean; question: QuestionState; } class QuestionUp extends Component { formRef: any; constructor(props: any) { super(props); this.formRef = React.createRef(); this.state = { industryDict: undefined, serviceTypeDict: undefined, isLoading: false, question: { id: '', questionTypes: '1', industryId: '', serviceTypeId: '', questionContent: '', answerOptions: { A: '', B: '', C: '', D: '' }, answer: '' }, }; } componentDidMount() { this.findIndustry() this.findDict(); this.getDetail(); } // 监管行业 findIndustry() { findIndustry().then((res: any) => { if (res.data) { this.setState({ industryDict: res.data }); } }); } // AQ服务类型 findDict() { dictionary('serviceTypeDict').then((res) => { if (res.data) { this.setState({ serviceTypeDict: res.data }); } }); } // 题目详情 getDetail = () => { const id = sessionStorage.getItem('id'); sessionStorage.removeItem('id'); getDetail(id).then((res: any) => { if (res.data) { let answerOptions = {}; const options = res.data.options.split(','); options.forEach((option: any) => { const [key, value] = option.split(':'); answerOptions[key] = value; }); const question: QuestionState = { id:id, questionTypes: String(res.data.questionTypes), industryId: String(res.data.industryId), serviceTypeId: String(res.data.serviceTypeId), questionContent: String(res.data.questionContent), answerOptions: answerOptions, answer: res.data.answer }; this.setState({ question: question }, () => { const formValues = {}; formValues['questionTypes'] = question.questionTypes; formValues['industryId'] = question.industryId; formValues['serviceTypeId'] = question.serviceTypeId; formValues['questionContent'] = question.questionContent; formValues['answer'] = question.answer; formValues['answerOption_A'] = question.answerOptions.A; formValues['answerOption_B'] = question.answerOptions.B; formValues['answerOption_C'] = question.answerOptions.C; formValues['answerOption_D'] = question.answerOptions.D; this.formRef.current.setFieldsValue(formValues); }); } }).catch(() => { message.error('获取题目详情失败,请重试'); }); }; // 题型 handleQuestionTypeChange = (value: string) => { this.setState((prevState) => { prevState.question.questionTypes = value; prevState.question.answer = ''; return { question: prevState.question }; }); }; // 单选框 handleRadioChange = (value: string) => { this.setState((prevState) => { prevState.question.answer = value; return { question: prevState.question }; }); }; // 多选框 handleCheckboxChange = (type: string, values: (string | number | boolean)[] | null) => { const currentAnswer = this.state.question.answer; const currentSelectedArray = currentAnswer? currentAnswer.split(',') : []; if (!values || values.length === 0) { const newSelectedArray = currentSelectedArray.filter(val => val!== type); newSelectedArray.sort(); this.setState((prevState) => { prevState.question.answer = newSelectedArray.join(','); return { question: prevState.question }; }); } else { const stringValues = values.map((val) => String(val)); const newSelectedArray = Array.from(new Set([...currentSelectedArray, ...stringValues])); newSelectedArray.sort(); this.setState((prevState) => { prevState.question.answer = newSelectedArray.join(','); return { question: prevState.question }; }); } }; // 答案 handleAnswerOptionChange = (option: string, value: string) => { this.setState((prevState) => { prevState.question.answerOptions[option] = value; return { question: prevState.question }; }); }; // 保存修改 update = () => { this.setState({ isLoading: true }); this.formRef.current.validateFields().then((values: Record) => { const { question } = this.state; const updatedQuestion = { ...question, industryId: values['industryId'], serviceTypeId: values['serviceTypeId'], questionContent: values['questionContent'] }; this.setState({ question: updatedQuestion }); update(updatedQuestion).then((res) => { const isSuccess = res.data; if (isSuccess) { message.success('修改成功'); this.setState({ isLoading: false }); this.props.history.push('/questionList'); } else { message.error('修改失败,请稍后重试'); this.setState({ isLoading: false }); } }).catch(() => { message.error('修改时发生错误,请检查'); this.setState({ isLoading: false }); }); }); }; render() { const { industryDict, serviceTypeDict, question, isLoading } = this.state; const handleListQuestion = () => { this.props.history.push('/questionList'); }; return (
this.handleQuestionTypeChange(e.target.value)}> 单选题 多选题
{question.questionTypes === '1'? ( this.handleRadioChange(e.target.value)} > A this.handleAnswerOptionChange('A', e.target.value)} /> ) : ( this.handleCheckboxChange('A', values,)}> A this.handleAnswerOptionChange('A', e.target.value)} /> )} {question.questionTypes === '1' ? ( this.handleRadioChange( e.target.value)} style={{ marginLeft: 20 }} > B this.handleAnswerOptionChange('B', e.target.value)} /> ) : ( this.handleCheckboxChange('B',values)}> B this.handleAnswerOptionChange( 'B', e.target.value)} /> )}
{question.questionTypes === '1'? ( this.handleRadioChange( e.target.value)} > C this.handleAnswerOptionChange('C', e.target.value)} /> ) : ( this.handleCheckboxChange('C', values)}> C this.handleAnswerOptionChange( 'C', e.target.value)} /> )} {question.questionTypes === '1'? ( this.handleRadioChange( e.target.value)} style={{ marginLeft: 20 }} > D this.handleAnswerOptionChange('D', e.target.value)} /> ) : ( this.handleCheckboxChange('D',values)}> D this.handleAnswerOptionChange('D', e.target.value)} /> )}
); } } export default QuestionUp;