import React, { Component } from 'react'; import { Breadcrumb,Layout,Table, Divider, Tag,Badge,Row, Col,Button, Menu, Icon} from 'antd'; import { Link } from 'react-router-dom'; import axios from 'axios'; import {markdownToHTML,getImageUrl} from 'educoder'; import CompetitionContents from './CompetitionCommonChild/CompetitionContents'; import CompetitionContentsChart from './CompetitionCommonChild/CompetitionContentsChart'; import CompetitionContentsMd from './CompetitionCommonChild/CompetitionContentsMd'; import CompetitionContentspdf from './CompetitionCommonChild/CompetitionContentspdf'; import './CompetitionCommon.css'; const {Sider } = Layout; class CompetitionCommon extends Component{ constructor(props) { super(props) this.state={ data: undefined, bannerdata: undefined, module_type: undefined, mdContentdata: undefined, chart_rules: undefined, Competitionedittype: false, chartdata: undefined, has_url: false, signupdata: undefined } } componentDidMount(){ window.document.title = '竞赛'; if(this.props.match.params.identifier!=null){ this.getbannerdata(); // this.setState({ // thiskeys:this.props.location.search.replace('?menu=', '') // }) // let url=`/competitions/${this.props.match.params.identifier}.json`; // axios.get(url).then((response) => { // if(response.status===200){ // this.setState({ // bannerdata:response.data // }) // } // }).catch((error) => { // console.log(error) // }) } } componentDidUpdate = (prevProps) => { if (prevProps.user != this.props.user) { console.log("componentDidUpdatess"); console.log(this.props.user); if (this.props.user && this.props.user.login != "") { const zul = `/competitions/${this.props.match.params.identifier}/competition_staff.json`; axios.get((zul)).then((result) => { if (result) { if (result.data) { this.setState({ signupdata: result.data }) } } }).catch((error) => { //console.log(error); }) } } } //获取头部信息 getbannerdata=()=>{ let menuid=this.props.location.search.replace('?menu=', ''); let url=`/competitions/${this.props.match.params.identifier}/common_header.json`; axios.get(url).then((response) => { if(response.status===200){ this.setState({ data: response.data, thiskeys: menuid === undefined || menuid === "" ? response.data.competition_modules[0].id : menuid, mode: response.data.mode }) if(menuid===undefined||menuid===""){ this.getrightdata( response.data.competition_modules[0].id, response.data.competition_modules[0].module_type, response.data.competition_modules[0].module_url, response.data.competition_modules[0].has_url ) }else{ let newlist=response.data.competition_modules; newlist.map((item,key)=>{ if(`${item.id}`===`${menuid}`){ this.getrightdata( item.id, item.module_type, item.module_url, item.has_url ) } }) } } }).catch((error) => { console.log(error) }) //this.props.user 有可能为空 if (this.props.user && this.props.user.login != "") { const zul = `/competitions/${this.props.match.params.identifier}/competition_staff.json`; axios.get((zul)).then((result) => { if (result) { if (result.data) { this.setState({ signupdata: result.data }) } } }).catch((error) => { //console.log(error); }) } } getrightdatas=(e)=>{ let keys = parseInt(e.key); this.getlistdata(keys) this.props.history.replace(`?menu=${keys}`); } getlistdata=(keys,listkey)=>{ let{data}=this.state; this.setState({ thiskeys:keys }) data&&data.competition_modules.map((item,key)=>{ if(item.module_type!="enroll") { if (keys === item.id) { this.getrightdata(item.id, item.module_type, item.module_url, item.has_url, listkey) return } } }) } getnewchartdata=(typeid,tabkey)=>{ if(typeid==="chart"){ let url=`/competitions/${this.props.match.params.identifier}/chart_rules.json`; axios.get(url) .then((response) => { if(response.status===200){ this.setState({ chart_rules:response.data, tabkey: tabkey === undefined ? response.data.stages[0].id === null ? "0" : `${response.data.stages[0].id}` : tabkey }) } }).catch((error) => { console.log(error) }) } } getrightdata=(id,typeid,module_url,has_url,listkey)=>{ // if(typeid==="enroll"){ // this.props.history.replace(`/competitions/${this.props.match.params.identifier}/enroll`); // return // } this.getnewchartdata(typeid, listkey) if(has_url===false){ let url=`${module_url}`; axios.get(url).then((response) => { if(response.status===200){ if(typeid==="chart"){ this.setState({ chartdata:response.data }) }else{ this.setState({ mdContentdata:response.data }) } } }).catch((error) => { console.log(error) }) }else{ if (module_url.substring(0, 7) == 'http://' || module_url.substring(0, 8) == 'https://') { // window.location.href= module_url window.open(module_url) }else{ window.open(`https://${module_url}`) // window.location.href=; } return } this.setState({ module_id:id, module_type:typeid, has_url:has_url }) } Competitionedit=()=>{ this.setState({ Competitionedittype: true }) } hideCompetitionedit=()=>{ this.setState({ Competitionedittype:false }) } gotocourse=(url)=>{ if(this.props.checkIfLogin()===false){ this.props.showLoginDialog() return } if(this.props.checkIfProfileCompleted()===false){ this.props.showProfileCompleteDialog() return } if(url===undefined){ let {data,signupdata}=this.state; if(signupdata.enrolled===true){ this.props.history.replace(`/courses/${data.course_id}`); }else{ if(data.member_of_course===true){ this.props.history.replace(`/courses/${data.course_id}`); }else{ // 以学生身份调用加入课堂 进入课堂首页 let url="/courses/apply_to_join_course.json" axios.post(url, { invite_code: data.invite_code, student:1 } ).then((response) => { if(response.data.status===0){ this.props.history.replace(`/courses/${data.course_id}`); } }) } } }else{ if (url === "personal") { this.Personalregistration() } else { this.props.history.replace(url); } } } Personalregistration = () => { let {signupdata} = this.state; let urls = `/competitions/${this.props.match.params.identifier}/enroll`; if (signupdata.enroll_ended === true) { //已截止 this.props.showNotification(`报名已截止`); return; } if (signupdata.enrolled === true) { this.props.showNotification(`你已经报名,不能重复报名!`); return; } const url = `/competitions/${this.props.match.params.identifier}/competition_teams.json`; axios.post(url).then((response) => { if (response) { if (response.data) { this.props.showNotification(`报名成功,预祝您夺得桂冠!`); this.props.history.replace(urls); } } }).catch((error) => { }); } Competitioncallback=(key)=>{ this.setState({ tabkey:key }) let url=`/competitions/${this.props.match.params.identifier}/charts.json`; axios.get(url,{params:{ stage_id:key===0||key===null?undefined:key }}).then((response) => { if(response.status===200){ this.setState({ chartdata:response.data }) } }).catch((error) => { console.log(error) }) } render() { let {data, module_type, Competitionedittype, signupdata} = this.state; return ( data===undefined?"":