[MM-31547] Stop users from being able to enter the same server name or URL twice (#2049)

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
This commit is contained in:
Devin Binnie
2022-04-19 09:31:04 -04:00
committed by GitHub
parent a26e3caf23
commit ce2ddb6a6a
6 changed files with 72 additions and 8 deletions

View File

@@ -4,7 +4,7 @@
import {app, dialog, IpcMainEvent, IpcMainInvokeEvent, Menu} from 'electron';
import log from 'electron-log';
import {Team} from 'types/config';
import {Team, TeamWithIndex} from 'types/config';
import {MentionData} from 'types/notification';
import Config from 'common/config';
@@ -109,7 +109,7 @@ export function handleNewServerModal() {
if (!mainWindow) {
return;
}
const modalPromise = ModalManager.addModal<unknown, Team>('newServer', html, modalPreload, {}, mainWindow, Config.teams.length === 0);
const modalPromise = ModalManager.addModal<TeamWithIndex[], Team>('newServer', html, modalPreload, Config.teams.map((team, index) => ({...team, index})), mainWindow, Config.teams.length === 0);
if (modalPromise) {
modalPromise.then((data) => {
const teams = Config.teams;
@@ -145,7 +145,15 @@ export function handleEditServerModal(e: IpcMainEvent, name: string) {
if (serverIndex < 0) {
return;
}
const modalPromise = ModalManager.addModal<Team, Team>('editServer', html, modalPreload, Config.teams[serverIndex], mainWindow);
const modalPromise = ModalManager.addModal<{currentTeams: TeamWithIndex[]; team: TeamWithIndex}, Team>(
'editServer',
html,
modalPreload,
{
currentTeams: Config.teams.map((team, index) => ({...team, index})),
team: {...Config.teams[serverIndex], index: serverIndex},
},
mainWindow);
if (modalPromise) {
modalPromise.then((data) => {
const teams = Config.teams;

View File

@@ -13,6 +13,7 @@ type Props = {
onClose?: () => void;
onSave?: (team: TeamWithIndex) => void;
team?: TeamWithIndex;
currentTeams?: TeamWithIndex[];
editMode?: boolean;
show?: boolean;
restoreFocus?: boolean;
@@ -62,6 +63,15 @@ export default class NewTeamModal extends React.PureComponent<Props, State> {
if (!this.state.saveStarted) {
return null;
}
if (this.props.currentTeams) {
const currentTeams = [...this.props.currentTeams];
if (this.props.editMode && this.props.team) {
currentTeams.splice(this.props.team.index, 1);
}
if (currentTeams.find((team) => team.name === this.state.teamName)) {
return 'A server with the same name already exists.';
}
}
return this.state.teamName.length > 0 ? null : 'Name is required.';
}
@@ -79,6 +89,15 @@ export default class NewTeamModal extends React.PureComponent<Props, State> {
if (!this.state.saveStarted) {
return null;
}
if (this.props.currentTeams) {
const currentTeams = [...this.props.currentTeams];
if (this.props.editMode && this.props.team) {
currentTeams.splice(this.props.team.index, 1);
}
if (currentTeams.find((team) => team.url === this.state.teamUrl)) {
return 'A server with the same URL already exists.';
}
}
if (this.state.teamUrl.length === 0) {
return 'URL is required.';
}

View File

@@ -28,11 +28,13 @@ const onSave = (data: TeamWithIndex) => {
const EditServerModalWrapper: React.FC = () => {
const [server, setServer] = useState<TeamWithIndex>();
const [currentTeams, setCurrentTeams] = useState<TeamWithIndex[]>();
const handleEditServerMessage = (event: {data: ModalMessage<TeamWithIndex>}) => {
const handleEditServerMessage = (event: {data: ModalMessage<{currentTeams: TeamWithIndex[]; team: TeamWithIndex}>}) => {
switch (event.data.type) {
case MODAL_INFO: {
setServer(event.data.data);
setServer(event.data.data.team);
setCurrentTeams(event.data.data.currentTeams);
break;
}
default:
@@ -52,6 +54,7 @@ const EditServerModalWrapper: React.FC = () => {
editMode={true}
show={Boolean(server)}
team={server}
currentTeams={currentTeams}
/>
);
};

View File

@@ -10,7 +10,7 @@ import ReactDOM from 'react-dom';
import {TeamWithIndex} from 'types/config';
import {ModalMessage} from 'types/modals';
import {GET_MODAL_UNCLOSEABLE, MODAL_CANCEL, MODAL_RESULT, MODAL_UNCLOSEABLE} from 'common/communication';
import {GET_MODAL_UNCLOSEABLE, MODAL_CANCEL, MODAL_INFO, MODAL_RESULT, MODAL_UNCLOSEABLE, RETRIEVE_MODAL_INFO} from 'common/communication';
import NewTeamModal from '../../components/NewTeamModal'; //'./addServer.jsx';
@@ -28,13 +28,17 @@ const onSave = (data: TeamWithIndex) => {
const NewServerModalWrapper: React.FC = () => {
const [unremoveable, setUnremovable] = useState<boolean>();
const [currentTeams, setCurrentTeams] = useState<TeamWithIndex[]>();
const handleNewServerMessage = (event: {data: ModalMessage<boolean>}) => {
const handleNewServerMessage = (event: {data: ModalMessage<unknown>}) => {
switch (event.data.type) {
case MODAL_UNCLOSEABLE: {
setUnremovable(event.data.data);
setUnremovable(event.data.data as boolean);
break;
}
case MODAL_INFO:
setCurrentTeams(event.data.data as TeamWithIndex[]);
break;
default:
break;
}
@@ -43,6 +47,7 @@ const NewServerModalWrapper: React.FC = () => {
useEffect(() => {
window.addEventListener('message', handleNewServerMessage);
window.postMessage({type: GET_MODAL_UNCLOSEABLE}, window.location.href);
window.postMessage({type: RETRIEVE_MODAL_INFO}, window.location.href);
return () => {
window.removeEventListener('message', handleNewServerMessage);
@@ -55,6 +60,7 @@ const NewServerModalWrapper: React.FC = () => {
onSave={onSave}
editMode={false}
show={true}
currentTeams={currentTeams}
/>
);
};