it works now

This commit is contained in:
David Ralph
2020-07-16 11:45:48 +01:00
parent 0c6022aa2d
commit 771b374a0b
3 changed files with 86 additions and 49 deletions

View File

@@ -4,7 +4,46 @@ import supportsWebP from 'supports-webp';
export default class Background extends React.Component {
doOffline() {
const photo = Math.floor(Math.random() * (20 - 1 + 1)) + 1; // There are 20 images in the offline-images folder
document.getElementById('backgroundCredits').style.display = 'none'; // Hide the location icon
let photographer; // Photographer credit
let pixabayNumbers = [2, 3, 9, 11, 13, 14, 15]; // As there are a lot of Pixabay photos, we shorten the code a bit here
if (pixabayNumbers.includes(photo)) photographer = 'Pixabay';
else switch (photo) {
default: {
photographer = 'Unknown';
break;
}
case 1: {
photographer = 'Tirachard Kumtanom';
break;
}
case 4: {
photographer = 'Sohail Na';
break;
}
case 7: {
photographer = 'Miriam Espacio';
break;
}
case 10: {
photographer = 'NO NAME';
break;
}
case 20: {
photographer = 'Fabian Wiktor';
break;
}
}
document.getElementById('photographer').innerText = `Photo by ${photographer} (Pexels)`; // Set the credit
document.getElementById('root').style.backgroundImage = `url(../offline-images/${photo}.jpeg)`; // Set the background
}
async setBackground() {
const enabled = localStorage.getItem('offlineMode');
if (enabled === 'true') return this.doOffline();
try { // First we try and get an image from the API...
let requestURL;
if (await supportsWebP) requestURL = 'https://api.muetab.xyz/getImage?webp=true';
@@ -16,39 +55,7 @@ export default class Background extends React.Component {
document.getElementById('photographer').innerText = `Photo by ${data.photographer}`; // Set the credit
document.getElementById('location').innerText = `${data.location}`; // Set the location tooltip
} catch (e) { // ..and if that fails we load one locally
const photo = Math.floor(Math.random() * (20 - 1 + 1)) + 1; // There are 20 images in the offline-images folder
document.getElementById('backgroundCredits').style.display = 'none'; // Hide the location icon
let photographer; // Photographer credit
let pixabayNumbers = [2, 3, 9, 11, 13, 14, 15]; // As there are a lot of Pixabay photos, we shorten the code a bit here
if (pixabayNumbers.includes(photo)) photographer = 'Pixabay';
else switch (photo) {
default: {
photographer = 'Unknown';
break;
}
case 1: {
photographer = 'Tirachard Kumtanom';
break;
}
case 4: {
photographer = 'Sohail Na';
break;
}
case 7: {
photographer = 'Miriam Espacio';
break;
}
case 10: {
photographer = 'NO NAME';
break;
}
case 20: {
photographer = 'Fabian Wiktor';
break;
}
}
document.getElementById('photographer').innerText = `Photo by ${photographer} (Pexels)`; // Set the credit
document.getElementById('root').style.backgroundImage = `url(../offline-images/${photo}.jpeg)`; // Set the background
this.doOffline();
}
}