Unify dialog data in App.state

This commit is contained in:
Simon Ser 2021-07-04 21:41:36 +02:00
parent af1e2e144a
commit 52ddda4e82

View file

@ -133,6 +133,7 @@ export default class App extends Component {
connectForm: true, connectForm: true,
loading: true, loading: true,
dialog: null, dialog: null,
dialogData: null,
error: null, error: null,
openPanels: { openPanels: {
bufferList: false, bufferList: false,
@ -160,7 +161,7 @@ export default class App extends Component {
this.handleNickClick = this.handleNickClick.bind(this); this.handleNickClick = this.handleNickClick.bind(this);
this.autocomplete = this.autocomplete.bind(this); this.autocomplete = this.autocomplete.bind(this);
this.handleBufferScrollTop = this.handleBufferScrollTop.bind(this); this.handleBufferScrollTop = this.handleBufferScrollTop.bind(this);
this.handleDialogDismiss = this.handleDialogDismiss.bind(this); this.dismissDialog = this.dismissDialog.bind(this);
this.handleAddNetworkClick = this.handleAddNetworkClick.bind(this); this.handleAddNetworkClick = this.handleAddNetworkClick.bind(this);
this.handleNetworkSubmit = this.handleNetworkSubmit.bind(this); this.handleNetworkSubmit = this.handleNetworkSubmit.bind(this);
this.handleNetworkRemove = this.handleNetworkRemove.bind(this); this.handleNetworkRemove = this.handleNetworkRemove.bind(this);
@ -995,16 +996,16 @@ export default class App extends Component {
} }
handleJoinClick(serverID) { handleJoinClick(serverID) {
this.setState({ dialog: "join", joinDialog: { server: serverID } }); this.openDialog("join", { server: serverID });
} }
handleJoinSubmit(data) { handleJoinSubmit(data) {
let client = this.clients.get(this.state.joinDialog.server); let client = this.clients.get(this.state.dialogData.server);
this.switchToChannel = data.channel; this.switchToChannel = data.channel;
client.send({ command: "JOIN", params: [data.channel] }); client.send({ command: "JOIN", params: [data.channel] });
this.setState({ dialog: null, joinDialog: null }); this.dismissDialog();
} }
autocomplete(prefix) { autocomplete(prefix) {
@ -1032,7 +1033,7 @@ export default class App extends Component {
} }
openHelp() { openHelp() {
this.setState({ dialog: "help" }); this.openDialog("help");
} }
handleBufferScrollTop() { handleBufferScrollTop() {
@ -1065,39 +1066,40 @@ export default class App extends Component {
}); });
} }
handleDialogDismiss() { openDialog(name, data) {
this.setState({ dialog: null }); this.setState({ dialog: name, dialogData: data });
}
dismissDialog() {
this.setState({ dialog: null, dialogData: null });
} }
handleAddNetworkClick() { handleAddNetworkClick() {
this.setState({ dialog: "network", networkDialog: null }); this.openDialog("network");
} }
handleManageNetworkClick(serverID) { handleManageNetworkClick(serverID) {
let server = this.state.servers.get(serverID); let server = this.state.servers.get(serverID);
let bouncerNetID = server.isupport.get("BOUNCER_NETID"); let bouncerNetID = server.isupport.get("BOUNCER_NETID");
let bouncerNetwork = this.state.bouncerNetworks.get(bouncerNetID); let bouncerNetwork = this.state.bouncerNetworks.get(bouncerNetID);
this.setState({ this.openDialog("network", {
dialog: "network", id: bouncerNetID,
networkDialog: { params: bouncerNetwork,
id: bouncerNetID,
params: bouncerNetwork,
},
}); });
} }
handleNetworkSubmit(attrs) { handleNetworkSubmit(attrs) {
let client = this.clients.values().next().value; let client = this.clients.values().next().value;
if (this.state.networkDialog && this.state.networkDialog.id) { if (this.state.dialogData && this.state.dialogData.id) {
if (Object.keys(attrs).length == 0) { if (Object.keys(attrs).length == 0) {
this.setState({ dialog: null }); this.dismissDialog();
return; return;
} }
client.send({ client.send({
command: "BOUNCER", command: "BOUNCER",
params: ["CHANGENETWORK", this.state.networkDialog.id, irc.formatTags(attrs)], params: ["CHANGENETWORK", this.state.dialogData.id, irc.formatTags(attrs)],
}); });
} else { } else {
attrs = { ...attrs, tls: "1" }; attrs = { ...attrs, tls: "1" };
@ -1107,7 +1109,7 @@ export default class App extends Component {
}); });
} }
this.setState({ dialog: null, networkDialog: null }); this.dismissDialog();
} }
handleNetworkRemove() { handleNetworkRemove() {
@ -1115,10 +1117,10 @@ export default class App extends Component {
client.send({ client.send({
command: "BOUNCER", command: "BOUNCER",
params: ["DELNETWORK", this.state.networkDialog.id], params: ["DELNETWORK", this.state.dialogData.id],
}); });
this.setState({ dialog: null, networkDialog: null }); this.dismissDialog();
} }
componentDidMount() { componentDidMount() {
@ -1212,27 +1214,27 @@ export default class App extends Component {
let dialog = null; let dialog = null;
switch (this.state.dialog) { switch (this.state.dialog) {
case "network": case "network":
let title = this.state.networkDialog ? "Edit network" : "Add network"; let title = this.state.dialogData ? "Edit network" : "Add network";
dialog = html` dialog = html`
<${Dialog} title=${title} onDismiss=${this.handleDialogDismiss}> <${Dialog} title=${title} onDismiss=${this.dismissDialog}>
<${NetworkForm} <${NetworkForm}
onSubmit=${this.handleNetworkSubmit} onSubmit=${this.handleNetworkSubmit}
onRemove=${this.handleNetworkRemove} onRemove=${this.handleNetworkRemove}
params=${this.state.networkDialog ? this.state.networkDialog.params : null} params=${this.state.dialogData ? this.state.dialogData.params : null}
/> />
</> </>
`; `;
break; break;
case "help": case "help":
dialog = html` dialog = html`
<${Dialog} title="Help" onDismiss=${this.handleDialogDismiss}> <${Dialog} title="Help" onDismiss=${this.dismissDialog}>
<${Help}/> <${Help}/>
</> </>
`; `;
break; break;
case "join": case "join":
dialog = html` dialog = html`
<${Dialog} title="Join channel" onDismiss=${this.handleDialogDismiss}> <${Dialog} title="Join channel" onDismiss=${this.dismissDialog}>
<${JoinForm} onSubmit=${this.handleJoinSubmit}/> <${JoinForm} onSubmit=${this.handleJoinSubmit}/>
</> </>
`; `;