This commit is contained in:
techgaun 2019-04-09 23:13:48 -05:00
parent df86f8e92c
commit ccea7c3df7
No known key found for this signature in database
GPG key ID: 8BB8C93B2167505A
2 changed files with 112 additions and 92 deletions

5
.prettierrc Normal file
View file

@ -0,0 +1,5 @@
{
"trailingComma": "es5",
"singleQuote": true,
"semi": true
}

View file

@ -1,5 +1,5 @@
window.addEventListener('load', () => {
initDT() // Initialize the DatatTable and window.columnNames variables
initDT(); // Initialize the DatatTable and window.columnNames variables
const repo = getRepoFromUrl();
@ -9,7 +9,7 @@ window.addEventListener('load', () => {
}
});
document.getElementById('form').addEventListener('submit', (e) => {
document.getElementById('form').addEventListener('submit', e => {
e.preventDefault();
fetchData();
});
@ -27,24 +27,31 @@ function fetchData() {
if (re.test(repo)) {
fetchAndShow(repo);
} else {
showMsg('Invalid GitHub repository! Format is <username>/<repo>', 'danger');
showMsg(
'Invalid GitHub repository! Format is <username>/<repo>',
'danger'
);
}
}
function updateDT( data ) {
function updateDT(data) {
// Remove any alerts, if any:
if ( $( '.alert' ) )
$( '.alert' ).remove()
if ($('.alert')) $('.alert').remove();
// Format dataset and redraw DataTable. Use second index for key name
const forks = []
for ( let fork of data ) {
fork.repoLink = `<a href="https://github.com/${fork.full_name}">Link</a>`
fork.ownerName = fork.owner.login
forks.push( fork )
const forks = [];
for (let fork of data) {
fork.repoLink = `<a href="https://github.com/${fork.full_name}">Link</a>`;
fork.ownerName = fork.owner.login;
forks.push(fork);
}
const dataSet = forks.map( fork => window.columnNamesMap.map( colNM => fork[colNM[1]] ) )
window.forkTable.clear().rows.add( dataSet ).draw()
const dataSet = forks.map(fork =>
window.columnNamesMap.map(colNM => fork[colNM[1]])
);
window.forkTable
.clear()
.rows.add(dataSet)
.draw();
}
function initDT() {
@ -60,51 +67,59 @@ function initDT() {
['Open Issues', 'open_issues_count'],
['Size', 'size'],
['Last Push', 'pushed_at'],
]
];
// Sort by stars:
const sortColName = 'Stars'
const sortColumnIdx = window.columnNamesMap.map( pair => pair[0] ).indexOf( sortColName )
const sortColName = 'Stars';
const sortColumnIdx = window.columnNamesMap
.map(pair => pair[0])
.indexOf(sortColName);
// Use first index for readable column name
// we use moment's fromNow() if we are rendering for `pushed_at`; better solution welcome
window.forkTable = $( '#forkTable' ).DataTable( {
columns: window.columnNamesMap.map( colNM => {
window.forkTable = $('#forkTable').DataTable({
columns: window.columnNamesMap.map(colNM => {
return {
title: colNM[0],
render: colNM[1] === 'pushed_at' ? (data, type, _row) => {
render:
colNM[1] === 'pushed_at'
? (data, type, _row) => {
if (type === 'display') {
return moment(data).fromNow()
return moment(data).fromNow();
}
return data
} : null
return data;
}
: null,
};
}),
'order': [[sortColumnIdx, 'desc']],
} )
order: [[sortColumnIdx, 'desc']],
});
}
function fetchAndShow( repo ) {
function fetchAndShow(repo) {
repo = repo.replace('https://github.com/', '');
repo = repo.replace('http://github.com/', '');
repo = repo.replace('.git', '');
fetch( `https://api.github.com/repos/${repo}/forks?sort=stargazers&per_page=100` )
.then( ( response ) => {
if ( !response.ok )
throw Error( response.statusText )
return response.json()
} )
.then( ( data ) => {
console.log( data )
updateDT( data )
} )
.catch( ( error ) => {
const msg = error.toString().indexOf( 'Forbidden' ) >= 0 ? 'Error: API Rate Limit Exceeded' : error
showMsg( `${msg}. Additional info in console`, 'danger' )
console.error( error )
} )
fetch(
`https://api.github.com/repos/${repo}/forks?sort=stargazers&per_page=100`
)
.then(response => {
if (!response.ok) throw Error(response.statusText);
return response.json();
})
.then(data => {
console.log(data);
updateDT(data);
})
.catch(error => {
const msg =
error.toString().indexOf('Forbidden') >= 0
? 'Error: API Rate Limit Exceeded'
: error;
showMsg(`${msg}. Additional info in console`, 'danger');
console.error(error);
});
}
function showMsg(msg, type) {