Skip to content

Commit

Permalink
Implement auto-refresh of the project statuses (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrej-merkun authored and Haroon Sheikh committed Oct 28, 2019
1 parent e08dd5e commit d5b4096
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
16 changes: 16 additions & 0 deletions src/components/ProjectDetails/ProjectDetails.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
.ProjectDetails {
position: relative;
}

.ProjectDetails .autoRefreshButton {
position: absolute;
top: 0;
right: 0;
color: white;
outline: none;
border: none;
background-color: #0D65CA;
padding: 5px;
}


table {
font-family: arial, sans-serif;
font-size: 0.8rem;
Expand Down
53 changes: 44 additions & 9 deletions src/components/ProjectDetails/ProjectDetails.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,66 @@
import React, { Component } from 'react';
import "./ProjectDetails.css";
var axios = require('axios');

const params = {};

if (process.env.REACT_APP_GITHUB_TOKEN) {
params.headers = {
Authorization: process.env.REACT_APP_GITHUB_TOKEN,
};
}

export default class ProjectDetails extends Component {
state={
commitDetails:null
commitDetails:null,
shouldRefresh: false,
}
componentDidMount=()=>{
const params = {};
if (process.env.REACT_APP_GITHUB_TOKEN) {
params.headers = {
Authorization: process.env.REACT_APP_GITHUB_TOKEN,
};
}

axios.get(
`https://api.github.com/repos/${this.props.name}/commits`,
params
).then(data=>this.setState({commitDetails:data.data}))
}

componentWillUnmount () {
clearInterval(this.interval);
}

toggleAutoRefresh (e) {
e.stopPropagation(); // Prevent the click event from being fired on the parent elements as well

this.setState({
shouldRefresh: !this.state.shouldRefresh,
}, () => {

clearInterval(this.interval);

if (this.state.shouldRefresh) {
this.interval = setInterval(() => {
axios.get(
`https://api.github.com/repos/${this.props.name}/commits`,
params
)
.then(data => this.setState({ commitDetails:data.data }))
}, 30000); // Refresh every 30 seconds
}

})
}

render() {
if(!this.state.commitDetails){
return(
<div><h5>Loading details ...</h5></div>
)
}
return (
<div>
<div className='ProjectDetails'>

<button onClick={(e) => this.toggleAutoRefresh(e)} className='autoRefreshButton'>
Toggle Auto Refresh {this.state.shouldRefresh ? 'OFF' : 'ON'}
</button>

Commit Details
<table>
<thead>
Expand Down

0 comments on commit d5b4096

Please sign in to comment.