-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
250 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
module.exports = { | ||
hasMore: true, | ||
data: [ | ||
{ | ||
img: 'http:https://images2015.cnblogs.com/blog/138012/201610/138012-20161022145742279-606202974.jpg', | ||
title: '河束人家', | ||
subTitle: '南锣鼓巷店', | ||
price: '150', | ||
distance: '120m', | ||
mumber: '389', | ||
id: Math.random().toString().slice(2) | ||
}, | ||
{ | ||
img: 'http:https://images2015.cnblogs.com/blog/138012/201610/138012-20161022145750123-1745839503.jpg', | ||
title: '漫漫火锅', | ||
subTitle: '[王府井]自助火锅', | ||
price: '113', | ||
distance: '140m', | ||
mumber: '689', | ||
id: Math.random().toString().slice(2) | ||
}, | ||
{ | ||
img: 'http:https://images2015.cnblogs.com/blog/138012/201610/138012-20161022145755545-1770557408.jpg', | ||
title: '北方涮肉', | ||
subTitle: '什刹海店', | ||
price: '92', | ||
distance: '160', | ||
mumber: '106', | ||
id: Math.random().toString().slice(2) | ||
}, | ||
{ | ||
img: 'http:https://images2015.cnblogs.com/blog/138012/201610/138012-20161022145800732-576947550.jpg', | ||
title: '姓高火锅', | ||
subTitle: '知春里店', | ||
price: '90', | ||
distance: '160', | ||
mumber: '58', | ||
id: Math.random().toString().slice(2) | ||
}, | ||
{ | ||
img: 'http:https://images2015.cnblogs.com/blog/138012/201610/138012-20161022145806201-1193851669.jpg', | ||
title: '八重牛府', | ||
subTitle: '最好吃的牛丸', | ||
price: '85', | ||
distance: '160', | ||
mumber: '1426', | ||
id: Math.random().toString().slice(2) | ||
}, | ||
{ | ||
img: 'http:https://images2015.cnblogs.com/blog/138012/201610/138012-20161022150855185-1659375763.jpg', | ||
title: '蜀乡涮锅', | ||
subTitle: '[王府井]自助火锅', | ||
price: '113', | ||
distance: '140m', | ||
mumber: '689', | ||
id: Math.random().toString().slice(2) | ||
}, | ||
{ | ||
img: 'http:https://images2015.cnblogs.com/blog/138012/201610/138012-20161022145800732-576947550.jpg', | ||
title: '满楼福火锅', | ||
subTitle: '知春路店', | ||
price: '90', | ||
distance: '160', | ||
mumber: '58', | ||
id: Math.random().toString().slice(2) | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,7 +88,6 @@ class List extends PureComponent { | |
}); | ||
|
||
}; | ||
|
||
} | ||
|
||
export default List; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import React, { PureComponent } from 'react'; | ||
import { connect } from 'react-redux'; | ||
|
||
import ListComponent from '~components/List'; | ||
import LoadMore from '~components/LoadMore'; | ||
|
||
import { getSearchData } from '~fetch/search/search'; | ||
|
||
// 初始化state | ||
|
||
const initialState = { | ||
data: [], // 存储列表数据 | ||
hasMore: false, // 记录当前状态下,是否还有更多数据,后端返回的,true即还有 | ||
isLoadingMore: false, // 记录当前状态下,是否正在加载,true即正在加载 | ||
page: 0 // 记录下一页的页码,首页是0 | ||
}; | ||
|
||
class SearchList extends PureComponent { | ||
constructor(props) { | ||
super(props); | ||
this.state = initialState; | ||
}; | ||
|
||
render() { | ||
return ( | ||
<div> | ||
{ | ||
this.state.data.length | ||
? <ListComponent data={this.state.data} /> | ||
: <div>loading...</div> | ||
} | ||
{ | ||
this.state.hasMore | ||
? <LoadMore isLoadingMore={this.state.isLoadingMore} loadMoreFn={this.loadMoreData} /> | ||
: '' | ||
} | ||
</div> | ||
); | ||
}; | ||
|
||
// 页面初次 | ||
componentDidMount() { | ||
// 获取首页数据 | ||
this.loadFirstPageData(); | ||
}; | ||
|
||
// 页面第二次,重新搜索 只走componentDidUpdate | ||
componentDidUpdate(prevProps, prevState) { | ||
const { keyword, category } = this.props; | ||
|
||
// 搜索条件完全相等时,忽略 | ||
if (keyword === prevProps.keyword && category === prevProps.category) { | ||
return; | ||
}; | ||
|
||
// 重置 state | ||
this.setState(initialState); | ||
|
||
// 重新加载数据 | ||
this.loadFirstPageData(); | ||
} | ||
|
||
// 获取首页数据 | ||
loadFirstPageData = () => { | ||
const cityName = this.props.userinfo.cityName; | ||
const { keyword, category } = this.props; | ||
const result = getSearchData(0, cityName, category, keyword); | ||
console.log(result) | ||
this.resultHandle(result); | ||
}; | ||
|
||
// 数据处理 | ||
resultHandle = result => { | ||
result.then(res => { | ||
return res.json(); | ||
}).then(json => { | ||
const hasMore = json.hasMore; | ||
const data = json.data; | ||
|
||
// 存储数据 | ||
this.setState({ | ||
hasMore: hasMore, | ||
data: this.state.data.concat(data) // 将第n+1页的数据累加到第n页上 | ||
}); | ||
}); | ||
}; | ||
|
||
// 加载更多 | ||
loadMoreData = () => { | ||
// 将状态变为加载中 | ||
this.setState({ | ||
isLoadingMore: true | ||
}); | ||
|
||
const cityName = this.props.userinfo.cityName; | ||
const page = this.state.page; | ||
const { keyword, category } = this.props; | ||
const result = getSearchData(0, cityName, category, keyword); | ||
|
||
this.resultHandle(result); | ||
|
||
// 增加page的计数 | ||
setTimeout(() =>{ | ||
this.setState({ | ||
page: page + 1, | ||
isLoadingMore: false | ||
}); | ||
}); | ||
|
||
}; | ||
} | ||
|
||
// ==========================redux react 绑定========================== | ||
|
||
const mapStateToProps = state => { | ||
return { | ||
userinfo: state.userinfo | ||
}; | ||
}; | ||
|
||
const mapDispatchToProps = dispatch => { | ||
return { | ||
}; | ||
}; | ||
|
||
|
||
|
||
export default connect( | ||
mapStateToProps, | ||
mapDispatchToProps | ||
)(SearchList); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import get from '../get'; | ||
|
||
// 搜索结果 | ||
export function getSearchData(page, cityName, category, keyword) { | ||
const keywordStr = keyword ? '/' + keyword : ''; | ||
const result = get('/api/search/' + page + '/' + encodeURIComponent(cityName) + '/' + category + keywordStr); | ||
|
||
return result; | ||
}; |