diff --git a/src/GoalsForm.js b/src/GoalsForm.js index a87ec74..cdfcb64 100644 --- a/src/GoalsForm.js +++ b/src/GoalsForm.js @@ -28,21 +28,25 @@ class GoalsForm extends Component { issueNumber: 1, errors: [], success: false, + prevRepoSelectorPageLink: null, + nextRepoSelectorPageLink: null }; } componentDidMount() { - this.getRepos() + this.getRepos(); + } + + getRepos(url = null) { + gitHubApiCommunicator.getRepos(this.props.token, url) .then(repoData => this.setRepos(repoData)) .then(repo => this.getMilestonesAndIssues(repo)) .then(response => this.setMilestoneAndIssueNumbers(response)) } - getRepos() { - return gitHubApiCommunicator.getRepos(this.props.token); - } - setRepos(repoData) { + this.setPagination(repoData); + const repos = repoData.data.map((repo) => { return { name: repo.name, @@ -57,6 +61,22 @@ class GoalsForm extends Component { return currentRepo; } + setPagination(repoData) { + const links = repoData.headers.link.split(",") + const result = {prev: null, next: null} + + links.map((link) => { + const key = JSON.parse(link.match(/".*"/g)[0]) + const value = link.match(/[^\s<>]+/)[0] + result[key] = value + }); + + this.setState({ + prevRepoSelectorPageLink: result.prev, + nextRepoSelectorPageLink: result.next + }); + } + getMilestonesAndIssues(repo) { return gitHubApiCommunicator.getMilestonesAndIssues( this.props.token, @@ -106,10 +126,30 @@ class GoalsForm extends Component { handleChangeRepo = (event) => { this.setState({ - currentRepo: this.state.repos[parseInt(event.target.value)] + currentRepo: this.state.repos[event.target.getAttribute("value")] }, this.updateMilestoneAndIssue) } + atBeginningOfRepoSelector = () => { + return this.state.prevRepoSelectorPageLink == null + } + + atEndOfRepoSelector = () => { + return this.state.nextRepoSelectorPageLink == null + } + + pageRepoSelectorNext = () => { + if (this.state.nextRepoSelectorPageLink) { + this.getRepos(this.state.nextRepoSelectorPageLink); + } + } + + pageRepoSelectorPrev = () => { + if (this.state.prevRepoSelectorPageLink) { + this.getRepos(this.state.prevRepoSelectorPageLink); + } + } + updateMilestoneAndIssue = () => { this.getMilestonesAndIssues(this.state.currentRepo) .then(response => this.setMilestoneAndIssueNumbers(response)) @@ -201,7 +241,12 @@ class GoalsForm extends Component {