Skip to content

Commit

Permalink
98 - Handling Pagination in React
Browse files Browse the repository at this point in the history
  • Loading branch information
CFE committed Jan 13, 2020
1 parent 2a6f010 commit 0c39d74
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 18 additions & 1 deletion tweetme2-web/src/tweets/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,31 @@ export function TweetsList(props) {
updateFinalTweets.unshift(tweets)
setTweets(updateFinalTweets)
}
const handleLoadNext = (event) => {
event.preventDefault()
if (nextUrl !== null) {
const handleLoadNextResponse = (response, status) =>{
if (status === 200){
setNextUrl(response.next)
const newTweets = [...tweets].concat(response.results)
setTweetsInit(newTweets)
setTweets(newTweets)
} else {
alert("There was an error")
}
}
apiTweetList(props.username, handleLoadNextResponse, nextUrl)
}
}

return <React.Fragment>{tweets.map((item, index)=>{
return <Tweet
tweet={item}
didRetweet={handleDidRetweet}
className='my-5 py-5 border bg-white text-dark'
key={`${index}-{item.id}`} />
})}
{ nextUrl !== null && <button className='btn btn-outline-primary'>Load next</button>}
{nextUrl !== null && <button onClick={handleLoadNext} className='btn btn-outline-primary'>Load next</button>}
</React.Fragment>
}

Expand Down
5 changes: 4 additions & 1 deletion tweetme2-web/src/tweets/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ export function apiTweetDetail(tweetId, callback) {
}


export function apiTweetList(username, callback) {
export function apiTweetList(username, callback, nextUrl) {
let endpoint = "/tweets/"
if (username){
endpoint = `/tweets/?username=${username}`
}
if (nextUrl !== null && nextUrl !== undefined) {
endpoint = nextUrl.replace("http://localhost:8000/api", "")
}
backendLookup("GET", endpoint, callback)
}

0 comments on commit 0c39d74

Please sign in to comment.