Skip to content

Commit

Permalink
axios 5-18
Browse files Browse the repository at this point in the history
  • Loading branch information
dlyt committed May 18, 2017
1 parent 08048e4 commit e5dbc00
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 124 deletions.
27 changes: 27 additions & 0 deletions app/actions/authActions.js
@@ -0,0 +1,27 @@
// import axios from 'axios'
// import * as types from './types'
// import { AsyncStorage } from 'react-native'
// import DeviceInfo from 'react-native-device-info'
// import setAuthorizationToken from '../lib/setAuthorizationToken'

// export function getToken() {
// const uuid = DeviceInfo.getUniqueID()
// const json = { user: { uuid: uuid } }
// return dispatch => {
// AsyncStorage.getItem(`userToken`).then(
// (res) => {
// if (!res) {
// axios.post('/users/tourists', json)
// .then((res) => {
// setAuthorizationToken(res.token)
// AsyncStorage.setItem(`userToken`, res.token)
// })
// }
// else{
// setAuthorizationToken(res)
// }
// }
// )
// }
// }

58 changes: 36 additions & 22 deletions app/actions/bookshelves.js
@@ -1,27 +1,42 @@
import axios from 'axios'
import * as types from './types'
import { AsyncStorage } from 'react-native'
import setAuthorizationToken from '../lib/setAuthorizationToken'

export function getBookshelf(uuid) {
export function getBookshelfFirst() {
return (dispatch, getState) => {
const json = { user: {uuid: uuid}}
return AsyncStorage.getItem(`userToken`)
.then((data) => {
if (!data) {
AsyncStorage.getItem(`userToken`).then(
(res) => {
if (!res) {
axios.post('/users/tourists', json)
.then((_data) => {
console.log(_data)
AsyncStorage.setItem(`userToken`, _data.token)
dispatch(setSearchedBookshelves({bookshelf: ''}));
.then((res) => {
setAuthorizationToken(res.token)
AsyncStorage.setItem(`userToken`, res.token)
})
}
else {
axios.get('/bookshelfs', '', data)
.then((_data) => {
dispatch(setSearchedBookshelves({bookshelf: _data.list}));
})
else{
setAuthorizationToken(res)
axios.get('/bookshelfs')
.then(
(res) => {
dispatch(setSearchedBookshelves({ bookshelf: res.data.list }));
},
(err) => console.log(err)
)
}
})
}
)
}
}

export function getBookshelf() {
return (dispatch) => {
axios.get('/bookshelfs').then(
(res) => {
dispatch(setSearchedBookshelves({ bookshelf: res.data.list }));
},
(err) => console.log(err)
)
}
}

Expand All @@ -34,17 +49,16 @@ export function setSearchedBookshelves({ bookshelf }) {

export function orderNovel(id) {
return (dispatch, getState) => {
// AsyncStorage.getItem('userToken')
// .then((data) => {
// console.log(data)
// })
Request.post('/bookshelfs/order', {id: id}, 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjU4Yzk0OTk3ZDE4ZTIwMjRiNjYzNjBmYiIsImlhdCI6MTQ4OTgxMzYwNH0.kgCcISiRUTuagiaQMYO3cQ-R2sTo2z0X6HCpgSEkZq4')
return axios.post('/bookshelfs/order', {id: id})
}
}

export function delect(id) {
return (dispatch, getState) => {
Request.post('/bookshelfs/delect', {id: id})
.then((data) => {})
return axios.post('/bookshelfs/delect', {id: id})
}
}




9 changes: 6 additions & 3 deletions app/actions/index.js
Expand Up @@ -2,10 +2,13 @@ import * as BookshelfActions from './bookshelves'
import * as NavigationActions from './navigation'
import * as ReaderActions from './reader'
import * as SearchActions from './search'
import * as AuthActions from './authActions'

export const ActionCreators = Object.assign({},
BookshelfActions,
NavigationActions,
AuthActions,
ReaderActions,
SearchActions,
);
BookshelfActions,
NavigationActions,
)

45 changes: 19 additions & 26 deletions app/actions/reader.js
@@ -1,34 +1,27 @@
import { AsyncStorage } from 'react-native';
import { AsyncStorage } from 'react-native'
import * as types from './types'
import Request from '../lib/request'
import axios from 'axios'

export function getFirstRenderChapters(id) {
return (dispatch, getState) => {
AsyncStorage.getItem('userToken')
.then((token) => {
Request.get(`/chapters/firstRender/${id}`, '', token)
.then((res) => {
dispatch({
type: types.GET_FIRST_RENDER_CHAPTER,
firstRenderChapters: res.response
});
axios.get(`/chapters/firstRender/${id}`).then(
(res) => {
dispatch({
type: types.GET_FIRST_RENDER_CHAPTER,
firstRenderChapters: res.data.response
})
})
.catch( (e) => {
console.log(e);
})
}
)
}
}



export function getChapter(id, num) {
const json = {
novelId: id,
num: num
}
return (dispatch, getState) => {
return Request.post(`/chapters`, json)
return axios.post(`/chapters`, json)
.then((data) => {
dispatch({
type: types.SET_CHAPTER_DETAIL,
Expand All @@ -44,9 +37,9 @@ export function setChapterDetail({ chapterContent }) {

export function getNextChatperDetail(id) {
return (dispatch, getState) => {
return Request.get(`/chapters/next/${id}`, '')
.then( (data) => {
dispatch(setNextChatperDetail({chapterContent: data.detail}));
return axios.get(`/chapters/next/${id}`)
.then( (res) => {
dispatch(setNextChatperDetail({chapterContent: res.data.detail}));
})
}
}
Expand All @@ -60,9 +53,9 @@ export function setNextChatperDetail({ chapterContent }) {

export function getLastChapterDetail(id) {
return (dispatch, getState) => {
return Request.get(`/chapters/last/${id}`, '')
.then( (data) => {
dispatch(setLastChatperDetail({chapterContent: data.detail}));
return axios.get(`/chapters/last/${id}`)
.then( (res) => {
dispatch(setLastChatperDetail({chapterContent: res.data.detail}));
})
}
}
Expand All @@ -76,9 +69,9 @@ export function setLastChatperDetail({ chapterContent }) {

export function getDirectory(id, order = 1) {
return (dispatch, getState) => {
Request.get(`/novels/directory/${id}`, {order: order})
.then( (data) => {
dispatch(setDirectory({results: data.results}));
axios.get(`/novels/directory/${id}?order=${order}`)
.then( (res) => {
dispatch(setDirectory({results: res.data.results}));
})
}
}
Expand Down
34 changes: 13 additions & 21 deletions app/actions/search.js
@@ -1,14 +1,13 @@
import { AsyncStorage } from 'react-native'
import * as types from './types'
import Request from '../lib/request'

import axios from 'axios'

export function searchNovelWords(text) {
return (dispatch, getState) => {
Request.get('/novels/search/zh', {keyword: text})
.then((data) => {
dispatch(setSearchNovelWords({novelName: data.response.r}))
})
axios.get(`/novels/search/zh?keyword=${text}`)
.then(
(res) => dispatch(setSearchNovelWords({novelName: res.data.response.r}))
)
}
}

Expand All @@ -21,10 +20,10 @@ export function setSearchNovelWords({ novelName }) {

export function searchNovelList(name) {
return (dispatch, getState) => {
Request.get('/novels/search/bqk', {name: name})
.then((data) => {
dispatch(setSearchNovelList({name: data.response}))
})
axios.get(`/novels/search/bqk?name=${name}`)
.then(
(res) => dispatch(setSearchNovelList({name: res.data.response}))
)
}
}

Expand All @@ -43,16 +42,9 @@ export function searchNovelInfo(name, url) {
}
}
return (dispatch, getState) => {
AsyncStorage.getItem('userToken')
.then((data) => {
Request.post('/novels/acquire', json, data)
.then((data) => {
dispatch(setSearchNovelInfo({novelInfo: data.novelInfo}))
})
.catch( (e) => {
console.log(e);
})
})
axios.post('/novels/acquire', json).then(
(res) => dispatch(setSearchNovelInfo({novelInfo: res.data.novelInfo}))
)
}
}

Expand All @@ -65,7 +57,7 @@ export function setSearchNovelInfo({ novelInfo }) {

export function searchImg(type) {
return (dispatch, getState) => {
Request.get('/test/img', {type: type})
axios.get('/test/img', {type: type})
.then((data) => {
dispatch(getImg({imgUrl: data.url}))
})
Expand Down
11 changes: 3 additions & 8 deletions app/containers/About.js
@@ -1,5 +1,5 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import React, { Component } from 'react'
import { connect } from 'react-redux'
import {
PixelRatio,
ScrollView,
Expand All @@ -13,12 +13,7 @@ import {
StyleSheet,
RefreshControl,
} from 'react-native';
import { appStyle } from '../styles';

import DeviceInfo from 'react-native-device-info'
import Swipeout from 'react-native-swipeout'


import { appStyle } from '../styles'

class About extends Component {
constructor(props) {
Expand Down
9 changes: 5 additions & 4 deletions app/containers/ApplicationTabs/index.ios.js
Expand Up @@ -5,6 +5,7 @@ import React, { Component } from 'react'
import { bindActionCreators } from 'redux'
import { ActionCreators } from '../../actions'
import { View, TabBarIOS, TabBarItemIOS } from 'react-native'
import { AsyncStorage } from 'react-native'

class ApplicationTabs extends Component {

Expand All @@ -14,7 +15,7 @@ class ApplicationTabs extends Component {
}

componentWillMount() {
this.props.getBookshelf(DeviceInfo.getUniqueID())

}

onPress(index) {
Expand All @@ -26,7 +27,7 @@ class ApplicationTabs extends Component {
<View style={ { flex: 1 } }>
{ React.createElement(component, this.props) }
</View>
);
)
}

render() {
Expand Down Expand Up @@ -61,7 +62,7 @@ function mapStateToProps(state) {
}

function mapDispatchToProps(dispatch) {
return bindActionCreators(ActionCreators, dispatch);
return bindActionCreators(ActionCreators, dispatch)
}

export default connect(mapStateToProps, mapDispatchToProps)(ApplicationTabs);
export default connect(mapStateToProps, mapDispatchToProps)(ApplicationTabs)
13 changes: 8 additions & 5 deletions app/containers/Detail.js
Expand Up @@ -28,11 +28,14 @@ class Detail extends Component{

orderNovel(id, join) {
if (!join) {
this.props.orderNovel(id).then(
(res) => this.props.getBookshelf(DeviceInfo.getUniqueID()),
(err) => { console.log(err) }
)
this.props.navigateReset([{key: 'ApplicationTabs'}], 0)
this.props.orderNovel(id)
.then(
(res) => {
this.props.getBookshelf()
this.props.navigateReset([{key: 'ApplicationTabs'}], 0)
},
(err) => { console.log(err) }
)
}
}

Expand Down
3 changes: 0 additions & 3 deletions app/containers/Directory.js
Expand Up @@ -49,9 +49,6 @@ class Directory extends Component{
AsyncStorage.getItem('userToken')
.then((token) => {
Request.post(`/bookshelfs/change`, json, token)
.then((res) => {
return true
})
})
.catch( (e) => {
console.log(e);
Expand Down

0 comments on commit e5dbc00

Please sign in to comment.