From d021cee7a96d04ec192e9a364e18eee1a72752bd Mon Sep 17 00:00:00 2001 From: xuwenyang Date: Fri, 24 May 2019 17:47:44 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=88=9B=E5=BB=BA/=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=97=A5=E8=AE=B0=E6=9C=AC=202.=20=E5=88=A0=E9=99=A4=E6=97=A5?= =?UTF-8?q?=E8=AE=B0=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/page/NotebookDetailPage.js | 34 ++++++++ src/page/NotebookEditPage.js | 142 +++++++++++++++++++++++---------- src/page/UserPage.js | 14 +++- src/util/api.js | 28 ++++++- src/util/event.js | 4 + 5 files changed, 178 insertions(+), 44 deletions(-) create mode 100644 src/util/event.js diff --git a/src/page/NotebookDetailPage.js b/src/page/NotebookDetailPage.js index a605f17..e4656ff 100644 --- a/src/page/NotebookDetailPage.js +++ b/src/page/NotebookDetailPage.js @@ -1,12 +1,46 @@ import React, {Component} from 'react'; import {StyleSheet, Text, View, ScrollView} from 'react-native'; +import {Navigation} from 'react-native-navigation'; import Color from '../style/color'; +import {Icon} from '../style/icon'; import NotebookDiaryList from '../component/notebook/notebookDiaryList'; export default class NotebookDetailPage extends Component { + constructor(props) { + super(props); + Navigation.events().bindComponent(this); + } + + static options(passProps) { + return { + topBar: { + title: { + text: '《' + passProps.notebook.subject + '》' + }, + rightButtons: [{ + id: 'navButtonNotebookSetting', + icon: Icon.navButtonNotebookSetting, + + color: Color.primary + }] + } + }; + } + + navigationButtonPressed({buttonId}) { + Navigation.push(this.props.componentId, { + component: { + name: 'NotebookEdit', + passProps: { + notebook: this.props.notebook + } + } + }); + } + render() { return ( diff --git a/src/page/NotebookEditPage.js b/src/page/NotebookEditPage.js index 7502134..d7f568c 100644 --- a/src/page/NotebookEditPage.js +++ b/src/page/NotebookEditPage.js @@ -1,5 +1,16 @@ import React, {Component} from 'react'; -import {StyleSheet, Text, View, ScrollView, Switch, TextInput, TouchableOpacity} from 'react-native'; +import { + StyleSheet, + Text, + View, + ScrollView, + Switch, + TextInput, + TouchableOpacity, + DeviceEventEmitter, + Keyboard, + Alert +} from 'react-native'; import {Navigation} from 'react-native-navigation'; import ActionSheet from 'react-native-actionsheet-api'; import ImagePicker from 'react-native-image-crop-picker' @@ -9,6 +20,7 @@ import Color from '../style/color'; import {Icon} from '../style/icon'; import Msg from '../util/msg'; import Api from '../util/api'; +import Event from "../util/event"; import Loading from '../component/loading' import DateInput from '../component/dateInput' @@ -23,11 +35,11 @@ export default class NotebookEditPage extends Component { this.state = { notebook: props.notebook, - subject: '', - isPublic: false, - dateString: '', + subject: props.notebook ? props.notebook.subject : '', + isPublic: props.notebook ? props.notebook.isPublic : false, - uploading: false + uploading: false, + saving: false } } @@ -35,7 +47,10 @@ export default class NotebookEditPage extends Component { return { topBar: { title: { - text: '创建日记本' + text: !passProps.notebook ? '创建日记本' : '修改日记本' + }, + backButton: { + title: passProps.backTitle ? passProps.backTitle : '返回' }, rightButtons: [{ id: 'save', @@ -53,9 +68,50 @@ export default class NotebookEditPage extends Component { } navigationButtonPressed({buttonId}) { - console.log('date string:', this.dateInput.getDate()); + this.saveNotebook(); } + async saveNotebook() { + let notebook = this.state.notebook; + + let subject = this.state.subject; + let isPublic = this.state.isPublic; + + try{ + this.setState({saving: true}); + + let result; + if (!notebook) { + let dateString = this.dateInput.getDate(); + result = await Api.createNotebook(subject, '', dateString, isPublic ? 10 : 1); + + } else { + result = await Api.updateNotebook(notebook.id, subject, notebook.description, isPublic ? 10 : 1); + } + + if(result) { + Keyboard.dismiss(); + + Msg.showMsg(!notebook ? '创建完成' : '保存完成'); + DeviceEventEmitter.emit(Event.updateNotebooks); + + Navigation.popToRoot(this.props.componentId); + + } else { + throw { + message: 'save notebook failed' + } + } + + } catch(e) { + console.log('e:', e); + Msg.showMsg(!notebook ? '创建日记本失败' : '修改日记本失败'); + } + + this.setState({saving: false}); + } + + async resizePhoto(uri, oWidth, oHeight) { let width = 0; let height = 0; @@ -123,24 +179,22 @@ export default class NotebookEditPage extends Component { } _onDelete() { - Navigation.popToRoot(this.props.componentId); - - /* - let notebookId = this.state.notebook ? this.state.notebook.id; + let notebookId = this.state.notebook ? this.state.notebook.id : null; if(!notebookId) { return; } Api.deleteNotebook(notebookId) .then(() => { + DeviceEventEmitter.emit(Event.updateNotebooks); + Alert.alert('提示', '日记本已删除', [{text: '好', onPress: () => { - Navigation.popToRoot(); + Navigation.popToRoot(this.props.componentId); }}]); }) .catch((err) => { Alert.alert('删除失败', err.message) }); - */ } render() { @@ -163,15 +217,21 @@ export default class NotebookEditPage extends Component { - - - {this.dateInput = r}} - style={localStyle.datePickerStyle} - customStyles={customDatePickerStyle}> - - - - + { + !this.state.notebook + ? ( + + + {this.dateInput = r}} + style={localStyle.datePickerStyle} + customStyles={customDatePickerStyle}> + + + + + ) : null + } + 公开日记本 @@ -183,24 +243,26 @@ export default class NotebookEditPage extends Component { - - - - 设置封面 - - - - - - - - 删除 - - - 提示:写过的日记本不能被删除 - + { + this.state.notebook + ? ( + + + + 设置封面 + + + + + 删除 + + + 提示:写过的日记本不能被删除 + + ) : null + } ); diff --git a/src/page/UserPage.js b/src/page/UserPage.js index 70be938..4f45460 100644 --- a/src/page/UserPage.js +++ b/src/page/UserPage.js @@ -1,5 +1,5 @@ import React, {Component} from 'react'; -import {StyleSheet, Text, View, Animated} from 'react-native'; +import {StyleSheet, Text, View, Animated, DeviceEventEmitter} from 'react-native'; import { PagerScroll, TabView, @@ -9,6 +9,7 @@ import { import {Navigation} from 'react-native-navigation'; import Api from '../util/api'; +import Event from "../util/event"; import Color from '../style/color'; import UserIntro from '../component/userIntro'; @@ -36,7 +37,15 @@ export default class UserPage extends Component { }; } - + componentDidMount(){ + this.listener = DeviceEventEmitter.addListener(Event.updateNotebooks, (param) => { + this.notebookList.refresh(); + }); + } + + componentWillUnmount(){ + this.listener.remove(); + } _renderLabel = props => ({route}) => { let routes = props.navigationState.routes; @@ -75,6 +84,7 @@ export default class UserPage extends Component { {...this.props} />, notebook: () => this.notebookList = r} user={this.user} {...this.props} /> diff --git a/src/util/api.js b/src/util/api.js index 56b7886..0ed8b4f 100644 --- a/src/util/api.js +++ b/src/util/api.js @@ -132,7 +132,28 @@ async function updateNotebookCover(bookId, photoUri) { name: 'image.jpg', type: 'image/jpg' } - }) + }); +} + +async function createNotebook(subject, description, expired, privacy) { + return call('POST', '/notebooks', { + subject: subject, + description: description, + expired: expired, + privacy: privacy + }); +} + +async function updateNotebook(id, subject, description, privacy) { + return call('PUT', '/notebooks/' + id, { + subject: subject, + description: description, + privacy: privacy + }); +} + +async function deleteNotebook(id) { + return call('DELETE', '/notebooks/' + id) } @@ -272,5 +293,8 @@ export default { getMessagesHistory, - updateNotebookCover + updateNotebookCover, + createNotebook, + updateNotebook, + deleteNotebook } \ No newline at end of file diff --git a/src/util/event.js b/src/util/event.js new file mode 100644 index 0000000..3eaf36c --- /dev/null +++ b/src/util/event.js @@ -0,0 +1,4 @@ +export default { + updateNotebooks: 'updateNotebooks' + +} \ No newline at end of file