forked from lzxb/react-cnode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopicCreate.jsx
More file actions
144 lines (132 loc) · 4.24 KB
/
TopicCreate.jsx
File metadata and controls
144 lines (132 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {NavLink as Link } from 'react-router-dom';
import { connect } from 'react-redux';
import action from '../Action/Index';
import { Tool, merged } from '../Tool';
import { DataLoad, DataNull, Header, TipMsgSignin, Footer, GetData } from './common/index';
/**
* 模块入口
*
* @class Main
* @extends {Component}
*/
class Main extends Component {
constructor(props) {
super(props);
/**
* 初始化组件状态
*/
this.state = {
title: '',
tab: '',
content: '',
accesstoken: this.props.User ? this.props.User.accesstoken : ''
};
this.postState = false;
/**
* 发表主题
*
* @returns
*/
this.rightClick = () => {
var {state} = this;
if (this.postState) return false;
if (!state.tab) {
return alert('请选择发表类型');
} else if (state.title.length < 10) {
return alert('标题字数10字以上');
} else if (state.content.length < 30) {
return alert('内容字数30字以上');
}
this.postState = true;
Tool.post('/api/v1/topics', this.state, (res) => {
if (res.success) {
this.context.router.history.push({
pathname: '/topic/' + res.topic_id
});
} else {
alert('发表失败');
this.postState = false;
}
}, () => {
alert('发表失败');
this.postState = false;
});
}
/**
* 监听用户选择发表类型
*
* @param {Object} e 事件出发的元素
*/
this.tabInput = (e) => {
this.state.tab = e.target.value;
}
/**
* 监听用户输入标题
*
* @param {Object} e //事件触发的元素
*/
this.titleInput = (e) => {
this.state.title = e.target.value;
}
/**
* 监听用户输入内容
*
* @param {Object} e //事件触发的元素
*/
this.contentInput = (e) => {
this.state.content = e.target.value;
}
}
render() {
var { User} = this.props;
var headerSet = {};
var main = null;
if (!User) {
main = <TipMsgSignin />
} else {
main = <NewTopic {...this.state} tabInput={this.tabInput} titleInput={this.titleInput} contentInput={this.contentInput} />
headerSet = {
rightIcon: 'fabu',
rightClick: this.rightClick
};
}
return (
<div>
<Header title="发表主题" {...headerSet} />
{main}
<Footer index="1" />
</div>
);
}
shouldComponentUpdate() {
return false;
}
}
Main.contextTypes = {
router: PropTypes.object.isRequired
}
class NewTopic extends Component {
render() {
return (
<div className="topic-create">
<div className="item">
<select name="tab" defaultValue={this.props.tab} onInput={this.props.tabInput}>
<option value="">请选择发表类型</option>
<option value="share">分享</option>
<option value="ask">问答</option>
<option value="job">招聘</option>
</select>
</div>
<div className="item">
<input type="text" defaultValue={this.props.title} onInput={this.props.titleInput} placeholder="标题字数 10 字以上" />
</div>
<div className="item">
<textarea defaultValue={this.props.content} onInput={this.props.contentInput} placeholder="内容字数 30 字以上"></textarea>
</div>
</div>
);
}
}
export default connect((state) => { return { User: state.User } }, action('TopicCreate'))(Main); //连接redux