Skip to content

Commit

Permalink
Get final Unsplash URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Cheung committed Nov 8, 2015
1 parent ee6e794 commit cd7276e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
21 changes: 4 additions & 17 deletions app/scripts/components/colour/newtab.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';

import Chrome from '../../modules/chrome';
import Colours from '../../modules/colours';
import Unsplash from '../../modules/unsplash';

import Time from './time';
import Hex from './hex';
Expand Down Expand Up @@ -50,23 +51,9 @@ class NewTab extends React.Component {
// Background images/opacity
if (settings.bg !== 'none') {
if (settings.bg === 'unsplash') {
let unsplashBgUrl = 'https://source.unsplash.com/';

switch (settings.bgUnsplashFreq) {
case 'perSession':
unsplashBgUrl += 'random';
break;

case 'daily':
unsplashBgUrl += 'daily';
break;

case 'weekly':
unsplashBgUrl += 'weekly';
break;
}

this.loadBgImage(unsplashBgUrl);
Unsplash.getImage(settings.bgUnsplashFreq, (url) => {
this.loadBgImage(url);
});
}

if (settings.bg === 'custom' && settings.bgCustomUrl !== '') {
Expand Down
32 changes: 32 additions & 0 deletions app/scripts/modules/unsplash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Unsplash {
static getImage (frequency, callback) {
// Set proper frequency in URL
let unsplashUrl = 'https://source.unsplash.com/';

switch (frequency) {
case 'daily':
unsplashUrl += 'daily';
break;

case 'weekly':
unsplashUrl += 'weekly';
break;

default: // perSession
unsplashUrl += 'random';
break;
}

// Follow redirect to get actual image URL
let req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (req.readyState === 4 && req.status === 200) {
callback(req.responseURL);
}
};
req.open('GET', unsplashUrl, true);
req.send(null);
}
}

export default Unsplash;

0 comments on commit cd7276e

Please sign in to comment.