Skip to content

Commit

Permalink
Merge pull request #29 from syonfoppen/feuture/syon-room-creation-page
Browse files Browse the repository at this point in the history
Feuture/syon room creation page
  • Loading branch information
syonfoppen committed Mar 29, 2024
2 parents b556517 + fa4f542 commit cd7d51c
Show file tree
Hide file tree
Showing 16 changed files with 657 additions and 39 deletions.
118 changes: 118 additions & 0 deletions src/Estiblazor.UI/Estiblazor.UI/Components/Pages/CreateRoom.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
@page "/createroom"
@rendermode InteractiveServer

@using Estiblazor.UI.Services.Rooms
@using System.Collections.ObjectModel
@using Estiblazor.UI.Enums

@inject IRoomCreationService RoomCreationService
@inject NavigationManager nav

<div class="flex-wrapper">
<div class="flex-header base">
<h1>Room: </h1>

<InputSelect @bind-Value="selectedTemplate" @bind-Value:after="SetTemplate">
<option selected value="">Select a template</option>
@foreach (var template in Enum.GetValues(typeof(RoomTemplates)))
{
<option value="@template">@template?.ToString()?.Replace("_", " ")</option>
}
</InputSelect>
</div>
<div class="flex-middle">
<div class="add-stage-wrapper base">
<div class="add-stage-overview">
<h2>Stages</h2>
<div class="stages">
@foreach (var stage in RoomCreationService.GetStages())
{
<div @onclick="() => RoomCreationService.SetSelectedStage(stage.Key)" class="stage @(RoomCreationService.IsSelectedStage(stage.Key) ? "selected" : "")">@stage.Value</div>
}
</div>

</div>

<div class="add-stage-input">
<InputText @bind-Value="NewStageName" class="form-control" placeholder="Stage name" />
<a class="add-button" @onclick="AddStage">Add Stage</a>
</div>
</div>
<div class="choice-wrapper">
<div class="choice-overview base">
@{
var selectedStage1 = RoomCreationService.GetSelectedStage();
}
@if (selectedStage1 != null)
{

<h3>@selectedStage1.StageName Choices</h3>

<div class="choices">

@foreach (var choice in selectedStage1.AvailableChoices)
{
<div class="choice" @oncontextmenu="() => RoomCreationService.RemoveChoice(choice.ChoiceId)" @oncontextmenu:preventDefault="true">@((MarkupString)choice.ChoiceName)</div>
}

</div>

}
else
{
<span>No stage selected</span>
}

</div>
<div class="choice-input base">
<div> <InputText @bind-Value="NewChoiceName" class="form-control" placeholder="Choice name" /> </div>
<div class="button-wrapper">
<div> <a class="add-button" @onclick="AddAvailableChoice">Add Choice</a> </div>
<div> <a class="add-button" @onclick="CreateRoomAndNavigate">Create Room</a> </div>
</div>
</div>
</div>
</div>

</div>



@code {
public string? NewStageName { get; set; }

public string? NewChoiceName { get; set; }

public RoomTemplates? selectedTemplate { get; set; }

public void AddStage()
{
if (!string.IsNullOrEmpty(NewStageName))
{
RoomCreationService.AddNewStage(NewStageName);
}
}

public void AddAvailableChoice()
{
if (RoomCreationService.GetSelectedStage != null && !string.IsNullOrEmpty(NewChoiceName))
{
RoomCreationService.AddNewChoice(NewChoiceName);
NewChoiceName = string.Empty;
}
}

public void SetTemplate()
{
if (selectedTemplate != null)
{
RoomCreationService.SetRoomTemplate(selectedTemplate.Value);
}
}

public void CreateRoomAndNavigate()
{
var roomId = RoomCreationService.CreateRoom();
nav.NavigateTo($"/Rooms/{roomId}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</div>


@foreach (var user in Room.Users)
@foreach (var user in Room.Users.OrderBy(o => o.Name))
{
<UserChoicesView @key=@user.Id RoomName="@Room.Name" UserId="@user.Id" />
}
Expand Down
9 changes: 9 additions & 0 deletions src/Estiblazor.UI/Estiblazor.UI/Enums/RoomTemplates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Estiblazor.UI.Enums
{
public enum RoomTemplates
{
Planning_Poker,
One_till_ten,
LIKE_DISLIKE
}
}
1 change: 1 addition & 0 deletions src/Estiblazor.UI/Estiblazor.UI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static void Main(string[] args)
.AddHttpContextAccessor()
.AddSingleton<IRoomCollection, RoomCollection>()
.AddSingleton<IUserCollection, UserCollection>()
.AddScoped<IRoomCreationService, RoomCreationService>()
.AddScoped<IUserProvider, LocalStorageUserProvider>()
.AddScoped<IsOnlineThingy>()
.AddBlazoredLocalStorage()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Estiblazor.UI.Services.Rooms
{
public class AddAvailableChoiceViewModel
{
public Guid ChoiceId { get; set; } = Guid.NewGuid();
public string ChoiceName { get; set; } = "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Reveal()
public void Reset()
{
IsRevealed = false;
while(userChoices.Count > 0)
while (userChoices.Count > 0)
{
RemoveChoice(userChoices[^1]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public interface IRoomCollection
RoomViewModel GetOrCreateRoom(string roomid);
RoomViewModel? GetExistingRoom(string roomId);
List<string> GetRoomNames();
void AddNewRoom(RoomViewModel vm);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Estiblazor.UI.Enums;

namespace Estiblazor.UI.Services.Rooms
{
public interface IRoomCreationService
{
void SetRoomTemplate(RoomTemplates roomTemplate);
NewStageModel? GetSelectedStage();

IEnumerable<KeyValuePair<Guid, string>> GetStages();
void AddNewStage(string stageName);
void AddNewChoice(string choice);
void RemoveChoice(Guid choiceId);
bool IsSelectedStage(Guid stageId);
void SetSelectedStage(Guid stageId);
string CreateRoom();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Estiblazor.UI.Services.Rooms
{
public class NewRoomModel
{
public ICollection<NewStageModel> Stages { get; set; } = [];
}
}
11 changes: 11 additions & 0 deletions src/Estiblazor.UI/Estiblazor.UI/Services/Rooms/NewStageModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Estiblazor.UI.Services.Rooms
{
public class NewStageModel
{
public Guid StageId { get; set; } = Guid.NewGuid();
public string StageName { get; set; } = "";
public bool IsSelected { get; set; } = false;

public ICollection<AddAvailableChoiceViewModel> AvailableChoices { get; set; } = [];
}
}
76 changes: 45 additions & 31 deletions src/Estiblazor.UI/Estiblazor.UI/Services/Rooms/RoomCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,59 @@ public RoomCollection(IMemoryCache memoryCache)
this.memoryCache = memoryCache;
}

private readonly HashSet<string> roomNames = new();
private readonly HashSet<string> roomNames = [];

private sealed record class RoomKey(string RoomId)
{
}

public List<string> GetRoomNames() => roomNames.ToList();

private readonly IMemoryCache memoryCache;

RoomViewModel IRoomCollection.GetOrCreateRoom(string roomid)
{
var vm = memoryCache.GetOrCreate(new RoomKey(roomid), cacheEntry =>
var vm = memoryCache.GetOrCreate(new RoomId(roomid), cacheEntry =>
{
var vm = new RoomViewModel { Id = new RoomId(roomid), Name = roomid };
vm.EstimationStages.Add(new EstimationStage
{
Name = "effort",
AvailableChoices = ["0.5", "1", "2", "3", "5", "8", "13", "20", "<i class=\"fa-solid fa-infinity\"></i>"],
IsRevealed = false,
UserChoices = [],
});
vm.EstimationStages.Add(new EstimationStage
{
Name = "complexity",
AvailableChoices = ["S", "M", "L", "<i class=\"fa-solid fa-infinity\"></i>"],
IsRevealed = false,
UserChoices = []
});
vm.EstimationStages.Add(new EstimationStage
{
Name = "Like",
AvailableChoices = ["<i class=\"fa-solid fa-thumbs-up\"></i>", "<i class=\"fa-solid fa-thumbs-down\"></i>"],
IsRevealed = false,
UserChoices = []
});
List<EstimationStage> estimationStages =
[
new EstimationStage
{
Name = "effort",
AvailableChoices = ["0.5", "1", "2", "3", "5", "8", "13", "20", "<i class=\"fa-solid fa-infinity\"></i>"],
IsRevealed = false,
UserChoices = [],
},
new EstimationStage
{
Name = "complexity",
AvailableChoices = ["S", "M", "L", "<i class=\"fa-solid fa-infinity\"></i>"],
IsRevealed = false,
UserChoices = []
},
new EstimationStage
{
Name = "Like",
AvailableChoices = ["<i class=\"fa-solid fa-thumbs-up\"></i>", "<i class=\"fas fa-meh\"></i>", "<i class=\"fa-solid fa-thumbs-down\"></i>"],
IsRevealed = false,
UserChoices = []
},
];
var vm = new RoomViewModel(estimationStages) { Id = new RoomId(roomid), Name = roomid };
OnCreate(roomid);
cacheEntry.SlidingExpiration = TimeSpan.FromDays(7);
cacheEntry.RegisterPostEvictionCallback(OnEvict);
ConfigureCacheEntry(cacheEntry);
return vm;
});

return vm!;
}

private void ConfigureCacheEntry(ICacheEntry cacheEntry)
{
cacheEntry.SlidingExpiration = TimeSpan.FromDays(7);
cacheEntry.RegisterPostEvictionCallback(OnEvict);
}

private void OnCreate(string name)
{
roomNames.Add(name);
Expand All @@ -70,11 +77,18 @@ private void OnEvict(object key, object? value, EvictionReason reason, object? s

RoomViewModel? IRoomCollection.GetExistingRoom(string roomId)
{
var vm = memoryCache.Get<RoomViewModel>(new RoomKey(roomId));
var vm = memoryCache.Get<RoomViewModel>(new RoomId(roomId));

return vm;
}

public void AddNewRoom(RoomViewModel vm)
{
using var entry = memoryCache.CreateEntry(new RoomId(vm.Id.Name));
entry.SetValue(vm);
OnCreate(vm.Id.Name);
ConfigureCacheEntry(entry);
}
}

}

0 comments on commit cd7d51c

Please sign in to comment.