From 8b4d1d18f15cdd7791b3be83d999426937a92e44 Mon Sep 17 00:00:00 2001 From: David Ralph Date: Fri, 17 Sep 2021 14:36:47 +0100 Subject: [PATCH] feat: multiple custom quotes, fix background settings --- .../modals/main/settings/sections/Quote.jsx | 81 +++++++++++++++-- .../sections/background/Background.jsx | 88 +++++++++---------- src/components/widgets/quote/Quote.jsx | 24 +++-- src/translations/en_GB.json | 1 + 4 files changed, 138 insertions(+), 56 deletions(-) diff --git a/src/components/modals/main/settings/sections/Quote.jsx b/src/components/modals/main/settings/sections/Quote.jsx index d02d6b2f..59243745 100644 --- a/src/components/modals/main/settings/sections/Quote.jsx +++ b/src/components/modals/main/settings/sections/Quote.jsx @@ -1,8 +1,8 @@ import variables from 'modules/variables'; -import { PureComponent } from 'react'; +import { PureComponent, Fragment } from 'react'; +import { Cancel } from '@mui/icons-material'; import Checkbox from '../Checkbox'; -import Text from '../Text'; import Switch from '../Switch'; import Slider from '../Slider'; import Dropdown from '../Dropdown'; @@ -14,6 +14,7 @@ export default class QuoteSettings extends PureComponent { super(); this.state = { quoteType: localStorage.getItem('quoteType') || 'api', + customQuote: this.getCustom() }; } @@ -23,13 +24,80 @@ export default class QuoteSettings extends PureComponent { } } + resetCustom = () => { + localStorage.setItem('customQuote', '[{"quote": "", "author": ""}]'); + this.setState({ + customQuote: [{ + quote: '', + author: '' + }] + }); + toast(this.getMessage('toasts.reset')); + EventBus.dispatch('refresh', 'background'); + } + + customQuote(e, text, index, type) { + const result = (text === true) ? e.target.value : e.target.result; + + const customQuote = this.state.customQuote; + customQuote[index][type] = result; + this.setState({ + customQuote + }); + this.forceUpdate(); + + localStorage.setItem('customQuote', JSON.stringify(customQuote)); + document.querySelector('.reminder-info').style.display = 'block'; + localStorage.setItem('showReminder', true); + } + + modifyCustomQuote(type, index) { + const customQuote = this.state.customQuote; + if (type === 'add') { + customQuote.push({ + quote: '', + author: '' + }); + } else { + customQuote.splice(index, 1); + } + + this.setState({ + customQuote + }); + this.forceUpdate(); + + localStorage.setItem('customQuote', JSON.stringify(customQuote)); + } + + getCustom() { + let data = JSON.parse(localStorage.getItem('customQuote')); + if (data === null) { + data = [{ + quote: localStorage.getItem('customQuote') || '', + author: localStorage.getItem('customQuoteAuthor') || '' + }]; + } + return data; + } + render() { let customSettings; if (this.state.quoteType === 'custom') { customSettings = ( <> - - +

{this.getMessage('modals.main.settings.sections.quote.custom')} {this.getMessage('modals.main.settings.buttons.reset')}

+ {this.state.customQuote.map((_url, index) => ( + + this.customQuote(e, true, index, 'quote')}> + this.customQuote(e, true, index, 'author')}> + {this.state.customQuote.length > 1 ? : null} +

+
+ ))} + ); } else { @@ -54,6 +122,8 @@ export default class QuoteSettings extends PureComponent { <>

{this.getMessage('modals.main.settings.sections.quote.title')}

+ +
this.setState({ quoteType: value })} category='quote'> {this.marketplaceType()} @@ -61,7 +131,6 @@ export default class QuoteSettings extends PureComponent { {customSettings} -

{this.getMessage('modals.main.settings.sections.quote.buttons.title')}

@@ -70,4 +139,4 @@ export default class QuoteSettings extends PureComponent { ); } -} \ No newline at end of file +} diff --git a/src/components/modals/main/settings/sections/background/Background.jsx b/src/components/modals/main/settings/sections/background/Background.jsx index 2ea56ca6..058959b6 100644 --- a/src/components/modals/main/settings/sections/background/Background.jsx +++ b/src/components/modals/main/settings/sections/background/Background.jsx @@ -135,7 +135,7 @@ export default class BackgroundSettings extends PureComponent { } render() { - const { getMessage, languagecode } = this; + const { getMessage } = this; let backgroundSettings; @@ -157,29 +157,29 @@ export default class BackgroundSettings extends PureComponent { const APISettings = ( <>
- +
- + {this.state.backgroundCategories.map((category) => ( ))}

- - - - - + + + + +

- - - - - - - - + + + + + + + + ); @@ -187,19 +187,19 @@ export default class BackgroundSettings extends PureComponent { const customSettings = ( <>
    -

    {getMessage(languagecode, 'modals.main.settings.sections.background.source.custom_background')} {getMessage(languagecode, 'modals.main.settings.buttons.reset')}

    +

    {getMessage('modals.main.settings.sections.background.source.custom_background')} {getMessage('modals.main.settings.buttons.reset')}

    {this.state.customBackground.map((_url, index) => ( this.customBackground(e, true, index)}> {this.state.customBackground.length > 1 ? : null} - this.uploadCustombackground(index)}>{getMessage(languagecode, 'modals.main.settings.sections.background.source.upload')} + this.uploadCustombackground(index)}>{getMessage('modals.main.settings.sections.background.source.upload')} {this.videoCustomSettings(index)}

    ))} - + this.customBackground(e, false, this.state.currentBackgroundIndex)} />
@@ -217,41 +217,41 @@ export default class BackgroundSettings extends PureComponent { return ( <> -

{getMessage(languagecode, 'modals.main.settings.sections.background.title')}

- - - - - +

{getMessage('modals.main.settings.sections.background.title')}

+ + + + + -

{getMessage(languagecode, 'modals.main.settings.sections.background.source.title')}

- this.setState({ backgroundType: value })} category='background'> +

{getMessage('modals.main.settings.sections.background.source.title')}

+ this.setState({ backgroundType: value })} category='background'> {this.marketplaceType()} - - - + + +
{backgroundSettings} -

{getMessage(languagecode, 'modals.main.settings.sections.background.buttons.title')}

- - - +

{getMessage('modals.main.settings.sections.background.buttons.title')}

+ + + -

{getMessage(languagecode, 'modals.main.settings.sections.background.effects.title')}

- - +

{getMessage('modals.main.settings.sections.background.effects.title')}

+ +

- - - - - - + + + + + + - + ); } diff --git a/src/components/widgets/quote/Quote.jsx b/src/components/widgets/quote/Quote.jsx index 04343708..364e1f8c 100644 --- a/src/components/widgets/quote/Quote.jsx +++ b/src/components/widgets/quote/Quote.jsx @@ -77,14 +77,26 @@ export default class Quote extends PureComponent { switch (this.state.type) { case 'custom': - const customQuote = localStorage.getItem('customQuote'); - const customQuoteAuthor = localStorage.getItem('customQuoteAuthor'); + let customQuote; + try { + customQuote = JSON.parse(localStorage.getItem('customQuote')); + } catch (e) { + // move to new format + customQuote = [{ + quote: localStorage.getItem('customQuote'), + author: localStorage.getItem('customQuoteAuthor') + }]; + localStorage.setItem('customQuote', JSON.stringify(customQuote)); + } - if (customQuote) { + // pick random + customQuote = customQuote[Math.floor(Math.random() * customQuote.length)]; + + if (customQuote !== '' && customQuote !== 'undefined' && customQuote !== ['']) { return this.setState({ - quote: '"' + customQuote + '"', - author: customQuoteAuthor, - authorlink: this.getAuthorLink(customQuote) + quote: '"' + customQuote.quote + '"', + author: customQuote.author, + authorlink: this.getAuthorLink(customQuote.author) }); } break; diff --git a/src/translations/en_GB.json b/src/translations/en_GB.json index 64f00480..8d02a37b 100644 --- a/src/translations/en_GB.json +++ b/src/translations/en_GB.json @@ -112,6 +112,7 @@ "author_link": "Author link", "custom": "Custom quote", "custom_author": "Custom author", + "add": "Add quote", "buttons": { "title": "Buttons", "copy": "Copy",