Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/new add edit volunteer #337

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
@layout Layout.AdminLayout
@page "/administration/admintasks/addeditvolunteer/{LocationId:int}"
@page "/administration/admintasks/addeditvolunteer/{LocationId:int}/{VolunteerId:int}"
@using BedBrigade.Common.Models
@attribute [Authorize(Roles = RoleNames.CanManageVolunteers)]

@if (Model == null)
{
<Spinner />
}
else
{
<div class="container my-5 ">
@if (VolunteerId == null)
{
<h3>Add Volunteer</h3>
}
else
{
<h3>Edit Volunteer</h3>
}

<EditForm Model="@Model" OnValidSubmit="HandleValidSubmit">
<DataAnnotationsValidator/>

<div class="row">

<div class="col-md-6">
<div class="card">
<div class="card-header" style="text-align: center">
Volunteer Information
</div>
<div class="card-body">

<div class="form-group mb-3">
<label for="locationDropDown">Location</label>
<SfDropDownList id="locationDropDown" TItem="Location" TValue="Int32" DataSource="Locations" @bind-Value=@Model.LocationId Enabled="@CanSetLocation" Placeholder="Location" FloatLabelType="FloatLabelType.Auto">
<DropDownListFieldSettings Text="Name" Value="LocationId"/>
</SfDropDownList>
<div style="font-size: smaller">
<ValidationMessage For="@(() => Model.LocationId)"/>
</div>
</div>

<div class="form-group mb-3">
<label for="firstname" class="formlabel required">First Name</label>
<SfTextBox ID="firstname" @bind-Value="Model.FirstName"></SfTextBox>
<div style="font-size: smaller">
<ValidationMessage For="@(() => Model.FirstName)"></ValidationMessage>
</div>
</div>

<div class="form-group mb-3">
<label for="lastName" class="formlabel required">Last Name</label>
<SfTextBox ID="lastName" @bind-Value="Model.LastName"></SfTextBox>
<div style="font-size: smaller">
<ValidationMessage For="@(() => Model.LastName)"></ValidationMessage>
</div>
</div>

<div class="form-group mb-3">
<label for="email" class="formlabel required">Email</label>
<SfTextBox ID="email" @bind-Value="Model.Email"></SfTextBox>
<div style="font-size: smaller">
<ValidationMessage For="@(() => Model.Email)"></ValidationMessage>
</div>
</div>

<div class="form-group mb-3">
<label for="phone">Phone</label>
<SfMaskedTextBox Mask='(000) 000-0000' ID="phone" @bind-Value="Model.Phone"></SfMaskedTextBox>
<div style="font-size: smaller">
<ValidationMessage For="@(() => Model.Phone)"></ValidationMessage>
</div>
</div>

<div class="form-group">
<label for="email" class="formlabel">Organization or Group</label>
<SfTextBox ID="email" @bind-Value="Model.OrganizationOrGroup"></SfTextBox>
</div>
<div class="form-group">
<label for="inst" class="formlabel">Message</label>
<SfTextBox ID="inst" @bind-Value="Model.Message" Multiline="true" HtmlAttributes="DescriptionHtmlAttribute"></SfTextBox>
</div>
</div>
</div>
</div>

<div class="col-md-6">
<div class="card">
<div class="card-header" style="text-align: center">
Delivery Vehicle
</div>
<div class="card-body">
<div class="control-section">
<div class="radio-control">
@BootstrapHelper.GetBootstrapMessage("help", "Do you have a vehicle that can carry a bed and mattress?", "", false, "compact")
<div class="row">
<SfRadioButton Label="@EnumHelper.GetEnumDescription(VehicleType.NoCar)" Name="RadioBtn" Value="@VehicleType.NoCar.ToString()" @bind-checked="Model.VehicleType" TChecked="VehicleType"></SfRadioButton>
</div>
<hr />
<div class="row">
<SfRadioButton Label="I have a Minvan with seats removed or folded into the floor" Name="RadioBtn" Value="@VehicleType.Minivan.ToString()" @bind-checked="Model.VehicleType" TChecked="VehicleType"></SfRadioButton>
</div>
<hr />
<div class="row">
<SfRadioButton Label="I have a large SUV (Chevrolet Suburban, Chevrolet Tahoe, Ford Expedition, GMC Yukon, Toyota Sequoia, Nissan Armada, etc.)" Name="RadioBtn" Value="@VehicleType.SUV.ToString()" @bind-checked="Model.VehicleType" TChecked="VehicleType"></SfRadioButton>
</div>
<hr />
<div class="row">
<SfRadioButton Label="I have a full size pickup truck with tie down straps or a cap" Value="@VehicleType.Truck.ToString()" Name="RadioBtn" @bind-checked="Model.VehicleType" TChecked="VehicleType"></SfRadioButton>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

@if (!string.IsNullOrEmpty(ErrorMessage))
{
<div class="alert alert-danger" role="alert">
@ErrorMessage
</div>
}

<button type="submit" class="btn btn-primary mt-5">Save</button>
<button type="button" class="btn btn-secondary mt-5" @onclick="HandleCancel">Cancel</button>
</EditForm>
</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
using System.Drawing;
using BedBrigade.Data.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using System.Security.Claims;
using BedBrigade.Common.Constants;
using BedBrigade.Common.Logic;
using BedBrigade.Common.Models;
using Serilog;
using Microsoft.AspNetCore.Components.Forms;
using System.Net;

namespace BedBrigade.Client.Components.Pages.Administration.AdminTasks
{
public partial class AddEditVolunteer : ComponentBase
{
[Parameter] public int? VolunteerId { get; set; }
[Parameter] public int? LocationId { get; set; }

[Inject] private NavigationManager? _navigationManager { get; set; }
[Inject] private ToastService _toastService { get; set; }
[Inject] private IVolunteerDataService _volunteerDataService { get; set; }
[Inject] private ILocationDataService? _svcLocation { get; set; }
[Inject] private AuthenticationStateProvider? _authState { get; set; }
private ClaimsPrincipal? Identity { get; set; }
public string ErrorMessage { get; set; }
public Volunteer? Model { get; set; }
private const string ErrorTitle = "Error";
protected List<Location>? Locations { get; private set; }
public bool CanSetLocation = false;
protected Dictionary<string, object> DescriptionHtmlAttribute { get; set; } = new Dictionary<string, object>()
{
{ "rows", "5" },
};


protected override async Task OnInitializedAsync()
{
var authState = await _authState.GetAuthenticationStateAsync();
Identity = authState.User;

var userName = Identity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value ?? Defaults.DefaultUserNameAndEmail;
Log.Information($"{userName} went to the Add/Edit Volunteer Page");

if (Identity.IsInRole(RoleNames.NationalAdmin))
{
CanSetLocation = true;
}

await LoadLocations();
await LoadVolunteer();
}

private async Task LoadLocations()
{
var result = await _svcLocation!.GetAllAsync();

if (result.Success) //
{
Locations = result.Data;
}
else
{
_toastService.Error(ErrorTitle, result.Message);
}
}

private async Task LoadVolunteer()
{
if (VolunteerId != null)
{
var result = await _volunteerDataService.GetByIdAsync(VolunteerId.Value);

if (result.Success)
{
Model = result.Data;
}
else
{
_toastService.Error(ErrorTitle, result.Message);
}
}
else
{
Model = new Volunteer();
Model.LocationId = LocationId.Value;
}
}

private async Task HandleValidSubmit()
{
if (IsValid() && await SaveVolunteer())
{
_navigationManager.NavigateTo("/administration/manage/volunteers");
}
}

private bool IsValid()
{
ErrorMessage = string.Empty;
bool isPhoneValid = Validation.IsValidPhoneNumber(Model.Phone);

if (!isPhoneValid)
{
ErrorMessage = "Phone numbers must be 10 digits with a valid area code and prefix.";
return false;
}

var emailResult = Validation.IsValidEmail(Model.Email);
if (!emailResult.IsValid)
{
ErrorMessage = emailResult.UserMessage;
return false;
}

return true;
}

private async Task<bool> SaveVolunteer()
{
if (VolunteerId != null)
{
var updateResult = await _volunteerDataService.UpdateAsync(Model!);
if (updateResult.Success)
{
_toastService.Success("Success", "Volunteer updated successfully");
return true;
}

_toastService.Error(ErrorTitle, updateResult.Message);
return false;
}

var existingVolunteerResult = await _volunteerDataService.GetByEmail(Model.Email);
if (existingVolunteerResult.Success)
{
ErrorMessage = "A volunteer with this email already exists.";
return false;
}

var result = await _volunteerDataService.CreateAsync(Model!);
if (result.Success)
{
_toastService.Success("Success", "Volunteer created successfully");
return true;
}

_toastService.Error(ErrorTitle, result.Message);
return false;
}

private void HandleCancel()
{
_navigationManager.NavigateTo("/administration/manage/volunteers");
}
}
}
Loading
Loading