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

222 lines
6.4 KiB
React
Raw Normal View History

2021-03-07 19:06:49 +00:00
import React from 'react';
import WifiOffIcon from '@material-ui/icons/WifiOff';
import LocalMallIcon from '@material-ui/icons/LocalMall';
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-03-20 12:55:20 +00:00
import MarketplaceFunctions from '../../../../../modules/helpers/marketplace';
2021-03-07 19:06:49 +00:00
import { toast } from 'react-toastify';
export default class Marketplace extends React.PureComponent {
2021-03-23 13:10:34 +00:00
constructor() {
super();
2021-03-07 19:06:49 +00:00
this.state = {
items: [],
button: '',
featured: {},
done: false,
2021-05-03 10:27:52 +00:00
item: {}
2021-04-08 19:02:31 +00:00
};
this.buttons = {
uninstall: <button className='removeFromMue' onClick={() => this.manage('uninstall')}>{window.language.modals.main.marketplace.product.buttons.remove}</button>,
install: <button className='addToMue' onClick={() => this.manage('install')}>{window.language.modals.main.marketplace.product.buttons.addtomue}</button>
2021-04-16 11:26:56 +00:00
};
this.language = window.language.modals.main.marketplace;
this.controller = new AbortController();
}
2021-03-07 19:06:49 +00:00
async toggle(type, data) {
2021-04-14 12:45:11 +00:00
if (type === 'item') {
let info;
// get item info
try {
info = await (await fetch(`${window.constants.MARKETPLACE_URL}/item/${this.props.type}/${data}`, { signal: this.controller.signal })).json();
2021-04-14 12:45:11 +00:00
} catch (e) {
if (this.controller.signal.aborted === false) {
return toast(window.language.toasts.error);
}
}
if (this.controller.signal.aborted === true) {
return;
2021-04-14 12:45:11 +00:00
}
2021-03-07 19:06:49 +00:00
2021-04-14 12:45:11 +00:00
// check if already installed
let button = this.buttons.install;
2021-04-14 12:45:11 +00:00
const installed = JSON.parse(localStorage.getItem('installed'));
2021-04-16 11:39:51 +00:00
if (installed.some((item) => item.name === info.data.name)) {
2021-04-14 12:45:11 +00:00
button = this.buttons.uninstall;
}
2021-04-14 12:45:11 +00:00
this.setState({
item: {
2021-05-03 14:39:34 +00:00
type: info.data.type,
2021-04-14 12:45:11 +00:00
display_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,
data: info.data
},
2021-05-03 10:27:52 +00:00
button: button
2021-04-14 12:45:11 +00:00
});
2021-06-21 16:42:14 +00:00
window.stats.postEvent('marketplace-item', `${this.state.item.display_name} viewed`);
2021-04-14 12:45:11 +00:00
} else {
this.setState({
2021-05-03 10:27:52 +00:00
item: {}
2021-04-14 12:45:11 +00:00
});
}
}
2021-03-07 19:06:49 +00:00
async getItems() {
const { data } = await (await fetch(window.constants.MARKETPLACE_URL + '/items/' + this.props.type, { signal: this.controller.signal })).json();
const featured = await (await fetch(window.constants.MARKETPLACE_URL + '/featured', { signal: this.controller.signal })).json();
if (this.controller.signal.aborted === true) {
return;
}
2021-03-07 19:06:49 +00:00
this.setState({
items: data,
oldItems: data,
featured: featured.data,
done: true
2021-03-07 19:06:49 +00:00
});
2021-05-02 13:44:23 +00:00
2021-06-21 16:42:14 +00:00
this.sortMarketplace(localStorage.getItem('sortMarketplace'), false);
2021-03-07 19:06:49 +00:00
}
manage(type) {
2021-04-14 12:45:11 +00:00
if (type === 'install') {
MarketplaceFunctions.install(this.state.item.type, this.state.item.data);
} else {
2021-04-21 18:35:33 +00:00
MarketplaceFunctions.uninstall(this.state.item.type, this.state.item.display_name);
2021-03-07 19:06:49 +00:00
}
toast(window.language.toasts[type + 'ed']);
2021-03-07 19:06:49 +00:00
this.setState({
button: (type === 'install') ? this.buttons.uninstall : this.buttons.install
});
2021-06-21 16:42:14 +00:00
window.stats.postEvent('marketplace-item', `${this.state.item.display_name} ${(type === 'install' ? 'installed': 'uninstalled')}`);
window.stats.postEvent('marketplace', (type === 'install' ? 'Install': 'Uninstall'));
2021-03-07 19:06:49 +00:00
}
2021-06-21 16:42:14 +00:00
sortMarketplace(value, sendEvent) {
2021-05-02 13:44:23 +00:00
let items = this.state.oldItems;
switch (value) {
case 'a-z':
items.sort();
// fix sort not working sometimes
if (this.state.sortType === 'z-a') {
items.reverse();
}
2021-05-02 13:44:23 +00:00
break;
case 'z-a':
items.sort();
items.reverse();
2021-05-03 10:27:52 +00:00
break;
default:
break;
2021-05-02 13:44:23 +00:00
}
this.setState({
items: items,
sortType: value
2021-05-02 13:44:23 +00:00
});
2021-06-21 16:42:14 +00:00
if (sendEvent) {
window.stats.postEvent('marketplace', 'Sort');
2021-06-21 16:42:14 +00:00
}
2021-05-02 13:44:23 +00:00
}
2021-03-07 19:06:49 +00:00
componentDidMount() {
if (navigator.onLine === false || localStorage.getItem('offlineMode') === 'true') {
return;
}
this.getItems();
}
componentWillUnmount() {
// stop making requests
this.controller.abort();
}
2021-03-07 19:06:49 +00:00
render() {
const errorMessage = (msg) => {
return (
<div className='emptyitems'>
<div className='emptyMessage'>
{msg}
</div>
</div>
);
2021-04-16 11:39:51 +00:00
};
2021-03-07 19:06:49 +00:00
2021-05-26 21:28:26 +00:00
const featured = () => {
2021-06-21 16:42:14 +00:00
const openFeatured = () => {
window.stats.postEvent('marketplace', 'Featured clicked');
2021-06-21 16:42:14 +00:00
window.open(this.state.featured.buttonLink);
}
2021-05-26 21:28:26 +00:00
return (
<div className='featured' style={{ backgroundColor: this.state.featured.colour }}>
2021-05-26 21:28:26 +00:00
<p>{this.state.featured.title}</p>
<h1>{this.state.featured.name}</h1>
2021-06-21 16:42:14 +00:00
<button className='addToMue' onClick={() => openFeatured()}>{this.state.featured.buttonText}</button>
2021-05-26 21:28:26 +00:00
</div>
);
}
if (navigator.onLine === false || localStorage.getItem('offlineMode') === 'true') {
return errorMessage(<>
<WifiOffIcon/>
<h1>{this.language.offline.title}</h1>
<p className='description'>{this.language.offline.description}</p>
</>);
2021-03-07 19:06:49 +00:00
}
if (this.state.done === false) {
return errorMessage(<h1>{window.language.modals.main.loading}</h1>);
2021-03-07 19:06:49 +00:00
}
if (this.state.items.length === 0) {
2021-05-26 21:28:26 +00:00
return (
<>
{featured()}
{errorMessage(<>
<LocalMallIcon/>
<h1>{window.language.modals.main.addons.empty.title}</h1>
<p className='description'>{this.language.no_items}</p>
</>)}
</>
)
}
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-03-07 19:06:49 +00:00
return (
2021-04-03 12:32:00 +00:00
<>
2021-05-26 21:28:26 +00:00
{featured()}
2021-05-03 10:27:52 +00:00
<br/>
<Dropdown label={window.language.modals.main.addons.sort.title} name='sortMarketplace' onChange={(value) => this.sortMarketplace(value)}>
<option value='a-z'>{window.language.modals.main.addons.sort.a_z}</option>
<option value='z-a'>{window.language.modals.main.addons.sort.z_a}</option>
</Dropdown>
<br/>
<Items items={this.state.items} toggleFunction={(input) => this.toggle('item', input)} />
2021-04-03 12:32:00 +00:00
</>
);
}
}