Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Commit e195669

Browse files
author
soliury
committed
finish publish topic
1 parent 35ecf84 commit e195669

File tree

5 files changed

+229
-26
lines changed

5 files changed

+229
-26
lines changed

app/components/overlay/Loading.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
var React = require('react-native')
2+
var { Icon } = require('react-native-icons')
3+
var Modal = require('react-native-modal')
4+
5+
var window = require('../../util/window')
6+
var { width, height } = window.get()
7+
8+
9+
var {
10+
View,
11+
StyleSheet,
12+
Component,
13+
Text,
14+
Image,
15+
ActivityIndicatorIOS
16+
} = React
17+
18+
19+
class Loading extends Component {
20+
constructor(props) {
21+
super(props)
22+
}
23+
24+
25+
render() {
26+
return (
27+
<Modal
28+
style={modalStyles}
29+
hideCloseButton={true}
30+
isVisible={this.props.isVisible}
31+
>
32+
33+
<ActivityIndicatorIOS
34+
style={modalStyles.row}
35+
size='large'
36+
></ActivityIndicatorIOS>
37+
</Modal>
38+
)
39+
}
40+
}
41+
42+
43+
var modalStyles = StyleSheet.create({
44+
container: {
45+
position: 'absolute',
46+
top: 0,
47+
bottom: 0,
48+
left: 0,
49+
right: 0,
50+
backgroundColor: 'transparent',
51+
justifyContent: 'center',
52+
alignItems: 'center'
53+
},
54+
backdrop: {
55+
position: 'absolute',
56+
top: 0,
57+
bottom: 0,
58+
left: 0,
59+
right: 0,
60+
backgroundColor: '#000000',
61+
opacity: 0.3,
62+
},
63+
modal: {
64+
backgroundColor: 'white',
65+
flex: 1,
66+
height: 120,
67+
width: 120,
68+
flexDirection: 'column',
69+
justifyContent: 'center',
70+
alignItems: 'center',
71+
borderRadius: 5
72+
}
73+
})
74+
75+
76+
module.exports = Loading

app/configs/Router.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ class Router {
101101
replaceWithHome() {
102102
this.navigator.popToTop()
103103
}
104+
105+
replaceWithTopic(props) {
106+
let routesList = this.navigator.getCurrentRoutes()
107+
let index = routesList[routesList.length - 1].index
108+
var route = {
109+
props: props,
110+
index: index,
111+
component: Topic,
112+
sceneConfig: customFloatFromRight
113+
}
114+
this.navigator.replace(route)
115+
}
104116
}
105117

106118
module.exports = Router

app/containers/Publish.js

Lines changed: 117 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
var React = require('react-native')
22
var { Icon } = require('react-native-icons')
33
var Modal = require('react-native-modal')
4-
4+
var precomputeStyle = require('precomputeStyle')
5+
var KeyboardEvents = require('react-native-keyboardevents')
6+
var KeyboardEventEmitter = KeyboardEvents.Emitter
57

68
var Nav = require('../components/Nav')
9+
var Loading = require('../components/overlay/Loading')
10+
11+
var TopicService = require('../services/TopicService')
712

813
var window = require('../util/window')
914
var { width, height } = window.get()
@@ -17,7 +22,8 @@ var {
1722
StyleSheet,
1823
PickerIOS,
1924
TouchableOpacity,
20-
TextInput
25+
TextInput,
26+
ScrollView
2127
} = React
2228

2329

@@ -35,21 +41,88 @@ class Publish extends Component {
3541
this.state = {
3642
selectTab: 'share',
3743
isPickerShow: false,
38-
dirty: false
44+
dirty: false,
45+
isPublishing: false
3946
}
47+
this.updateKeyboardSpace = this.updateKeyboardSpace.bind(this)
48+
this.resetKeyboardSpace = this.resetKeyboardSpace.bind(this)
4049
}
4150

4251

43-
_renderPickerContent() {
44-
return Object.keys(this.tabs).map(tab=> {
45-
return (
46-
<PickerItemIOS
47-
key={tab}
48-
value={tab}
49-
label={this.tabs[tab]}
50-
></PickerItemIOS>
51-
)
52+
updateKeyboardSpace(frames) {
53+
this.contentInput.setNativeProps({
54+
height: contentHeight - frames.end.height
55+
})
56+
}
57+
58+
resetKeyboardSpace() {
59+
this.contentInput.setNativeProps({
60+
height: contentHeight
61+
})
62+
}
63+
64+
65+
componentDidMount() {
66+
KeyboardEventEmitter.on(KeyboardEvents.KeyboardDidShowEvent, this.updateKeyboardSpace)
67+
KeyboardEventEmitter.on(KeyboardEvents.KeyboardWillHideEvent, this.resetKeyboardSpace)
68+
}
69+
70+
componentWillUnmount() {
71+
KeyboardEventEmitter.off(KeyboardEvents.KeyboardDidShowEvent, this.updateKeyboardSpace)
72+
KeyboardEventEmitter.off(KeyboardEvents.KeyboardWillHideEvent, this.resetKeyboardSpace)
73+
}
74+
75+
76+
_blur() {
77+
this.titleInput.blur()
78+
this.contentInput.blur()
79+
}
80+
81+
82+
_validateForm() {
83+
if (!this.state.dirty) {
84+
return window.alert('请选择要发布的板块!')
85+
}
86+
87+
88+
if (!this.titleInputValue || this.titleInput == '') {
89+
return window.alert('标题不能为空!')
90+
}
91+
92+
if (!this.contentInputValue || this.contentInput == '') {
93+
return window.alert('你还没写东西呢!')
94+
}
95+
96+
if (this.titleInputValue.length <= 10) {
97+
return window.alert('标题字数必须在10字以上!')
98+
}
99+
100+
return true
101+
}
102+
103+
104+
_submit() {
105+
if (this.state.isPublishing || !this._validateForm()) return
106+
107+
this.setState({
108+
isPublishing: true
52109
})
110+
TopicService.req.publish(this.titleInputValue, this.state.selectTab, this.contentInputValue, this.props.state.user.token)
111+
.then(topicId=> {
112+
this.props.router.replaceWithTopic({
113+
topicId: topicId,
114+
from: 'html'
115+
})
116+
})
117+
.catch(err=> {
118+
console.log(err)
119+
window.alert('发布帖子失败!')
120+
})
121+
.done(()=> {
122+
this.setState({
123+
isPublishing: false
124+
})
125+
})
53126
}
54127

55128

@@ -58,8 +131,7 @@ class Publish extends Component {
58131
isPickerShow: true,
59132
dirty: true
60133
})
61-
this.titleInput.blur()
62-
this.contentInput.blur()
134+
this._blur()
63135
}
64136

65137

@@ -77,6 +149,19 @@ class Publish extends Component {
77149
}
78150

79151

152+
_renderPickerContent() {
153+
return Object.keys(this.tabs).map(tab=> {
154+
return (
155+
<PickerItemIOS
156+
key={tab}
157+
value={tab}
158+
label={this.tabs[tab]}
159+
></PickerItemIOS>
160+
)
161+
})
162+
}
163+
164+
80165
render() {
81166
var router = this.props.router
82167

@@ -85,16 +170,15 @@ class Publish extends Component {
85170
text: '返回',
86171
onPress: ()=> {
87172
router.pop()
173+
this._blur()
88174
}
89175
},
90176
Center: {
91177
text: '发表帖子'
92178
},
93179
Right: {
94180
text: '发布',
95-
onPress: ()=> {
96-
97-
}
181+
onPress: ()=> this._submit()
98182
}
99183
}
100184

@@ -105,7 +189,9 @@ class Publish extends Component {
105189
navs={navs}
106190
></Nav>
107191

108-
<View style={styles.content}>
192+
<ScrollView
193+
ref={view=>this.contentView=view}
194+
style={styles.content}>
109195
<TouchableOpacity
110196
onPress={this._onPickerPress.bind(this)}
111197
>
@@ -142,8 +228,9 @@ class Publish extends Component {
142228
ref={view=>this.titleInput=view}
143229
placeholder='请输入标题'
144230
style={styles.titleInput}
145-
//onChangeText={(text) => this.setState({text})}
146-
//value={this.state.text}
231+
onChangeText={(text) => {
232+
this.titleInputValue = text
233+
}}
147234
/>
148235
</View>
149236

@@ -153,11 +240,12 @@ class Publish extends Component {
153240
value={'\n'+config.replySuffix}
154241
style={styles.topicContent}
155242
multiline={true}
156-
//onChangeText={(text) => this.setState({text})}
157-
//value={this.state.text}
243+
onChangeText={(text) => {
244+
this.contentInputValue = text
245+
}}
158246
/>
159247

160-
</View>
248+
</ScrollView>
161249

162250

163251
<Modal
@@ -180,14 +268,18 @@ class Publish extends Component {
180268

181269
</Modal>
182270

271+
<Loading
272+
isVisible={this.state.isPublishing}
273+
></Loading>
274+
183275
</View>
184276
)
185277
}
186278
}
187279

188280

189281
var textColor = 'rgba(0,0,0,0.7)'
190-
282+
var contentHeight = height - 51 * 2 - Nav.navHeight
191283

192284
var styles = StyleSheet.create({
193285
container: {
@@ -229,7 +321,7 @@ var styles = StyleSheet.create({
229321
alignItems: 'flex-start',
230322
justifyContent: 'space-between',
231323
paddingTop: 20,
232-
height: height - 51 * 2 - Nav.navHeight
324+
height: contentHeight
233325
}
234326
})
235327

app/containers/Topic.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,12 @@ class Topic extends Component {
298298
}
299299

300300
return (
301-
<View style={styles.container}></View>
301+
<View style={styles.container}>
302+
<ActivityIndicatorIOS
303+
size="large"
304+
animating={true}
305+
style={{marginTop:20,width:width,flex:1}}/>
306+
</View>
302307
)
303308

304309
}

app/services/TopicService.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,23 @@ req.upComment = function (replyId, token) {
130130
}
131131

132132

133+
req.publish = function (title, tab, content, token) {
134+
let url = `${config.domain + config.apiPath}/topics`
135+
const body = {
136+
title: title,
137+
tab: tab,
138+
content: content,
139+
accesstoken: token
140+
}
141+
return request.post(url, body)
142+
.then(data=> {
143+
if (data.success) {
144+
return data.topic_id
145+
}
146+
throw 'publish failed'
147+
})
148+
}
149+
150+
133151
exports.storage = storage
134152
exports.req = req

0 commit comments

Comments
 (0)