import React, { Component } from 'react';
// import LeftNav from './LeftNav'
import Header from './Header'
import MainContentContainer from './MainContentContainer'
import TPIContext from '../../context/TPIContext'
import TPIContextProvider from '../../context/TPIContextProvider'
import update from 'immutability-helper'
import axios from 'axios';
// import { getUrl } from 'educoder'
// let _url_origin = getUrl()
// const $ = window.$
// let prefix = '/react/build'
// if (window.location.port == 3007) {
// prefix = ''
// } else {
// }
// 下面这个加载方式有问题,改到html 写script标签
// if (!window['TPIScriptLoaded']) {
// // $('head').append($('')
// // .attr('href', `${_url_origin}${prefix}/js/xterm/xterm.css`));
// $.when(
// $.getScript( `${_url_origin}/assets/kindeditor/kindeditor.js` ),
// $.getScript( `${_url_origin}${prefix}/js/create_kindeditor.js` ),
// $.getScript( `${_url_origin}/javascripts/educoder/edu_application.js` ),
// $.Deferred(function( deferred ){
// $( deferred.resolve );
// })
// ).done(function(){
// window.TPIScriptLoaded = true
// //place your code here, the scripts are all loaded
// // callback && callback()
// });
// } else {
// // callback && callback()
// }
class Index extends Component {
constructor(props) {
super(props)
// tpi body overflow hidden
var styles = window.$('body').attr('style');
styles += 'overflow: hidden !important;'
window.$('body').attr('style', styles);
window.$('#root').css('position', 'absolute')
// TPI浏览器缩放
setTimeout(() => {
const agent = navigator.userAgent;
if (agent.indexOf('Mac OS') == -1) {
window._initZoomCheck()
}
}, 800)
this.onDrawerButtonClick = this.onDrawerButtonClick.bind(this)
this.onStarChange = this.onStarChange.bind(this)
this.saveChallengeStar = this.saveChallengeStar.bind(this)
this.onChallengesDrawerClose = this.onChallengesDrawerClose.bind(this)
this.starArray = [];
// 将原本放置在Header.js中的state上提到Index.js
this.state = {
challengesDrawerOpen: false,
taskListLoading: true,
challenges: [], // 任务列表
}
}
onChallengesDrawerClose() {
this.setState({
challengesDrawerOpen: false
})
}
onDrawerButtonClick() {
if (this.props.loading) {
return;
}
const { shixun, myshixun } = this.props;
// var getChallengesUrl = `/api/v1/games/${this.props.game.identifier}/challenges`;
var getChallengesUrl = `/myshixuns/${myshixun.identifier}/challenges.json`;
this.setState({
taskListLoading: true,
challengesDrawerOpen:true
})
axios.get(getChallengesUrl, {
// withCredentials: true,
})
.then((response) => {
console.log(response);
if (response.data.status == -1) {
console.error('获取任务列表失败!')
return;
}
this.starArray = [];
this.setState({
challenges: response.data,
taskListLoading: false,
})
})
.catch(function (error) {
console.log(error);
});
}
onStarChange(challenge, index, value) {
this.starArray[index] = value;
}
saveChallengeStar(challenge, index) {
const { challenges } = this.state
const { shixun, game } = this.props;
const value = this.starArray[index]
if (!value && !challenges[index].star) {
this.props.showSnackbar('请先选择评星数量(1-5星)!')
return;
}
// var saveChallengeStarUrl = `/api/v1/games/${challenge.identifier}/star`;
var saveChallengeStarUrl = `/tasks/${challenge.identifier}/star.json?shixun_id=${shixun.id}&star=${value}`;
axios.get(saveChallengeStarUrl
// , {
// star: value,
// shixun_id: shixun.id
// }
// ,{
// withCredentials: true,
// }
)
.then((response) => {
console.log(response);
// {"reward_code":86} TODO 金币数量变化
if (response.data.reward_code === -1) {
this.props.showSnackbar('该任务已评过星了!')
return;
}
if (challenges && challenges[index]) { // 每次展开都会重新加载任务列表
const new_challenges = challenges.slice(0);
new_challenges[index].star = value;
this.setState({ challenges: new_challenges })
}
})
.catch(function (error) {
console.log(error);
});
}
render() {
const context = this.props;
// 不将cost_time传给MainContent
let gameWithoutCostTime;
if (context.game) {
gameWithoutCostTime = Object.assign({}, context.game);
gameWithoutCostTime.cost_time = undefined;
}
return (
{/* 区分下repo和evaluate模块的,以及子模块的 */}
);
}
}
export default Index;