-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendTopic.js
152 lines (138 loc) · 4.98 KB
/
SendTopic.js
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
145
146
147
148
149
150
151
152
'use strict';
import React,{
ActivityIndicatorIOS,
Alert,
Animated,
Component,
Dimensions,
Image,
Navigator,
ScrollView,
StyleSheet,
Text,
TextInput,
TouchableOpacity,
View,
WebView,
SegmentedControlIOS,
} from 'react-native';
import Store from './Store';
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor:'#333333',
paddingTop:20,
},
button: {
alignItems:'center',
justifyContent:'center',
paddingLeft:10,
paddingRight:10,
}
});
export default class SendTopic extends Component {
constructor(props) {
super(props);
this.state = {title:'', content:'', tab: '分享'};
}
getTabCode() {
switch (this.state.tab) {
case '招聘':
return 'job';
case '问答':
return 'ask';
default:
return 'share';
}
}
_onCommit() {
if (this.state.title.length < 10) {
Alert.alert('标题需要10个字以上');
return;
}
if (this.state.content.length < 5) {
Alert.alert('内容需要5个字以上');
return;
}
let url = 'https://cnodejs.org/api/v1/topics';
let body = `accesstoken=${Store.get('accesstoken')}&tab=${this.getTabCode()}&title=${encodeURIComponent(this.state.title)}&content=${encodeURIComponent(this.state.content)}`;
fetch(url, {
method: 'POST',
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body:body,
})
.then(res => res.json())
.then(res => {
console.log(res)
if (res.success) {
this.props.navigator.pop();
this.props.parent.refresh();
} else {
// TODO need Toast??
}
})
.catch(error => {
console.log(error);
// TODO need Toast??
})
.done();
}
render() {
let title = this.state.title;
let content = this.state.content;
return (
<View style={styles.container}>
<View style={{flexDirection:'row', height: 38, borderWidth:0, borderColor:'white', paddingTop:0}}>
<TouchableOpacity style={styles.button} onPress={() => this.props.navigator.pop()}>
<Text style={{color:'white', fontSize:15}}>关闭</Text>
</TouchableOpacity>
<Text style={{flex:1, textAlign: 'center', color:'white', fontSize:18, fontWeight:'bold', paddingTop:9}}>发表主题</Text>
<TouchableOpacity style={styles.button} onPress={this._onCommit.bind(this)}>
<Text style={{color:'white', fontSize:15}}>提交</Text>
</TouchableOpacity>
</View>
<View style={{backgroundColor:'rgb(220,220,220)', flex:1}}>
<Select style={{paddingLeft:10, paddingRight:10, marginTop: 10, flexDirection:'row'}} tab={this.state.tab}
onSelect={(tab) => this.setState({tab:tab})}/>
<TextInput
style={{padding:8, paddingTop:2, margin:10, height: 36, backgroundColor:'#fff', fontSize:16, borderRadius:5, borderBottomWidth:1, borderBottomColor:'grey',}}
onChangeText={(text) => this.setState({title: text})} multiline={true}
placeholder="标题10字以上" autoFocus={true}
/>
<TextInput
style={{paddingLeft:8, paddingRight:8, paddingTop:2, marginLeft:10, marginRight:10, height: 220, borderRadius:5,
borderWidth:0, borderColor: '#80bd00', backgroundColor:'#fff', fontSize:16,borderBottomWidth:1, borderBottomColor:'grey',}}
onChangeText={(text) => this.setState({content: text})}
placeholder="内容" multiline={true}
/>
</View>
</View>
);
}
}
class Select extends Component {
constructor(props) {
super(props);
}
render() {
return (
<View style={this.props.style}>
<TouchableOpacity style={[styles.button, {backgroundColor: this.props.tab == '分享'?'#fff':'#b1b1b1', paddingTop:10, paddingBottom:10, borderRadius:5,
borderBottomWidth:1, borderBottomColor:'grey',}]}
onPress={() => this.props.onSelect('分享')}>
<Text style={{color: this.props.tab == '分享'?'#000':'#fff', fontSize:16}}>分享</Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, {backgroundColor: this.props.tab == '问答'?'#fff':'#b1b1b1', paddingTop:10, paddingBottom:10, marginLeft:10, borderRadius:5,borderBottomWidth:1, borderBottomColor:'grey', }]}
onPress={() => this.props.onSelect('问答')}>
<Text style={{color: this.props.tab == '问答'?'#000':'#fff', fontSize:16}}>问答</Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, {backgroundColor: this.props.tab == '招聘'?'#fff':'#b1b1b1', paddingTop:10, paddingBottom:10, marginLeft:10, borderRadius:5, borderBottomWidth:1, borderBottomColor:'grey',}]}
onPress={() => this.props.onSelect('招聘')}>
<Text style={{color: this.props.tab == '招聘'?'#000':'#fff', fontSize:16}}>招聘</Text>
</TouchableOpacity>
</View>
);
}
}