add guard cannotSendMessage
This commit is contained in:
@@ -161,7 +161,10 @@ export class ChatModel {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// messaging
|
// messaging
|
||||||
sendMessage = (body: string): void => {
|
sendMessage = (body: string): boolean => {
|
||||||
|
const senderName = this.settingsModel.username;
|
||||||
|
if (senderName == "") return false;
|
||||||
|
|
||||||
const allChannels = [this.info.primaryChannel];
|
const allChannels = [this.info.primaryChannel];
|
||||||
for (const secondaryChannel of this.info.secondaryChannels) {
|
for (const secondaryChannel of this.info.secondaryChannels) {
|
||||||
allChannels.push(secondaryChannel);
|
allChannels.push(secondaryChannel);
|
||||||
@@ -171,11 +174,12 @@ export class ChatModel {
|
|||||||
|
|
||||||
const chatMessage: ChatMessage = ChatModel.createChatMessage(
|
const chatMessage: ChatMessage = ChatModel.createChatMessage(
|
||||||
combinedChannel,
|
combinedChannel,
|
||||||
this.settingsModel.username,
|
senderName,
|
||||||
body
|
body
|
||||||
);
|
);
|
||||||
|
|
||||||
this.connectionModel.sendMessageOrStore(chatMessage);
|
this.connectionModel.sendMessageOrStore(chatMessage);
|
||||||
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
// memory
|
// memory
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ export function HomePage(
|
|||||||
<span>{translations.homePage.yourNameLabel}</span>
|
<span>{translations.homePage.yourNameLabel}</span>
|
||||||
<input
|
<input
|
||||||
placeholder={translations.homePage.yourNamePlaceholder}
|
placeholder={translations.homePage.yourNamePlaceholder}
|
||||||
bind:value={settingsViewModel.nameInput}
|
bind:value={settingsViewModel.usernameInput}
|
||||||
on:enter={settingsViewModel.setName}
|
on:enter={settingsViewModel.setName}
|
||||||
></input>
|
></input>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -3,11 +3,13 @@ import * as React from "bloatless-react";
|
|||||||
import ChatListModel from "../Model/chatListModel";
|
import ChatListModel from "../Model/chatListModel";
|
||||||
import { ChatModel } from "../Model/chatModel";
|
import { ChatModel } from "../Model/chatModel";
|
||||||
import ChatViewModel from "./chatViewModel";
|
import ChatViewModel from "./chatViewModel";
|
||||||
|
import SettingsViewModel from "./settingsViewModel";
|
||||||
import StorageModel from "../Model/storageModel";
|
import StorageModel from "../Model/storageModel";
|
||||||
|
|
||||||
export default class ChatListViewModel {
|
export default class ChatListViewModel {
|
||||||
chatListModel: ChatListModel;
|
chatListModel: ChatListModel;
|
||||||
storageModel: StorageModel;
|
storageModel: StorageModel;
|
||||||
|
settingsViewModel: SettingsViewModel;
|
||||||
|
|
||||||
// state
|
// state
|
||||||
newChatPrimaryChannel: React.State<string> = new React.State("");
|
newChatPrimaryChannel: React.State<string> = new React.State("");
|
||||||
@@ -42,8 +44,13 @@ export default class ChatListViewModel {
|
|||||||
};
|
};
|
||||||
|
|
||||||
createChatViewModel = (chatModel: ChatModel): ChatViewModel => {
|
createChatViewModel = (chatModel: ChatModel): ChatViewModel => {
|
||||||
return new ChatViewModel(chatModel, this.storageModel, this);
|
return new ChatViewModel(
|
||||||
}
|
chatModel,
|
||||||
|
this.storageModel,
|
||||||
|
this.settingsViewModel,
|
||||||
|
this
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
// view
|
// view
|
||||||
openChat = (chatViewModel: ChatViewModel): void => {
|
openChat = (chatViewModel: ChatViewModel): void => {
|
||||||
@@ -59,7 +66,7 @@ export default class ChatListViewModel {
|
|||||||
for (const chatViewModel of this.chatViewModels.value) {
|
for (const chatViewModel of this.chatViewModels.value) {
|
||||||
chatViewModel.updateIndex();
|
chatViewModel.updateIndex();
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
// restore
|
// restore
|
||||||
restoreChats = (): void => {
|
restoreChats = (): void => {
|
||||||
@@ -71,9 +78,14 @@ export default class ChatListViewModel {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// init
|
// init
|
||||||
constructor(chatListModel: ChatListModel, storageModel: StorageModel) {
|
constructor(
|
||||||
|
chatListModel: ChatListModel,
|
||||||
|
storageModel: StorageModel,
|
||||||
|
settingsViewModel: SettingsViewModel
|
||||||
|
) {
|
||||||
this.chatListModel = chatListModel;
|
this.chatListModel = chatListModel;
|
||||||
this.storageModel = storageModel;
|
this.storageModel = storageModel;
|
||||||
|
this.settingsViewModel = settingsViewModel;
|
||||||
|
|
||||||
this.restoreChats();
|
this.restoreChats();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ import StorageModel, { storageKeys } from "../Model/storageModel";
|
|||||||
import ChatListViewModel from "./chatListViewModel";
|
import ChatListViewModel from "./chatListViewModel";
|
||||||
import { ChatModel } from "../Model/chatModel";
|
import { ChatModel } from "../Model/chatModel";
|
||||||
import { Color } from "./colors";
|
import { Color } from "./colors";
|
||||||
|
import SettingsViewModel from "./settingsViewModel";
|
||||||
import { localeCompare } from "../Model/Utility/utility";
|
import { localeCompare } from "../Model/Utility/utility";
|
||||||
|
|
||||||
export default class ChatViewModel {
|
export default class ChatViewModel {
|
||||||
chatModel: ChatModel;
|
chatModel: ChatModel;
|
||||||
storageModel: StorageModel;
|
storageModel: StorageModel;
|
||||||
|
settingsViewModel: SettingsViewModel;
|
||||||
chatListViewModel: ChatListViewModel;
|
chatListViewModel: ChatListViewModel;
|
||||||
|
|
||||||
// state
|
// state
|
||||||
@@ -44,6 +46,7 @@ export default class ChatViewModel {
|
|||||||
this.primaryChannelInput.value == this.primaryChannel.value
|
this.primaryChannelInput.value == this.primaryChannel.value
|
||||||
);
|
);
|
||||||
cannotSetEncryptionKey: React.State<boolean>;
|
cannotSetEncryptionKey: React.State<boolean>;
|
||||||
|
cannotSendMessage: React.State<boolean>;
|
||||||
|
|
||||||
// sorting
|
// sorting
|
||||||
updateIndex = (): void => {
|
updateIndex = (): void => {
|
||||||
@@ -100,13 +103,13 @@ export default class ChatViewModel {
|
|||||||
this.close();
|
this.close();
|
||||||
this.chatModel.delete();
|
this.chatModel.delete();
|
||||||
this.chatListViewModel.restoreChats();
|
this.chatListViewModel.restoreChats();
|
||||||
}
|
};
|
||||||
|
|
||||||
// messaging
|
// messaging
|
||||||
sendMessage = (): void => {
|
sendMessage = (): void => {
|
||||||
this.chatModel.sendMessage(this.composingMessage.value);
|
this.chatModel.sendMessage(this.composingMessage.value);
|
||||||
this.composingMessage.value = "";
|
this.composingMessage.value = "";
|
||||||
}
|
};
|
||||||
|
|
||||||
// restore
|
// restore
|
||||||
restorePageSelection = (): void => {
|
restorePageSelection = (): void => {
|
||||||
@@ -134,10 +137,12 @@ export default class ChatViewModel {
|
|||||||
constructor(
|
constructor(
|
||||||
chatModel: ChatModel,
|
chatModel: ChatModel,
|
||||||
storageModel: StorageModel,
|
storageModel: StorageModel,
|
||||||
|
settingsViewModel: SettingsViewModel,
|
||||||
chatListViewModel: ChatListViewModel
|
chatListViewModel: ChatListViewModel
|
||||||
) {
|
) {
|
||||||
this.chatModel = chatModel;
|
this.chatModel = chatModel;
|
||||||
this.storageModel = storageModel;
|
this.storageModel = storageModel;
|
||||||
|
this.settingsViewModel = settingsViewModel;
|
||||||
this.chatListViewModel = chatListViewModel;
|
this.chatListViewModel = chatListViewModel;
|
||||||
|
|
||||||
this.primaryChannel.value = chatModel.info.primaryChannel;
|
this.primaryChannel.value = chatModel.info.primaryChannel;
|
||||||
@@ -149,6 +154,11 @@ export default class ChatViewModel {
|
|||||||
() => this.encryptionKeyInput.value == this.chatModel.info.encryptionKey
|
() => this.encryptionKeyInput.value == this.chatModel.info.encryptionKey
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.cannotSendMessage = React.createProxyState(
|
||||||
|
[this.settingsViewModel.username],
|
||||||
|
() => this.settingsViewModel.username.value == ""
|
||||||
|
);
|
||||||
|
|
||||||
this.color.value = chatModel.color;
|
this.color.value = chatModel.color;
|
||||||
|
|
||||||
this.restoreSecondaryChannels();
|
this.restoreSecondaryChannels();
|
||||||
|
|||||||
@@ -6,21 +6,23 @@ export default class SettingsViewModel {
|
|||||||
settingsModel: SettingsModel;
|
settingsModel: SettingsModel;
|
||||||
|
|
||||||
// state
|
// state
|
||||||
nameInput: React.State<string> = new React.State("");
|
username: React.State<string> = new React.State("");
|
||||||
|
usernameInput: React.State<string> = new React.State("");
|
||||||
firstDayOfWeekInput: React.State<number> = new React.State(0);
|
firstDayOfWeekInput: React.State<number> = new React.State(0);
|
||||||
|
|
||||||
// guards
|
// guards
|
||||||
cannotSetName: React.State<boolean> = React.createProxyState(
|
cannotSetName: React.State<boolean> = React.createProxyState(
|
||||||
[this.nameInput],
|
[this.usernameInput],
|
||||||
() =>
|
() =>
|
||||||
this.nameInput.value == "" ||
|
this.usernameInput.value == "" ||
|
||||||
this.nameInput.value == this.settingsModel.username
|
this.usernameInput.value == this.settingsModel.username
|
||||||
);
|
);
|
||||||
|
|
||||||
// set
|
// set
|
||||||
setName = (): void => {
|
setName = (): void => {
|
||||||
this.settingsModel.setName(this.nameInput.value);
|
this.settingsModel.setName(this.usernameInput.value);
|
||||||
this.nameInput.callSubscriptions();
|
this.username.value = this.settingsModel.username;
|
||||||
|
this.usernameInput.callSubscriptions();
|
||||||
};
|
};
|
||||||
|
|
||||||
setFirstDayofWeek = (): void => {
|
setFirstDayofWeek = (): void => {
|
||||||
@@ -31,7 +33,8 @@ export default class SettingsViewModel {
|
|||||||
constructor(settingsModel: SettingsModel) {
|
constructor(settingsModel: SettingsModel) {
|
||||||
this.settingsModel = settingsModel;
|
this.settingsModel = settingsModel;
|
||||||
|
|
||||||
this.nameInput.value = settingsModel.username;
|
this.username.value = settingsModel.username;
|
||||||
|
this.usernameInput.value = settingsModel.username;
|
||||||
this.firstDayOfWeekInput.value = settingsModel.firstDayOfWeek;
|
this.firstDayOfWeekInput.value = settingsModel.firstDayOfWeek;
|
||||||
|
|
||||||
// subscriptions
|
// subscriptions
|
||||||
|
|||||||
+5
-1
@@ -29,7 +29,11 @@ storageModel.print();
|
|||||||
// viewModels
|
// viewModels
|
||||||
const settingsViewModel = new SettingsViewModel(settingsModel);
|
const settingsViewModel = new SettingsViewModel(settingsModel);
|
||||||
const connectionViewModel = new ConnectionViewModel(connectionModel);
|
const connectionViewModel = new ConnectionViewModel(connectionModel);
|
||||||
const chatListViewModel = new ChatListViewModel(chatListModel, storageModel);
|
const chatListViewModel = new ChatListViewModel(
|
||||||
|
chatListModel,
|
||||||
|
storageModel,
|
||||||
|
settingsViewModel
|
||||||
|
);
|
||||||
|
|
||||||
// view
|
// view
|
||||||
document.body.append(<div id="background"></div>);
|
document.body.append(<div id="background"></div>);
|
||||||
|
|||||||
Reference in New Issue
Block a user