Lieb-Website/Lieb/Pages/Raids/RaidTemplateEdit.razor
2022-03-13 14:57:48 +01:00

254 lines
No EOL
7.2 KiB
Text

@page "/raidtemplateedit"
@page "/raidtemplateedit/{raidId}"
@using Lieb.Data
@using Lieb.Models
@using Lieb.Models.GuildWars2.Raid
@using System.ComponentModel.DataAnnotations
@inject RaidTemplateService RaidTemplateService
@inject UserService UserService
@inject TimeZoneService TimeZoneService
@inject NavigationManager NavigationManager
@inject IJSRuntime JsRuntime
<h3>CreateRaid</h3>
<AuthorizeView Policy="@Constants.Roles.RaidLead" Context="authorizationContext">
<EditForm Model="@_template" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
@{
bool _isEdit = _template.RaidTemplateId != 0;
}
<p>
<label>
Title:
<InputText @bind-Value="_template.Title" />
</label>
</p>
<p>
<label>
Description:
<InputTextArea @bind-Value="_template.Description" />
</label>
</p>
<p>
<label>
Raid Type:
<InputSelect @bind-Value="_template.RaidType" disabled="@_isEdit">
<option value="@RaidType.Planned">Planned</option>
<option value="@RaidType.RandomWithBoons">Random with boons covred</option>
<option value="@RaidType.RandomClasses">Random classes</option>
<option value="@RaidType.RandomEliteSpecialization">Random elite specializations</option>
</InputSelect>
</label>
</p>
<p>
<label>
Date:
<InputDate @bind-Value="_raidDate" />
</label>
</p>
<p>
<label>
Start Time:
<input type="time" @bind="_startTime" />
</label>
</p>
<p>
<label>
End Time:
<input type="time" @bind="_endTime" />
</label>
</p>
<p>
<label>
Time Zone:
<InputSelect @bind-Value="_template.RequiredRole">
<option value="@_template.TimeZone">@_template.TimeZone</option>
@if(_userTimeZone != @_template.TimeZone)
{
<option value="@_userTimeZone">@_userTimeZone</option>
}
</InputSelect>
</label>
</p>
<p>
<label>
Required Role:
<InputSelect @bind-Value="_template.RequiredRole">
<option value="">Not Locked</option>
@foreach(LiebRole role in UserService.GetLiebRoles())
{
if (!role.IsSystemRole)
{
<option value="@role.RoleName">@role.RoleName</option>
}
}
</InputSelect>
</label>
</p>
<p>
<label>
Free for all date:
<InputDate @bind-Value="_freeForAllDate" />
</label>
</p>
<p>
<label>
Free for all time:
<input type="time" @bind="_freeForAllTime" />
</label>
</p>
<p>
<label>
Organizer:
<InputText @bind-Value="_template.Organizer" />
</label>
</p>
<p>
<label>
Guild:
<InputText @bind-Value="_template.Guild" />
</label>
</p>
<p>
<label>
Voice chat:
<InputText @bind-Value="_template.VoiceChat" />
</label>
</p>
@if(_template.RaidType == RaidType.Planned)
{
<p>
<label>
Roles:
</label>
<button type=button @onclick="() => AddRoleClicked()">Add role</button>
<table>
<tr>
<th>Spots</th>
<th>Role name</th>
<th>Description</th>
</tr>
@foreach( PlannedRaidRole role in _template.Roles)
{
<tr>
<td><InputNumber @bind-Value="role.Spots" /></td>
<td><InputText @bind-Value="role.Name" /></td>
<td><InputText @bind-Value="role.Description" /></td>
<td><button type=button @onclick="() => DeleteRoleClicked(role)">Delete</button></td>
</tr>
}
</table>
</p>
}
<ValidationSummary />
<label class="validation-message" >@_errorMessage</label>
<button type="submit">Submit</button>
</EditForm>
<br/>
<button type=button @onclick="() => DeleteRaidClicked()">Delete Raid</button>
</AuthorizeView>
@code {
[Parameter]
public string raidId { get; set; }
public RaidTemplate _template;
private string _errorMessage = string.Empty;
private DateTimeOffset _raidDate = DateTime.Now.Date;
private DateTimeOffset _startTime;
private DateTimeOffset _endTime;
private DateTimeOffset _freeForAllDate = DateTime.Now.Date;
private DateTimeOffset _freeForAllTime;
private string _userTimeZone = string.Empty;
protected override async Task OnInitializedAsync()
{
if(!string.IsNullOrEmpty(raidId) && int.TryParse(raidId, out int parsedId))
{
_template = RaidTemplateService.GetTemplate(parsedId);
_startTime = _template.StartTime;
_endTime = _template.EndTime;
_raidDate = _startTime.Date;
_freeForAllTime = _template.FreeForAllTime;
_freeForAllDate = _freeForAllTime.Date;
}
else
{
_template = new RaidTemplate();
}
_userTimeZone = await TimeZoneService.GetUserTimeZone();
}
async Task AddRoleClicked()
{
_template.Roles.Add(new PlannedRaidRole());
}
async Task DeleteRoleClicked(PlannedRaidRole role)
{
_template.Roles.Remove(role);
}
async Task DeleteRaidClicked()
{
bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Are you sure you want to delete the raid?");
if (confirmed)
{
await RaidTemplateService.DeleteTemplate(_template.RaidTemplateId);
NavigationManager.NavigateTo("raidoverview");
}
}
private async Task HandleValidSubmit()
{
if(_template.RaidType != RaidType.Planned)
{
_template.Roles.Clear();
_template.Roles.Add(new PlannedRaidRole()
{
Spots = 10,
Name = "Random",
Description = _template.RaidType.ToString()
});
}
if(_template.Roles.Count == 0)
{
_errorMessage = "Roles are needed for a raid.";
return;
}
_template.TimeZone = await TimeZoneService.GetUserTimeZone();
_template.StartTime =_raidDate.Date + _startTime.TimeOfDay;
if(_startTime.TimeOfDay > _endTime.TimeOfDay)
{
_template.EndTime = _raidDate.Date + _endTime.TimeOfDay;
}
else
{
_template.EndTime = _raidDate.Date.AddDays(1) + _endTime.TimeOfDay;
}
_template.FreeForAllTime = _freeForAllDate.Date + _freeForAllTime.TimeOfDay;
await RaidTemplateService.AddOrEditTemplate(_template);
NavigationManager.NavigateTo("raidoverview");
}
}