refactor and cleanup modals slightly

This commit is contained in:
David Ralph
2020-10-15 15:23:01 +01:00
parent edd8872403
commit b70bd85370
6 changed files with 117 additions and 133 deletions

View File

@@ -0,0 +1,177 @@
import React from 'react';
import LocalMallIcon from '@material-ui/icons/LocalMall';
import { toast } from 'react-toastify';
import Item from '../marketplace/Item';
import MarketplaceFunctions from '../../../modules/marketplaceFunctions';
export default class Addons extends React.PureComponent {
constructor(...args) {
super(...args);
this.state = {
installed: [],
current_data: {
type: '',
name: '',
content: {}
},
item_data: {
name: 'Name',
author: 'Author',
description: 'Description',
updated: '???',
version: '1.0.0',
icon: ''
},
button: <button className='removeFromMue' onClick={() => this.uninstall()}>{this.props.marketplaceLanguage.product.buttons.remove}</button>
}
}
toggle(type, type2, data) {
if (type === 'item') {
let installed = JSON.parse(localStorage.getItem('installed'));
let info = installed.find(i => i.name === data).content;
this.setState({
current_data: { type: type2, name: data, content: info },
item_data: {
name: info.data.name,
author: info.data.author,
description: MarketplaceFunctions.urlParser(info.data.description.replace(/\n/g, '<br>')),
updated: info.updated,
version: info.data.version,
icon: info.data.screenshot_url
}
});
document.getElementById('item').style.display = 'block';
document.getElementById('marketplace').style.display = 'none';
} else {
this.setState({
button: <button className='removeFromMue' onClick={() => this.uninstall()}>{this.props.marketplaceLanguage.product.buttons.remove}</button>
});
document.getElementById('marketplace').style.display = 'block';
document.getElementById('item').style.display = 'none';
}
}
uninstall() {
let type, data = this.state.current_data.type;
if (data === undefined) data = this.state.current_data.content.data.type;
switch (data) {
case 'photos':
type = 'photo_packs';
break;
case 'quotes':
type = 'quote_packs';
break;
default:
type = this.state.current_data.type;
break;
}
MarketplaceFunctions.uninstall(this.state.current_data.name, type);
toast(this.props.toastLanguage.removed);
this.setState({
button: '',
installed: JSON.parse(localStorage.getItem('installed'))
});
}
install(input) {
let installed = JSON.parse(localStorage.getItem('installed'));
let button;
const installStuff = () => {
installed.push({
content: {
updated: 'Unpublished',
data: input
}
});
localStorage.setItem('installed', JSON.stringify(installed));
toast(this.props.toastLanguage.installed);
button = <button className='removeFromMue' onClick={() => this.uninstall()}>{this.props.marketplaceLanguage.product.buttons.remove}</button>;
this.setState({
button: button,
installed: JSON.parse(localStorage.getItem('installed'))
});
}
switch (input.type) {
case 'settings':
localStorage.removeItem('backup_settings');
let oldSettings = [];
for (const key of Object.keys(localStorage)) oldSettings.push({name: key, value: localStorage.getItem(key)});
localStorage.setItem('backup_settings', JSON.stringify(oldSettings));
input.settings.forEach(element => localStorage.setItem(element.name, element.value));
installStuff();
break;
case 'photos':
localStorage.setItem('photo_packs', JSON.stringify(input.photos));
installStuff();
break;
case 'theme':
localStorage.setItem('theme', input.theme);
installStuff();
break;
case 'quote_packs':
if (input.quote_api) localStorage.setItem('quote_api', JSON.stringify(input.quote_api));
localStorage.setItem('quote_packs', JSON.stringify(input.quotes));
installStuff();
break;
default:
break;
}
}
componentDidMount() {
document.getElementById('file-input').onchange = (e) => {
const file = e.target.files[0];
if (file.type !== 'application/json') return console.error(`expected json, got ${file.type}`);
const reader = new FileReader();
reader.readAsText(file, 'UTF-8');
reader.onload = (readerEvent) => {
const content = JSON.parse(readerEvent.target.result);
this.install(content);
};
};
this.setState({ installed: JSON.parse(localStorage.getItem('installed')) });
}
render() {
let content = <div className='items'>
{this.state.installed.map((item) =>
<div className='item' onClick={() => this.toggle('item', item.type, item.name)}>
<img alt='icon' src={item.content.data.icon_url} />
<div className='details'>
<h4>{item.content.data.name}</h4>
<p>{item.content.data.author}</p>
</div>
</div>)}
</div>;
if (this.state.installed.length === 0) {
content = <div className='items'>
<div className='emptyMessage'>
<LocalMallIcon />
<h1>{this.props.language.empty.title}</h1>
<p className='description'>{this.props.language.empty.description}</p>
<button className='goToMarket' onClick={this.props.openMarketplace}>{this.props.language.empty.button}</button>
</div>
</div>;
}
return (
<div>
<div id='marketplace'>
<input id='file-input' type='file' name='name' className='hidden' />
<button className='addToMue sideload' onClick={() => document.getElementById('file-input').click()}>{this.props.language.sideload}</button>
<h1>{this.props.language.added}</h1>
{content}
</div>
<Item button={this.state.button} data={this.state.item_data} function={() => this.toggle()} language={this.props.marketplaceLanguage.product} />
</div>
);
}
}

View File

@@ -0,0 +1,221 @@
import React from 'react';
import WifiOffIcon from '@material-ui/icons/WifiOff';
import ArrowBackIcon from '@material-ui/icons/ArrowBack';
import { toast } from 'react-toastify';
import Item from '../marketplace/Item';
import MarketplaceFunctions from '../../../modules/marketplaceFunctions';
import * as Constants from '../../../modules/constants';
import Items from '../marketplace/Items';
export default class Marketplace extends React.PureComponent {
constructor(...args) {
super(...args);
this.state = {
themes: [],
settings: [],
photo_packs: [],
quote_packs: [],
see_more: [],
see_more_type: '',
current_data: {
type: '',
name: '',
content: {}
},
button: '',
featured: {},
done: false,
item_data: {
name: 'Name',
author: 'Author',
description: 'Description',
updated: '???',
version: '1.0.0',
icon: ''
}
}
this.offlineHTML = <div id='marketplace'>
<div className='emptyMessage' style={{'marginTop': '20px', 'transform': 'translateY(80%)'}}>
<WifiOffIcon />
<h1>{this.props.language.offline.title}</h1>
<p className='description'>{this.props.language.offline.description}</p>
</div>
</div>;
}
async toggle(type, type2, data) {
if (type === 'seemore') {
document.getElementById('marketplace').style.display = 'none';
document.getElementById('seemore').style.display = 'block';
return this.setState({
see_more: this.state[type2],
see_more_type: type2
});
}
if (type === 'item') {
let info;
try {
info = await (await fetch(`${Constants.MARKETPLACE_URL}/item/${type2}/${data}`)).json();
} catch (e) {
return toast(this.props.toastLanguage.error);
}
this.setState({
current_data: { type: type2, name: data, content: info },
item_data: {
name: info.data.name,
author: info.data.author,
description: MarketplaceFunctions.urlParser(info.data.description.replace(/\n/g, '<br>')),
updated: info.updated,
version: info.data.version,
icon: info.data.screenshot_url
}
});
document.getElementById('marketplace').style.display = 'none';
document.getElementById('seemore').style.display = 'none';
document.getElementById('item').style.display = 'block';
let button = <button className='addToMue' onClick={() => this.install()}>{this.props.language.product.buttons.addtomue}</button>;
const installed = JSON.parse(localStorage.getItem('installed'));
if (installed.some(item => item.name === data)) button = <button className='removeFromMue' onClick={() => this.uninstall()}>{this.props.language.product.buttons.remove}</button>;
this.setState({ button: button });
} else {
document.getElementById('marketplace').style.display = 'block';
document.getElementById('item').style.display = 'none';
document.getElementById('seemore').style.display = 'none';
}
}
async getItems() {
const data = await (await fetch(Constants.MARKETPLACE_URL + '/all')).json();
const featured = await (await fetch(Constants.MARKETPLACE_URL + '/featured')).json();
this.setState({
themes: data.data.themes,
settings: data.data.settings,
photo_packs: data.data.photo_packs,
quote_packs: data.data.quote_packs,
see_more: data.data.photo_packs,
featured: featured.data,
done: true
});
}
install() {
let installed = JSON.parse(localStorage.getItem('installed'));
let button;
const installStuff = () => {
installed.push(this.state.current_data);
localStorage.setItem('installed', JSON.stringify(installed));
toast(this.props.toastLanguage.installed);
button = <button className='removeFromMue' onClick={() => this.uninstall()}>{this.props.language.product.buttons.remove}</button>;
this.setState({ button: button });
}
switch (this.state.current_data.type) {
case 'settings':
localStorage.removeItem('backup_settings');
let oldSettings = [];
for (const key of Object.keys(localStorage)) oldSettings.push({name: key, value: localStorage.getItem(key)});
localStorage.setItem('backup_settings', JSON.stringify(oldSettings));
this.state.current_data.content.data.settings.forEach(element => localStorage.setItem(element.name, element.value));
installStuff();
break;
case 'photo_packs':
localStorage.setItem('photo_packs', JSON.stringify(this.state.current_data.content.data.photos));
installStuff();
break;
case 'theme':
localStorage.setItem('theme', this.state.current_data.content.data.theme);
installStuff();
break;
case 'quote_packs':
if (this.state.current_data.content.data.quote_api) localStorage.setItem('quote_api', JSON.stringify(this.state.current_data.content.data.quote_api));
localStorage.setItem('quote_packs', JSON.stringify(this.state.current_data.content.data.quotes));
installStuff();
break;
default:
break;
}
}
uninstall() {
MarketplaceFunctions.uninstall(this.state.current_data.name, this.state.current_data.type);
toast(this.props.toastLanguage.removed);
this.setState({
button: <button className='addToMue' onClick={() => this.install()}>{this.props.language.product.buttons.addtomue}</button>
});
}
componentDidMount() {
if (navigator.onLine === false) return;
this.getItems();
}
render() {
if (navigator.onLine === false) return this.offlineHTML;
if (this.state.done === false) {
return (
<div id='marketplace'>
<div className='emptyMessage' style={{'marginTop': '20px', 'transform': 'translateY(80%)'}}>
<h1>Loading...</h1>
</div>
</div>
)
}
return (
<div>
<div id='marketplace'>
<div className='featured' style={{backgroundColor: this.state.featured.colour}}>
<p>{this.state.featured.title}</p>
<h1>{this.state.featured.name}</h1>
<button className='addToMue' onClick={() => window.location.href = this.state.featured.buttonLink}>{this.state.featured.buttonText}</button>
</div>
<Items
title={this.props.language.photo_packs}
seeMoreTitle={this.props.language.see_more}
items={this.state.photo_packs.slice(0, 3)}
toggleFunction={(input) => this.toggle('item', 'photo_packs', input)}
seeMoreFunction={() => this.toggle('seemore', 'photo_packs')} />
<Items
title={this.props.language.preset_settings}
seeMoreTitle={this.props.language.see_more}
items={this.state.settings.slice(0, 3)}
toggleFunction={(input) => this.toggle('item', 'settings', input)}
seeMoreFunction={() => this.toggle('seemore', 'settings')} />
<Items
title={this.props.language.quote_packs}
seeMoreTitle={this.props.language.see_more}
items={this.state.quote_packs.slice(0, 3)}
toggleFunction={(input) => this.toggle('item', 'quote_packs', input)}
seeMoreFunction={() => this.toggle('seemore', 'quote_packs')} />
<Items
title={this.props.language.themes}
seeMoreTitle={this.props.language.see_more}
items={this.state.themes.slice(0, 3)}
toggleFunction={(input) => this.toggle('item', 'theme', input)}
seeMoreFunction={() => this.toggle('seemore', 'themes')} />
</div>
<Item
button={this.state.button}
data={this.state.item_data}
content={this.state.current_data}
function={() => this.toggle()} language={this.props.language.product}
/>
<div id='seemore'>
<ArrowBackIcon className='backArrow' onClick={() => this.toggle()} />
<Items
title={this.props.language.see_more}
seeMoreTitle={this.props.language.see_more}
toggleFunction={(input) => this.toggle('item', this.state.see_more_type, input)}
items={this.state.see_more}
/>
</div>
</div>
)
}
}

View File

@@ -0,0 +1,99 @@
import React from 'react';
import SettingsFunctions from '../../../modules/settingsFunctions';
import Checkbox from '../settings/Checkbox';
import Slider from '../settings/Slider';
import Section from '../settings/Section';
import { toast } from 'react-toastify';
import BackgroundSettings from '../settings/sections/BackgroundSettings';
import SearchSettings from '../settings/sections/SearchSettings';
import LanguageSettings from '../settings/sections/LanguageSettings';
export default class Settings extends React.PureComponent {
resetGreeting() {
document.getElementById('greetingName').value = '';
toast(this.props.toastLanguage.reset);
}
updateCurrent() {
document.getElementById('greetingName').value = localStorage.getItem('greetingName');
document.getElementById('language').value = localStorage.getItem('language');
if (document.getElementById('modal').classList.contains('dark')) {
const choices = document.getElementsByClassName('choices');
for (let i = 0; i < choices.length; i++) choices[i].style.backgroundColor = '#2f3542';
}
}
componentDidMount() {
this.updateCurrent();
document.getElementById('file-input').onchange = (e) => {
const file = e.target.files[0];
if (file.type !== 'application/json') return console.error(`expected json, got ${file.type}`);
const reader = new FileReader();
reader.readAsText(file, 'UTF-8');
reader.onload = (readerEvent) => {
const content = JSON.parse(readerEvent.target.result);
for (const key of Object.keys(content)) localStorage.setItem(key, content[key]);
toast(this.props.toastLanguage.imported);
};
};
}
render() {
return (
<div className='columns'>
<Section title={this.props.language.time.title} name='time'>
<Checkbox name='seconds' text={this.props.language.time.seconds} />
<Checkbox name='24hour' text={this.props.language.time.twentyfourhour} />
<Checkbox name='ampm' text={this.props.language.time.ampm} />
<Checkbox name='zero' text={this.props.language.time.zero} />
<Checkbox name='analog' text={this.props.language.time.analog} />
</Section>
<Section title={this.props.language.greeting.title} name='greeting'>
<Checkbox name='events' text={this.props.language.greeting.events} />
<Checkbox name='defaultGreetingMessage' text={this.props.language.greeting.default} />
<ul>
<p>{this.props.language.greeting.name} <span className='modalLink' onClick={() => this.resetGreeting()}>{this.props.language.reset}</span></p>
<input type='text' id='greetingName'></input>
</ul>
</Section>
<Section title={this.props.language.quote.title} name='quote'>
<Checkbox name='copyButton' text={this.props.language.quote.copy} />
<Checkbox name='tweetButton' text={this.props.language.quote.tweet} />
<Checkbox name='favouriteQuoteEnabled' text={this.props.language.experimental.favourite} />
</Section>
<Section title={this.props.language.background.title} name='background'>
<BackgroundSettings language={this.props.language} toastLanguage={this.props.toastLanguage} />
</Section>
<Section title={this.props.language.searchbar.title} name='searchBar'>
<SearchSettings language={this.props.language} toastLanguage={this.props.toastLanguage} />
</Section>
<div className='section'>
<h4 className='nodropdown'>{this.props.language.offline}</h4>
<Slider name='offlineMode'/>
</div>
<div className='section'>
<h4 className='nodropdown'>{this.props.language.experimental.dark}</h4>
<Slider name='darkTheme'/>
</div>
<Section title={this.props.language.experimental.title} name='experimental' slider={false}>
<Checkbox name='webp' text={this.props.language.experimental.webp} />
<Checkbox name='animations' text={this.props.language.experimental.animations} />
<Checkbox name='voiceSearch' text={this.props.language.experimental.voicesearch} newFeature={true} />
<Checkbox name='brightnessTime' text='Automatic Night Mode' newFeature={true} />
</Section>
<LanguageSettings language={this.props.language} />
<button className='apply' onClick={() => SettingsFunctions.saveStuff(this.props.language.background.disabled)}>{this.props.language.apply}</button>
<button className='reset' onClick={() => this.props.setDefaultSettings()}>{this.props.language.reset}</button>
<button className='export' onClick={() => SettingsFunctions.exportSettings()}>{this.props.language.export}</button>
<button className='import' onClick={() => document.getElementById('file-input').click()}>{this.props.language.import}</button>
<input id='file-input' type='file' name='name' className='hidden' accept='application/json' />
</div>
);
}
}