mirror of
https://github.com/mgerb/ps-launcher
synced 2026-01-11 19:02:50 +00:00
add/update/delete servers
This commit is contained in:
@@ -2,8 +2,9 @@ import React from 'react';
|
||||
import fs from 'fs';
|
||||
import { exec } from 'child_process';
|
||||
import { inject, observer } from 'mobx-react';
|
||||
import { AppState } from '../../state/AppState';
|
||||
import { AppState, ServerType } from '../../state/AppState';
|
||||
import { toast } from '../../util';
|
||||
import { Modal } from '../Modal/Modal';
|
||||
|
||||
import './ServerList.scss';
|
||||
|
||||
@@ -11,23 +12,34 @@ interface Props {
|
||||
AppState?: AppState;
|
||||
}
|
||||
|
||||
interface State {
|
||||
showModal: boolean;
|
||||
editModal: boolean;
|
||||
editServerIndex: number;
|
||||
modalTitle: string;
|
||||
modalServerName: string;
|
||||
modalRealmList: string;
|
||||
modalWebsiteURL: string;
|
||||
}
|
||||
|
||||
@inject('AppState')
|
||||
@observer
|
||||
export class ServerList extends React.Component<Props, any> {
|
||||
export class ServerList extends React.Component<Props, State> {
|
||||
constructor(props: Props) {
|
||||
super(props);
|
||||
this.state = this.defaultState;
|
||||
}
|
||||
|
||||
private renderItems(): any {
|
||||
const { AppState } = this.props;
|
||||
return this.props.AppState.selectedExpansion.servers.map((server, index) => {
|
||||
const selected = AppState.selectedExpansion.selectedServerIndex === index ? ' selected' : '';
|
||||
return (
|
||||
<div key={index} className={'list-item' + selected} onClick={() => AppState.setSelectedServerIndex(index)}>
|
||||
{server.name}
|
||||
</div>
|
||||
);
|
||||
});
|
||||
private get defaultState(): State {
|
||||
return {
|
||||
showModal: false,
|
||||
editModal: false,
|
||||
editServerIndex: undefined,
|
||||
modalTitle: '',
|
||||
modalServerName: '',
|
||||
modalRealmList: '',
|
||||
modalWebsiteURL: '',
|
||||
};
|
||||
}
|
||||
|
||||
private async play(): Promise<void> {
|
||||
@@ -72,14 +84,143 @@ export class ServerList extends React.Component<Props, any> {
|
||||
});
|
||||
}
|
||||
|
||||
private modalOnCancel(): void {
|
||||
this.setState(this.defaultState);
|
||||
}
|
||||
|
||||
private modalOnDelete(): void {
|
||||
if (confirm('Are you sure you want to delete this server?')) {
|
||||
this.props.AppState.deleteServer(this.state.editServerIndex);
|
||||
this.setState(this.defaultState);
|
||||
}
|
||||
}
|
||||
|
||||
private modalOnSave(): void {
|
||||
const { modalServerName, modalRealmList, modalWebsiteURL } = this.state;
|
||||
|
||||
if (modalServerName === '' || modalRealmList === '' || modalWebsiteURL === '') {
|
||||
toast.error('All fields are required');
|
||||
return;
|
||||
}
|
||||
|
||||
const newServer: ServerType = {
|
||||
name: modalServerName,
|
||||
realmlist: modalRealmList,
|
||||
website: modalWebsiteURL,
|
||||
};
|
||||
|
||||
// edit server
|
||||
if (this.state.editModal) {
|
||||
this.props.AppState.editServer(this.state.editServerIndex, newServer);
|
||||
toast.success('Saved');
|
||||
this.setState(this.defaultState);
|
||||
} else { // else add new server
|
||||
if (!this.props.AppState.addServer(newServer)) {
|
||||
toast.error('Server already exists');
|
||||
} else {
|
||||
toast.success('Saved');
|
||||
this.setState(this.defaultState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private addServerClick(): void {
|
||||
this.setState({
|
||||
showModal: true,
|
||||
editModal: false,
|
||||
modalTitle: 'New Server',
|
||||
});
|
||||
}
|
||||
|
||||
private editServerClick(serverIndex: number): void {
|
||||
const server = this.props.AppState.selectedExpansion.servers[serverIndex];
|
||||
this.setState({
|
||||
showModal: true,
|
||||
editModal: true,
|
||||
editServerIndex: serverIndex,
|
||||
modalTitle: server.name,
|
||||
modalServerName: server.name,
|
||||
modalRealmList: server.realmlist,
|
||||
modalWebsiteURL: server.website,
|
||||
});
|
||||
}
|
||||
|
||||
private renderItems(): any {
|
||||
const { AppState } = this.props;
|
||||
return this.props.AppState.selectedExpansion.servers.map((server, index) => {
|
||||
const selected = AppState.selectedExpansion.selectedServerIndex === index ? ' selected' : '';
|
||||
return (
|
||||
<div key={index} className={'list-item' + selected} onClick={() => AppState.setSelectedServerIndex(index, true)}>
|
||||
<div className="ellipsis" title={server.name}>{server.name}</div>
|
||||
<i className="fa fa-pencil-square-o edit-button" title="Edit" onClick={() => this.editServerClick(index)}/>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private renderModal(): any {
|
||||
const { showModal, editModal, modalTitle, modalServerName, modalRealmList, modalWebsiteURL } = this.state;
|
||||
|
||||
return (
|
||||
<Modal isOpen={showModal} onClose={this.modalOnCancel.bind(this)} title={modalTitle}>
|
||||
<div className="modal-content">
|
||||
<div style={{ flex: 1 }}>
|
||||
<div className="form-group">
|
||||
<label className="form-group__label">Server Name</label>
|
||||
<input
|
||||
className="input"
|
||||
placeholder="Name"
|
||||
value={modalServerName}
|
||||
onChange={e => this.setState({ modalServerName: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="form-group">
|
||||
<label className="form-group__label">Realm List</label>
|
||||
<input
|
||||
className="input"
|
||||
placeholder="logon.server.com"
|
||||
value={modalRealmList}
|
||||
onChange={e => this.setState({ modalRealmList: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="form-group">
|
||||
<label className="form-group__label">Website URL</label>
|
||||
<input
|
||||
className="input"
|
||||
placeholder="https://www.server.com"
|
||||
value={modalWebsiteURL}
|
||||
onChange={e => this.setState({ modalWebsiteURL: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="button-group">
|
||||
<div style={{ display: 'flex' }}>
|
||||
{editModal && <i className="fa fa-trash fa-lg delete-button" onClick={() => this.modalOnDelete()}/>}
|
||||
</div>
|
||||
<div>
|
||||
<button className="button" onClick={this.modalOnCancel.bind(this)}>
|
||||
Cancel
|
||||
</button>
|
||||
<button className="button button--success" onClick={this.modalOnSave.bind(this)}>
|
||||
Save
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
public render(): any {
|
||||
const { selectedServer } = this.props.AppState;
|
||||
|
||||
return (
|
||||
<div className="server-list">
|
||||
{this.renderModal()}
|
||||
<div className="server-list-heading">
|
||||
<div>Servers</div>
|
||||
<i className="fa fa-plus" />
|
||||
<i className="fa fa-plus edit-button" onClick={this.addServerClick.bind(this)} />
|
||||
</div>
|
||||
<div className="item-container">{this.renderItems()}</div>
|
||||
<div className="start-button-container">
|
||||
|
||||
Reference in New Issue
Block a user