mue/src/components/modals/main/marketplace/sections/Added.jsx

175 lines
4.6 KiB
React
Raw Normal View History

import variables from 'modules/variables';
2021-08-14 19:10:48 +00:00
import { PureComponent } from 'react';
import { MdLocalMall } from 'react-icons/md';
2021-08-15 21:28:37 +00:00
import { toast } from 'react-toastify';
2021-03-07 19:06:49 +00:00
import Item from '../Item';
import Items from '../Items';
2021-05-02 13:44:23 +00:00
import Dropdown from '../../settings/Dropdown';
2021-03-07 19:06:49 +00:00
2021-08-28 14:34:12 +00:00
import { uninstall, urlParser } from 'modules/helpers/marketplace';
2021-03-13 18:15:57 +00:00
2021-08-14 19:10:48 +00:00
export default class Added extends PureComponent {
getMessage = (text) => variables.language.getMessage(variables.languagecode, text);
2021-03-23 13:10:34 +00:00
constructor() {
super();
2021-03-07 19:06:49 +00:00
this.state = {
installed: JSON.parse(localStorage.getItem('installed')),
2021-05-03 10:27:52 +00:00
item: {},
button: '',
};
2021-03-07 19:06:49 +00:00
this.buttons = {
uninstall: (
<button className="removeFromMue" onClick={() => this.uninstall()}>
{this.getMessage('modals.main.marketplace.product.buttons.remove')}
</button>
),
2021-04-16 11:26:56 +00:00
};
2021-03-07 19:06:49 +00:00
}
2021-04-14 12:45:11 +00:00
toggle(type, data) {
2021-03-07 19:06:49 +00:00
if (type === 'item') {
const installed = JSON.parse(localStorage.getItem('installed'));
const info = {
data: installed.find((i) => i.name === data.name),
};
2021-03-07 19:06:49 +00:00
this.setState({
2021-04-14 12:45:11 +00:00
item: {
type: info.data.type,
display_name: info.data.name,
author: info.data.author,
description: urlParser(info.data.description.replace(/\n/g, '<br>')),
2021-04-14 12:45:11 +00:00
//updated: info.updated,
version: info.data.version,
icon: info.data.screenshot_url,
data: info.data,
},
button: this.buttons.uninstall,
2021-04-14 12:45:11 +00:00
});
2021-09-28 22:04:04 +00:00
variables.stats.postEvent('marketplace', 'Item viewed');
} else {
2021-04-14 12:45:11 +00:00
this.setState({
item: {},
2021-04-14 12:45:11 +00:00
});
2021-03-07 19:06:49 +00:00
}
2021-03-13 18:15:57 +00:00
}
2021-04-14 12:45:11 +00:00
uninstall() {
2021-08-14 19:10:48 +00:00
uninstall(this.state.item.type, this.state.item.display_name);
toast(this.getMessage('toasts.uninstalled'));
2021-03-13 18:15:57 +00:00
this.setState({
2021-04-14 12:45:11 +00:00
button: '',
installed: JSON.parse(localStorage.getItem('installed')),
2021-03-13 18:15:57 +00:00
});
2021-06-21 16:42:14 +00:00
2021-09-28 22:04:04 +00:00
variables.stats.postEvent('marketplace', 'Uninstall');
2021-03-13 18:15:57 +00:00
}
2021-03-07 19:06:49 +00:00
2021-06-21 16:42:14 +00:00
sortAddons(value, sendEvent) {
2021-05-02 13:44:23 +00:00
let installed = JSON.parse(localStorage.getItem('installed'));
switch (value) {
case 'newest':
installed.reverse();
break;
case 'oldest':
break;
case 'a-z':
installed.sort();
break;
case 'z-a':
installed.sort();
installed.reverse();
2021-05-03 10:27:52 +00:00
break;
default:
break;
2021-05-02 13:44:23 +00:00
}
this.setState({
installed,
2021-05-02 13:44:23 +00:00
});
2021-06-21 16:42:14 +00:00
if (sendEvent) {
2021-09-28 22:04:04 +00:00
variables.stats.postEvent('marketplace', 'Sort');
2021-06-21 16:42:14 +00:00
}
2021-05-02 13:44:23 +00:00
}
updateCheck() {
let updates = 0;
this.state.installed.forEach(async (item) => {
const data = await (
await fetch(variables.constants.MARKETPLACE_URL + '/item/' + item.name)
).json();
if (data.version !== item.version) {
updates++;
}
});
if (updates > 0) {
toast(
this.getMessage('modals.main.addons.updates_available', {
amount: updates,
}),
);
} else {
toast(this.getMessage('modals.main.addons.no_updates'));
}
}
2021-05-02 13:44:23 +00:00
componentDidMount() {
2021-06-21 16:42:14 +00:00
this.sortAddons(localStorage.getItem('sortAddons'), false);
2021-05-02 13:44:23 +00:00
}
2021-03-07 19:06:49 +00:00
render() {
if (this.state.installed.length === 0) {
return (
<div className="emptyItems">
<div className="emptyMessage">
<MdLocalMall />
<span className="title">{this.getMessage('modals.main.addons.empty.title')}</span>
<span className="subtitle">
{this.getMessage('modals.main.addons.empty.description')}
</span>
2021-03-07 19:06:49 +00:00
</div>
</div>
);
2021-03-07 19:06:49 +00:00
}
2021-05-03 10:27:52 +00:00
if (this.state.item.display_name) {
return (
<Item
data={this.state.item}
button={this.state.button}
toggleFunction={() => this.toggle()}
/>
);
2021-05-03 10:27:52 +00:00
}
2021-03-07 19:06:49 +00:00
return (
2021-04-03 12:32:00 +00:00
<>
<Dropdown
label={this.getMessage('modals.main.addons.sort.title')}
name="sortAddons"
onChange={(value) => this.sortAddons(value)}
>
<option value="newest">{this.getMessage('modals.main.addons.sort.newest')}</option>
<option value="oldest">{this.getMessage('modals.main.addons.sort.oldest')}</option>
<option value="a-z">{this.getMessage('modals.main.addons.sort.a_z')}</option>
<option value="z-a">{this.getMessage('modals.main.addons.sort.z_a')}</option>
2021-05-03 10:27:52 +00:00
</Dropdown>
<button className="addToMue sideload updateCheck" onClick={() => this.updateCheck()}>
{this.getMessage('modals.main.addons.check_updates')}
</button>
<br />
<Items
items={this.state.installed}
toggleFunction={(input) => this.toggle('item', input)}
/>
2021-04-03 12:32:00 +00:00
</>
2021-03-07 19:06:49 +00:00
);
}
}