-
Notifications
You must be signed in to change notification settings - Fork 783
feat: Paged events screen #736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b204895
feat: implement pagination in events screen
machour 282aa3d
refactor: remove old code that used to handle this
machour 504fb9d
chore: get rid of lodash.uniqby, already included in lodash
machour 7a484ab
refactor: simplify code and add TODO item
machour 42c5d6f
refactor: rename Client.fetch() to Client.call() to avoid confusions
machour 73b6609
Merge remote-tracking branch 'upstream/master' into paged-events-screen
machour File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,5 @@ | ||
import { createPaginationActionSet } from 'utils'; | ||
|
||
export const ACTIVITY_GET_EVENTS_RECEIVED = createPaginationActionSet( | ||
'ACTIVITY_GET_EVENTS_RECEIVED' | ||
); |
This file contains hidden or 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,145 @@ | ||
import Schemas from './schemas'; | ||
|
||
export class Client { | ||
API_ROOT = 'https://api.github.com/'; | ||
|
||
/** | ||
* Enum for HTTP methods. | ||
* | ||
* @enum {string} | ||
*/ | ||
Method = { | ||
GET: 'GET', | ||
HEAD: 'HEAD', | ||
PUT: 'PUT', | ||
DELETE: 'DELETE', | ||
PATCH: 'PATCH', | ||
POST: 'POST', | ||
}; | ||
|
||
authHeaders = {}; | ||
|
||
call = async ( | ||
url, | ||
params = {}, | ||
{ method = this.Method.GET, body = {}, headers = {} } = {} | ||
) => { | ||
let finalUrl; | ||
|
||
if (params.url) { | ||
// a different url was provided, use it instead (paginated) | ||
finalUrl = params.url; | ||
} else { | ||
finalUrl = url; | ||
// add explicitely specified parameters | ||
if (params.per_page) { | ||
finalUrl = `${finalUrl}${finalUrl.includes('?') ? '&' : '?'}per_page=${ | ||
params.per_page | ||
}`; | ||
} | ||
} | ||
|
||
if (!finalUrl.includes(this.API_ROOT)) { | ||
finalUrl = `${this.API_ROOT}${finalUrl}`; | ||
} | ||
|
||
const parameters = { | ||
method, | ||
headers: { | ||
'Cache-Control': 'no-cache', | ||
...this.authHeaders, | ||
...headers, | ||
}, | ||
}; | ||
|
||
const withBody = [this.Method.PUT, this.Method.PATCH, this.Method.POST]; | ||
|
||
if (withBody.indexOf(method) !== -1) { | ||
parameters.body = JSON.stringify(body); | ||
if (method === this.Method.PUT) { | ||
parameters.headers['Content-Length'] = 0; | ||
} | ||
} | ||
|
||
return fetch(finalUrl, parameters) | ||
.then(response => response) | ||
.catch(error => error); | ||
}; | ||
|
||
/* eslint-disable no-unused-vars */ | ||
|
||
/** | ||
* Sets the authorization headers given an access token. | ||
* | ||
* @abstract | ||
* @param {string} token The oAuth access token | ||
*/ | ||
setAuthHeaders = token => { | ||
this.authHeaders = { Authorization: `token ${token}` }; | ||
}; | ||
|
||
/** | ||
* Counts the entities available by analysing the Response object | ||
* | ||
* @async | ||
* @param {Response} response | ||
*/ | ||
getCount = async response => { | ||
if (!response.ok) { | ||
return 0; | ||
} | ||
|
||
let linkHeader = response.headers.get('Link'); | ||
|
||
if (linkHeader !== null) { | ||
linkHeader = linkHeader.match(/page=(\d)+/g).pop(); | ||
|
||
return linkHeader.split('=').pop(); | ||
} | ||
|
||
return response | ||
.json() | ||
.then(data => data.length) | ||
.catch(() => { | ||
// TODO: Properly handle the error and show it if needed. | ||
return 0; | ||
}); | ||
}; | ||
|
||
getNextPageUrl = response => { | ||
const { headers } = response; | ||
const link = headers.get('link'); | ||
const nextLink = link | ||
? link.split(',').find(s => s.indexOf('rel="next"') > -1) | ||
: null; | ||
|
||
if (!nextLink) { | ||
return null; | ||
} | ||
|
||
return nextLink | ||
.trim() | ||
.split(';')[0] | ||
.slice(1, -1); | ||
}; | ||
|
||
/** | ||
* The activity endpoint | ||
*/ | ||
activity = { | ||
/** | ||
* Gets received events | ||
* | ||
* @param {string} userId | ||
*/ | ||
getEventsReceived: async (userId, params) => { | ||
return this.call(`users/${userId}/received_events`, params).then( | ||
response => ({ | ||
response, | ||
nextPageUrl: this.getNextPageUrl(response), | ||
schema: Schemas.EVENT_ARRAY, | ||
}) | ||
); | ||
}, | ||
}; | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can simplify this method to something like:
Not picky however 😁 The way it currently is is also fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!