Spaces:
Running
Running
File size: 23,289 Bytes
3418204 33d40ba 3418204 72f2be7 3418204 8c88a89 3418204 8c88a89 3418204 8c88a89 3418204 8c88a89 3418204 8c88a89 3418204 8c88a89 3418204 8c88a89 3418204 bec15e2 3418204 8c88a89 3418204 8c88a89 3418204 f6d34ff 3418204 8c88a89 3418204 bec15e2 3418204 8c88a89 3418204 8c88a89 3418204 8c88a89 3418204 fa69bf0 8c88a89 fa69bf0 8c88a89 fa69bf0 3418204 8c88a89 3418204 8c88a89 3418204 8c88a89 3418204 8c88a89 3418204 8c88a89 3418204 72f2be7 8c88a89 72f2be7 3418204 8c88a89 3418204 72f2be7 3418204 3bd1468 fa69bf0 3418204 72f2be7 fa69bf0 72f2be7 3418204 3bd1468 fa69bf0 3bd1468 fa69bf0 3bd1468 fa69bf0 3418204 3bd1468 fa69bf0 3418204 fa69bf0 3418204 fa69bf0 3418204 121df90 3418204 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | @page "/projects/assign"
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using TaskTrackingSystem.Shared.Models.Project
@using TaskTrackingSystem.Shared.Models.Role
@rendermode @(new InteractiveServerRenderMode(prerender: false))
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
@inject ApiClientService ApiClient
@inject IJSRuntime JS
@inject NavigationManager Navigation
@inject AuthenticationStateProvider AuthStateProvider
@inject MenuAuthorizationService MenuAuthorization
<PageTitle>@AppLocalization.PageTitle("projectAssignment", "Project Assignment")</PageTitle>
<SuccessAlert Message="@successMessage" />
<div class="space-y-6">
<!-- Page Header -->
<div class="flex items-center justify-between">
<div>
<h2 class="text-3xl font-bold tracking-tight text-slate-900">@AppLocalization.PageTitle("projectAssignment", "Project Assignment")</h2>
<p class="text-slate-500 mt-1">@AppLocalization.Text("project.assignDescription", "Assign team members to projects to control their task visibility.")</p>
</div>
</div>
@if (isLoading)
{
<LoadingSpinner Message="@AppLocalization.Text("common.loadingProjectsAndUsers", "Loading projects and users...")" />
}
else
{
<div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
<!-- Left panel: Projects list -->
<div class="lg:col-span-1 rounded-xl border border-slate-200 bg-white shadow-sm overflow-hidden flex flex-col h-[600px]">
<div class="p-4 border-b border-slate-100 bg-slate-50/60">
<h3 class="font-bold text-slate-800 text-sm mb-3">@AppLocalization.Text("common.selectProject", "Select Project")</h3>
<div class="relative flex gap-2">
<div class="relative flex-1">
<input @bind="projectSearchInput" type="text" placeholder="@AppLocalization.Text("common.searchProjects", "Search projects...")"
class="w-full rounded-lg border border-slate-200 pl-9 pr-3 py-1.5 text-xs text-slate-900 placeholder:text-slate-400 focus:outline-none focus:ring-2 focus:ring-violet-600 focus:border-transparent transition-all" />
<i data-lucide="search" class="absolute left-3 top-2.5 h-3.5 w-3.5 text-slate-400"></i>
</div>
<button @onclick="ApplyProjectSearch" class="inline-flex items-center rounded-lg bg-violet-600 px-3 py-1.5 text-xs font-medium text-white hover:bg-violet-700 transition-colors shrink-0">
<i data-lucide="search" class="h-3.5 w-3.5 mr-1.5"></i> @AppLocalization.Text("action.search", "Search")
</button>
<button @onclick="ResetProjectSearch" class="inline-flex items-center rounded-lg border border-slate-200 bg-white px-3 py-1.5 text-xs font-medium text-slate-700 hover:bg-slate-50 transition-colors shrink-0">
<i data-lucide="rotate-ccw" class="h-3.5 w-3.5 mr-1.5 text-slate-500"></i> @AppLocalization.Text("action.reset", "Reset")
</button>
</div>
</div>
<div class="flex-1 overflow-y-auto divide-y divide-slate-100">
@if (FilteredProjects.Any())
{
@foreach (var proj in FilteredProjects)
{
var isSelected = SelectedProject?.Id == proj.Id;
<button @onclick="() => SelectProject(proj)"
class="w-full text-left p-4 hover:bg-slate-50/60 transition-colors flex flex-col gap-1 @(isSelected ? "bg-violet-50/60 border-l-4 border-violet-600 pl-3" : "")">
<div class="flex items-center justify-between">
<span class="text-sm font-semibold text-slate-900 @(isSelected ? "text-violet-700" : "")">@proj.Name</span>
<span class="text-[10px] text-slate-400">@DisplayFormats.Date(proj.StartDate) - @DisplayFormats.Date(proj.EndDate)</span>
</div>
@if (!string.IsNullOrEmpty(proj.Description))
{
<p class="text-xs text-slate-500 line-clamp-1">@proj.Description</p>
}
</button>
}
}
else
{
<div class="p-8 text-center text-slate-400 text-xs">@AppLocalization.Text("common.noProjectsFound", "No projects found.")</div>
}
</div>
</div>
<!-- Right panel: Assignment controls -->
<div class="lg:col-span-2 rounded-xl border border-slate-200 bg-white shadow-sm flex flex-col h-[600px] overflow-hidden">
@if (SelectedProject == null)
{
<div class="flex-1 flex flex-col items-center justify-center p-8 text-center bg-slate-50/30">
<div class="h-12 w-12 rounded-full bg-slate-100 flex items-center justify-center text-slate-400 mb-3">
<i data-lucide="folder-kanban" class="h-6 w-6"></i>
</div>
<h4 class="text-sm font-bold text-slate-800">@AppLocalization.Text("common.noProjectSelected", "No Project Selected")</h4>
<p class="text-xs text-slate-500 mt-1 max-w-sm">@AppLocalization.Text("project.selectProjectPrompt", "Please select a project from the left panel to manage user assignments.")</p>
</div>
}
else
{
<!-- Selected Project Header -->
<div class="p-6 border-b border-slate-100 bg-slate-50 flex flex-col md:flex-row md:items-center justify-between gap-4">
<div>
<span class="text-[10px] font-bold text-violet-600 uppercase tracking-wider">@AppLocalization.Text("common.currentlyManaging", "Currently Managing")</span>
<h3 class="text-xl font-bold text-slate-900">@SelectedProject.Name</h3>
@if (!string.IsNullOrEmpty(SelectedProject.Description))
{
<p class="text-xs text-slate-500 mt-1">@SelectedProject.Description</p>
}
</div>
<div class="flex items-center gap-2 shrink-0">
<span class="text-xs text-slate-500 bg-slate-100 px-3 py-1 rounded-full border border-slate-200">
@DisplayFormats.Date(SelectedProject.StartDate) - @DisplayFormats.Date(SelectedProject.EndDate)
</span>
</div>
</div>
<!-- Filter & Selection Stats -->
<div class="px-6 py-3 border-b border-slate-100 bg-slate-50/30 flex flex-col sm:flex-row sm:items-center justify-between gap-3">
<div class="relative max-w-xs flex-1 flex gap-2">
<div class="relative flex-1">
<input @bind="userSearchInput" type="text" placeholder="@AppLocalization.Text("common.searchTeamMembers", "Search team members...")"
class="w-full rounded-lg border border-slate-200 pl-9 pr-3 py-1.5 text-xs text-slate-900 placeholder:text-slate-400 focus:outline-none focus:ring-2 focus:ring-violet-600 focus:border-transparent transition-all" />
<i data-lucide="search" class="absolute left-3 top-2.5 h-3.5 w-3.5 text-slate-400"></i>
</div>
<button @onclick="ApplyUserSearch" class="inline-flex items-center rounded-lg bg-violet-600 px-3 py-1.5 text-xs font-medium text-white hover:bg-violet-700 transition-colors shrink-0">
<i data-lucide="search" class="h-3.5 w-3.5 mr-1.5"></i> @AppLocalization.Text("action.search", "Search")
</button>
<button @onclick="ResetUserSearch" class="inline-flex items-center rounded-lg border border-slate-200 bg-white px-3 py-1.5 text-xs font-medium text-slate-700 hover:bg-slate-50 transition-colors shrink-0">
<i data-lucide="rotate-ccw" class="h-3.5 w-3.5 mr-1.5 text-slate-500"></i> @AppLocalization.Text("action.reset", "Reset")
</button>
</div>
@if (isAdminUser)
{
<div class="w-40 shrink-0">
<select @bind="selectedRoleId" class="w-full rounded-lg border border-slate-200 px-3 py-1.5 text-xs text-slate-900 focus:outline-none focus:ring-2 focus:ring-violet-600 focus:border-transparent transition-all">
<option value="0">@AppLocalization.Text("common.allRoles", "All Roles")</option>
@foreach (var r in roles)
{
<option value="@r.Id">@r.Name</option>
}
</select>
</div>
}
else
{
<div class="text-xs font-semibold text-violet-700 bg-violet-50 border border-violet-100 px-3 py-1.5 rounded-lg shrink-0">
@AppLocalization.Text("project.employeesOnly", "Employees only")
</div>
}
<div class="flex items-center gap-3 shrink-0">
<button @onclick="SelectAllUsers" class="text-xs font-semibold text-violet-600 hover:text-violet-800 transition-colors">@AppLocalization.Text("common.selectAll", "Select All")</button>
<span class="text-slate-300">|</span>
<button @onclick="DeselectAllUsers" class="text-xs font-semibold text-slate-600 hover:text-slate-800 transition-colors">@AppLocalization.Text("common.deselectAll", "Deselect All")</button>
<span class="text-slate-300">|</span>
<span class="text-xs font-bold text-slate-600">@SelectedUserIds.Count @AppLocalization.Text("common.selected", "selected")</span>
</div>
</div>
<!-- Users list checklist -->
<div class="flex-1 overflow-y-auto p-6 space-y-2">
@if (isLoadingMembers)
{
<div class="flex justify-center py-12">
<LoadingSpinner Message="@AppLocalization.Text("common.loadingMembers", "Loading members...")" />
</div>
}
else
{
@if (FilteredUsers.Any())
{
<div class="grid grid-cols-1 md:grid-cols-2 gap-3">
@foreach (var user in FilteredUsers)
{
var isChecked = SelectedUserIds.Contains(user.Id);
<label class="flex items-center gap-3 p-3 rounded-xl border border-slate-100 bg-white hover:bg-slate-50/50 hover:border-slate-200 cursor-pointer transition-all shadow-sm select-none @(isChecked ? "border-violet-100 bg-violet-50/10" : "")">
<input type="checkbox" checked="@isChecked"
@onchange="(e) => ToggleUserSelection(user.Id, (bool)(e.Value ?? false))"
class="h-4 w-4 rounded border-slate-300 accent-violet-600 focus:ring-violet-500" />
<span class="flex h-9 w-9 items-center justify-center rounded-full bg-violet-100 text-violet-700 text-xs font-bold shrink-0">
@(user.FirstName.Length > 0 ? user.FirstName[0].ToString().ToUpper() : "")@(user.LastName.Length > 0 ? user.LastName[0].ToString().ToUpper() : "")
</span>
<div class="min-w-0 flex-1">
<p class="text-sm font-semibold text-slate-800 truncate">@user.FirstName @user.LastName</p>
<p class="text-xs text-slate-400 truncate">@@@user.Username</p>
</div>
</label>
}
</div>
}
else
{
<div class="text-center text-slate-400 py-12 text-sm">@AppLocalization.Text("common.noTeamMembersFound", "No team members match your search criteria.")</div>
}
}
</div>
<!-- Action bar -->
<div class="p-6 border-t border-slate-100 bg-slate-50/50 flex flex-col sm:flex-row sm:items-center justify-between gap-4">
<div>
@if (!string.IsNullOrEmpty(statusMessage))
{
<p class="text-sm font-medium @(isError ? "text-red-600" : "text-emerald-700")">
<i data-lucide="@(isError ? "alert-circle" : "check-circle")" class="h-4 w-4 inline mr-1.5"></i>
@statusMessage
</p>
}
</div>
@if (canSaveAssignments)
{
<button @onclick="RequestSaveConfirmation" disabled="@(isSaving || isLoadingMembers)"
class="w-full sm:w-auto inline-flex items-center justify-center rounded-lg bg-violet-600 px-5 py-2.5 text-sm font-semibold text-white hover:bg-violet-700 disabled:opacity-50 transition-colors shadow-sm">
<i data-lucide="user-check" class="h-4 w-4 mr-2"></i>
@(isSaving ? AppLocalization.Text("common.loadingSpinner", "Loading...") : AppLocalization.Text("common.saveAssignmentChanges", "Save Assignment Changes"))
</button>
}
</div>
}
</div>
</div>
}
</div>
<ConfirmDialog IsVisible="showConfirmDialog"
Title="@AppLocalization.Text("project.saveAssignmentTitle", "Save Assignment?")"
Message="@AppLocalization.Text("project.saveAssignmentConfirm", "Are you sure you want to save these project member assignments?")"
ConfirmText="@AppLocalization.Text("issue.yesSave", "Yes, Save")"
Icon="user-check"
OnConfirm="ConfirmSaveAssignments"
OnCancel="CloseConfirmDialog" />
@code {
private bool isLoading = true;
private bool isLoadingMembers = false;
private bool isSaving = false;
private bool canSaveAssignments = false;
private bool showConfirmDialog = false;
private string? statusMessage;
private string? successMessage;
private bool isError = false;
private List<ProjectDto> projects = new();
private List<UserDto> allUsers = new();
private List<RoleDto> roles = new();
private long? adminRoleId;
private long? employeeRoleId;
private bool isAdminUser;
private ProjectDto? SelectedProject { get; set; }
private HashSet<long> SelectedUserIds { get; set; } = new();
private string projectSearchInput = "";
private string projectSearch = "";
private string userSearchInput = "";
private string userSearch = "";
private long selectedRoleId = 0;
protected override async Task OnInitializedAsync()
{
var authState = await AuthStateProvider.GetAuthenticationStateAsync();
isAdminUser = authState.User.IsInRole("Admin");
canSaveAssignments = authState.User.IsInRole("Admin") || await MenuAuthorization.HasAccessCodeAsync(authState.User, "Projects_Update");
var client = ApiClient.CreateClient();
try
{
var projectsTask = client.GetFromJsonAsync<List<ProjectDto>>("Project", Serialization.CaseInsensitive);
var usersTask = client.GetFromJsonAsync<List<UserDto>>("User", Serialization.CaseInsensitive);
var rolesTask = client.GetFromJsonAsync<List<RoleDto>>("Role", Serialization.CaseInsensitive);
await Task.WhenAll(projectsTask, usersTask, rolesTask);
projects = projectsTask.Result ?? new();
allUsers = usersTask.Result ?? new();
var allLoadedRoles = rolesTask.Result ?? new();
var adminRole = allLoadedRoles.FirstOrDefault(r => string.Equals(r.Name, "Admin", StringComparison.OrdinalIgnoreCase));
var employeeRole = allLoadedRoles.FirstOrDefault(r => string.Equals(r.Name, "Employee", StringComparison.OrdinalIgnoreCase));
adminRoleId = adminRole?.Id;
employeeRoleId = employeeRole?.Id;
roles = allLoadedRoles.Where(r => !string.Equals(r.Name, "Admin", StringComparison.OrdinalIgnoreCase)).ToList();
if (!isAdminUser && employeeRoleId.HasValue)
{
selectedRoleId = employeeRoleId.Value;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error loading initial data: {ex.Message}");
}
finally
{
isLoading = false;
}
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await JS.InvokeVoidAsync("initIcons");
}
private IEnumerable<ProjectDto> FilteredProjects =>
string.IsNullOrWhiteSpace(projectSearch)
? projects
: projects.Where(p => p.Name.Contains(projectSearch, StringComparison.OrdinalIgnoreCase));
private IEnumerable<UserDto> FilteredUsers
{
get
{
var result = allUsers.Where(u => u.IsActive);
if (adminRoleId.HasValue)
{
result = result.Where(u => u.RoleId != adminRoleId.Value);
}
if (isAdminUser && selectedRoleId > 0)
{
result = result.Where(u => u.RoleId == selectedRoleId);
}
else if (!isAdminUser && employeeRoleId.HasValue)
{
result = result.Where(u => u.RoleId == employeeRoleId.Value);
}
if (!string.IsNullOrWhiteSpace(userSearch))
{
result = result.Where(u => $"{u.FirstName} {u.LastName}".Contains(userSearch, StringComparison.OrdinalIgnoreCase)
|| u.Username.Contains(userSearch, StringComparison.OrdinalIgnoreCase));
}
return result;
}
}
private void ApplyProjectSearch()
{
projectSearch = projectSearchInput;
}
private void ApplyUserSearch()
{
userSearch = userSearchInput;
}
private void ResetProjectSearch()
{
projectSearchInput = "";
projectSearch = "";
}
private void ResetUserSearch()
{
userSearchInput = "";
userSearch = "";
selectedRoleId = isAdminUser ? 0 : employeeRoleId ?? 0;
}
private async Task SelectProject(ProjectDto project)
{
SelectedProject = project;
SelectedUserIds.Clear();
statusMessage = null;
isLoadingMembers = true;
StateHasChanged();
var client = ApiClient.CreateClient();
try
{
var membersResult = await client.GetFromJsonAsync<Result<IEnumerable<UserDto>>>($"Project/{project.Id}/members", Serialization.CaseInsensitive);
if (membersResult?.IsSuccess == true && membersResult.Value != null)
{
SelectedUserIds = new HashSet<long>(membersResult.Value.Select(u => u.Id));
}
}
catch (Exception ex)
{
Console.WriteLine($"Error loading project members: {ex.Message}");
statusMessage = "Failed to load project members.";
isError = true;
}
finally
{
isLoadingMembers = false;
StateHasChanged();
await JS.InvokeVoidAsync("initIcons");
}
}
private void ToggleUserSelection(long userId, bool isChecked)
{
if (isChecked) SelectedUserIds.Add(userId);
else SelectedUserIds.Remove(userId);
statusMessage = null;
}
private void SelectAllUsers()
{
foreach (var u in FilteredUsers)
{
SelectedUserIds.Add(u.Id);
}
statusMessage = null;
}
private void DeselectAllUsers()
{
foreach (var u in FilteredUsers)
{
SelectedUserIds.Remove(u.Id);
}
statusMessage = null;
}
private void RequestSaveConfirmation()
{
if (SelectedProject == null) return;
showConfirmDialog = true;
}
private void CloseConfirmDialog()
{
showConfirmDialog = false;
}
private async Task ConfirmSaveAssignments()
{
showConfirmDialog = false;
await SaveAssignments();
}
private async Task SaveAssignments()
{
if (SelectedProject == null) return;
isSaving = true;
statusMessage = null;
var client = ApiClient.CreateClient();
try
{
var dto = new AssignMembersDto { UserIds = SelectedUserIds.ToList() };
var response = await client.PostAsJsonAsync($"Project/{SelectedProject.Id}/members", dto);
if (response.IsSuccessStatusCode)
{
statusMessage = "Project assignments saved successfully!";
successMessage = "Assignment saved successfully!";
isError = false;
Navigation.NavigateTo("/projects/all");
}
else
{
statusMessage = "Failed to save assignment. Please try again.";
isError = true;
}
}
catch (Exception ex)
{
statusMessage = $"Error: {ex.Message}";
isError = true;
}
finally
{
isSaving = false;
await JS.InvokeVoidAsync("initIcons");
}
}
}
|