ahh shit
This commit is contained in:
110
Pages/Chat.razor
110
Pages/Chat.razor
@@ -1,13 +1,119 @@
|
||||
@page "/chat/{room?}"
|
||||
@using Microsoft.AspNetCore.SignalR.Client;
|
||||
@using LiveChat.Models;
|
||||
@inject NavigationManager Navigation
|
||||
|
||||
<PageTitle>Chat room</PageTitle>
|
||||
|
||||
<PageTitle>Chat room </PageTitle>
|
||||
|
||||
@if(Room is null or "")
|
||||
{
|
||||
<p class="alert alert-info"><i class="bi bi-exclamation-circle-fill"></i> Ingen rom ble avgitt. Lag et rom og putt det i URLen.</p>
|
||||
<p class="alert alert-info"><i class="bi bi-exclamation-circle-fill"></i> Fant ikke rom i URL. Lag et rom og putt det i URLen.</p>
|
||||
}
|
||||
|
||||
<h1>Rom @Room</h1>
|
||||
|
||||
<p>Din tilkoblings ID: @connectionId. Brukernavn: @username</p>
|
||||
|
||||
@foreach (var msg in msgs)
|
||||
{
|
||||
<div class="message">@msg</div>
|
||||
}
|
||||
|
||||
<div class="messageFormContainer">
|
||||
@if (messageModel == null)
|
||||
{
|
||||
<p>laster inn meldingsboks</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<EditForm Model="@messageModel" OnValidSubmit="@HandleSubmit" class="MessageForm">
|
||||
<DataAnnotationsValidator />
|
||||
<div class="MessageFormValidation">
|
||||
<ValidationSummary />
|
||||
</div>
|
||||
|
||||
<div class="MessageFormInputs">
|
||||
<input @bind-value="messageModel.Message" placeholder="Skriv inn din melding..." class="MessageFormMessage" />
|
||||
<button type="submit" class="MessageFormButton"><i class="bi bi-send-fill"></i></button>
|
||||
</div>
|
||||
</EditForm>
|
||||
}
|
||||
</div>
|
||||
|
||||
@code {
|
||||
[Parameter]
|
||||
public string? Room { get; set; }
|
||||
|
||||
private HubConnection? hubConnection;
|
||||
|
||||
private List<string> msgs = new List<string>();
|
||||
|
||||
private bool ErViInne = false;
|
||||
|
||||
private string connectionId = "";
|
||||
|
||||
private string username = "";
|
||||
|
||||
private MessageModel messageModel = new MessageModel() { User = "Something", Message = "" };
|
||||
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
hubConnection = new HubConnectionBuilder()
|
||||
.WithUrl(Navigation.ToAbsoluteUri("/chathub"))
|
||||
.Build();
|
||||
|
||||
await hubConnection.StartAsync();
|
||||
|
||||
// lag tilfeldig navn
|
||||
username = "Bruker" + new Random().Next(1, 1000);
|
||||
|
||||
if (Room != null || Room != "")
|
||||
{
|
||||
// sjekk om rom finnes
|
||||
// hvis ikke, lag et rom
|
||||
using (var db = new ChattingContext())
|
||||
{
|
||||
var room = db.Rooms.FirstOrDefault(r => r.RoomJoinCode == Room);
|
||||
if (room is null)
|
||||
{
|
||||
Console.WriteLine("Lager rom?!?");
|
||||
room = new Room() { RoomJoinCode = Room };
|
||||
db.Rooms.Add(room);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// bli med i rom
|
||||
await hubConnection.SendAsync("JoinRoom", Room, username);
|
||||
ErViInne = true;
|
||||
messageModel.User = username;
|
||||
|
||||
hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
|
||||
{
|
||||
var encodedMsg = $"{user}: {message}";
|
||||
msgs.Add(encodedMsg);
|
||||
InvokeAsync(StateHasChanged);
|
||||
});
|
||||
}
|
||||
|
||||
private async Task HandleSubmit()
|
||||
{
|
||||
messageModel.User = username;
|
||||
await hubConnection.SendAsync("SendMessageRoom", messageModel.User, messageModel.Message, Room);
|
||||
}
|
||||
|
||||
public bool IsConnected =>
|
||||
hubConnection?.State == HubConnectionState.Connected;
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (hubConnection is not null)
|
||||
{
|
||||
await hubConnection.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,11 +12,11 @@
|
||||
<div hidden="@(!ModalIsVisible)">
|
||||
<EditForm Model="@userModel" OnValidSubmit="@SetUser">
|
||||
<DataAnnotationsValidator />
|
||||
<ValidationSummary />
|
||||
|
||||
<div class="giveNameMessage">
|
||||
<h1>Skriv inn navnet ditt</h1>
|
||||
<p>Dette kan ikke endres (helt til du oppdaterer sida)</p>
|
||||
<ValidationSummary />
|
||||
<input @bind-value="userModel.User" disabled="@(!ModalIsVisible)" placeholder="Navn" class="MessageFormName" />
|
||||
<button type="submit" disabled="@(!IsConnected)" class="MessageFormButton"><i class="bi bi-send-fill"></i> Sett navn</button>
|
||||
</div>
|
||||
@@ -101,6 +101,7 @@ else
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
|
||||
hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
|
||||
{
|
||||
var encodedMsg = $"{user}: {message}";
|
||||
|
||||
Reference in New Issue
Block a user