Skip to content

Commit

Permalink
Refactor to reduce code
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceK33z committed Dec 14, 2016
1 parent 4d8a377 commit 5554026
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 90 deletions.
44 changes: 1 addition & 43 deletions src/imdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function modifyPlexButton(el, action, title, key) {
el.classList.add('imdb-to-plex-button--couchpotato');
el.addEventListener('click', (e) => {
e.preventDefault();
addToCouchpotato();
addToCouchpotato(config, imdbId);
});
}

Expand Down Expand Up @@ -76,48 +76,6 @@ function initPlexThingy() {
});
}

function addToCouchpotato(action) {
chrome.runtime.sendMessage({
type: 'VIEW_COUCHPOTATO',
url: `${config.couchpotatoUrl}/media.get`,
imdbId,
basicAuth: config.couchpotatoBasicAuth,
}, function(res) {
const movieExists = res.success;
if (res.err) {
showNotification('warning', 'CouchPotato request failed (look in DevTools for more info)');
console.error('Error with viewing on CouchPotato:', res.err);
return;
}
if (!movieExists) {
addToCouchPotatoRequest(imdbId);
return;
}
showNotification('info', `Movie is already in CouchPotato (status: ${res.status})`);
});
}

function addToCouchPotatoRequest(imdbId) {
chrome.runtime.sendMessage({
type: 'ADD_COUCHPOTATO',
url: `${config.couchpotatoUrl}/movie.add`,
imdbId,
basicAuth: config.couchpotatoBasicAuth,
}, function(res) {
if (res.err) {
showNotification('warning', 'Could not add to CouchPotato (look in DevTools for more info)');
console.error('Error with adding on CouchPotato:', err);
return;
}
if (res.success) {
showNotification('info', 'Added movie on CouchPotato.');
} else {
showNotification('warning', 'Could not add to CouchPotato.');
}
});
}


let config;
if (isMovie() && imdbId) {
getOptions().then((options) => {
Expand Down
48 changes: 1 addition & 47 deletions src/movieo.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function modifyPlexButton(el, action, title, key) {
el.classList.add('movieo-to-plex-button--couchpotato');
el.addEventListener('click', (e) => {
e.preventDefault();
addToCouchpotato();
addToCouchpotato(config, getImdbId());
});
}

Expand All @@ -121,49 +121,3 @@ function getImdbId() {
}
return null;
}

function addToCouchpotato(action) {
const imdbId = getImdbId();
if (!imdbId) {
console.log('Cancelled adding to CouchPotato since there is no IMDB ID');
return;
}
chrome.runtime.sendMessage({
type: 'VIEW_COUCHPOTATO',
url: `${config.couchpotatoUrl}/media.get`,
imdbId,
basicAuth: config.couchpotatoBasicAuth,
}, function(res) {
const movieExists = res.success;
if (res.err) {
showNotification('warning', 'CouchPotato request failed (look in DevTools for more info)');
console.error('Error with viewing on CouchPotato:', res.err);
return;
}
if (!movieExists) {
addToCouchPotatoRequest(imdbId);
return;
}
showNotification('info', `Movie is already in CouchPotato (status: ${res.status})`);
});
}

function addToCouchPotatoRequest(imdbId) {
chrome.runtime.sendMessage({
type: 'ADD_COUCHPOTATO',
url: `${config.couchpotatoUrl}/movie.add`,
imdbId,
basicAuth: config.couchpotatoBasicAuth,
}, function(res) {
if (res.err) {
showNotification('warning', 'Could not add to CouchPotato (look in DevTools for more info)');
console.error('Error with adding on CouchPotato:', err);
return;
}
if (res.success) {
showNotification('info', 'Added movie on CouchPotato.');
} else {
showNotification('warning', 'Could not add to CouchPotato.');
}
});
}
45 changes: 45 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,48 @@ function showNotification(state, text) {
document.body.removeChild(el);
}, 5000);
}

function addToCouchpotato(options, imdbId) {
if (!imdbId) {
console.log('Cancelled adding to CouchPotato since there is no IMDB ID');
return;
}
chrome.runtime.sendMessage({
type: 'VIEW_COUCHPOTATO',
url: `${options.couchpotatoUrl}/media.get`,
imdbId,
basicAuth: options.couchpotatoBasicAuth,
}, function(res) {
const movieExists = res.success;
if (res.err) {
showNotification('warning', 'CouchPotato request failed (look in DevTools for more info)');
console.error('Error with viewing on CouchPotato:', res.err);
return;
}
if (!movieExists) {
addToCouchPotatoRequest(imdbId);
return;
}
showNotification('info', `Movie is already in CouchPotato (status: ${res.status})`);
});
}

function addToCouchPotatoRequest(imdbId) {
chrome.runtime.sendMessage({
type: 'ADD_COUCHPOTATO',
url: `${config.couchpotatoUrl}/movie.add`,
imdbId,
basicAuth: config.couchpotatoBasicAuth,
}, function(res) {
if (res.err) {
showNotification('warning', 'Could not add to CouchPotato (look in DevTools for more info)');
console.error('Error with adding on CouchPotato:', err);
return;
}
if (res.success) {
showNotification('info', 'Added movie on CouchPotato.');
} else {
showNotification('warning', 'Could not add to CouchPotato.');
}
});
}

0 comments on commit 5554026

Please sign in to comment.