Skip to content

Commit

Permalink
(#431) wasm: add expansion pannelt for settings
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed Oct 11, 2024
1 parent 602b86e commit 1707556
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ public async Task UpdateUserNotificationPreferencesAsync(Guid studentId, Notific
preferencesDto.FriendsNotifications
};

var jsonData = JsonSerializer.Serialize(updatePreferencesData);
Console.WriteLine($"Sending UpdateUserNotificationPreferences request: {jsonData}");

await _httpClient.PostAsync($"students/{studentId}/notifications", updatePreferencesData);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@inject IIdentityService IdentityService
@inject IStudentsService StudentsService
@inject NavigationManager NavigationManager
@inject ISnackbar Snackbar
@using Astravent.Web.Wasm.DTO
@using MudBlazor
@using System.Text.Json
Expand Down Expand Up @@ -45,13 +46,13 @@ else
IsLoading = true;
try
{
var studentId = IdentityService.GetCurrentUserId();
var studentId = await IdentityService.GetCurrentUserIdFromJwtAsync(); // Correct method to get user ID from JWT
StudentWithGalleryImagesDto = await StudentsService.GetStudentWithGalleryImagesAsync(studentId);
NotificationPreferencesDto = await StudentsService.GetUserNotificationPreferencesAsync(studentId);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Snackbar.Add($"Error loading preferences: {ex.Message}", Severity.Error);
}
finally
{
Expand All @@ -63,13 +64,13 @@ else
{
try
{
var studentId = IdentityService.GetCurrentUserId();
var studentId = await IdentityService.GetCurrentUserIdFromJwtAsync(); // Correct method to get user ID from JWT
await StudentsService.UpdateUserNotificationPreferencesAsync(studentId, NotificationPreferencesDto, StudentWithGalleryImagesDto.Student.EmailNotifications);
Console.WriteLine("Notification preferences updated successfully.");
Snackbar.Add("Notification preferences updated successfully.", Severity.Success);
}
catch (Exception ex)
{
Console.WriteLine($"Error saving notification preferences: {ex.Message}");
Snackbar.Add($"Error saving notification preferences: {ex.Message}", Severity.Error);
}
}
}
121 changes: 51 additions & 70 deletions MiniSpace.Web/src/Astravent.Web.Wasm/Pages/Account/ShowAccount.razor
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@inject IMediaFilesService MediaFilesService
@inject NavigationManager NavigationManager
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject ISnackbar Snackbar

<AuthWrapper>
<MudContainer Class="account-container">
Expand Down Expand Up @@ -56,7 +57,7 @@
}
else if (activeTabIndex == 2)
{
<NotificationsComponent IsLoading="isLoading" NotificationPreferencesDto="notificationPreferencesDto" StudentWithGalleryImagesDto="studentWithGalleryImagesDto" />
<NotificationsSettingsComponent IsLoading="isLoading" NotificationPreferencesDto="notificationPreferencesDto" StudentWithGalleryImagesDto="studentWithGalleryImagesDto" />
}
else if (activeTabIndex == 3)
{
Expand Down Expand Up @@ -238,17 +239,26 @@

private async Task SaveChanges()
{
var studentDto = studentWithGalleryImagesDto.Student;
studentDto.Languages = selectedLanguages.ToList();
studentDto.Interests = selectedInterests.ToList();
try
{
var studentDto = studentWithGalleryImagesDto.Student;
studentDto.Languages = selectedLanguages.ToList();
studentDto.Interests = selectedInterests.ToList();

await StudentsService.UpdateStudentLanguagesAndInterestsAsync(
studentDto.Id,
studentDto.Languages,
studentDto.Interests
);
await StudentsService.UpdateStudentLanguagesAndInterestsAsync(
studentDto.Id,
studentDto.Languages,
studentDto.Interests
);

await StudentsService.UpdateStudentDto(studentDto.Id);
Snackbar.Add("Changes saved successfully.", Severity.Success);
}
catch (Exception ex)
{
Snackbar.Add($"Failed to save changes: {ex.Message}", Severity.Error);
}

await StudentsService.UpdateStudentDto(studentDto.Id);
StateHasChanged();
}

Expand All @@ -258,18 +268,7 @@
{
var studentDto = studentWithGalleryImagesDto.Student;

foreach (var education in studentDto.Education)
{
Console.WriteLine($"Education StartDate: {education.StartDate}, EndDate: {education.EndDate}");
}

foreach (var work in studentDto.Work)
{
Console.WriteLine($"Work StartDate: {work.StartDate}, EndDate: {work.EndDate}");
}


// Ensure that StartDate and EndDate are handled correctly
// Handle dates for Education and Work
foreach (var education in studentDto.Education)
{
education.StartDate = education.StartDate != DateTime.MinValue ? education.StartDate : null;
Expand All @@ -282,30 +281,7 @@
work.EndDate = work.EndDate != DateTime.MinValue ? work.EndDate : null;
}

var updateStudentData = new
{
studentDto.Id,
studentDto.FirstName,
studentDto.LastName,
studentDto.ProfileImageUrl,
studentDto.Description,
studentDto.EmailNotifications,
studentDto.ContactEmail,
studentDto.Languages,
studentDto.Interests,
studentDto.Education,
studentDto.Work,
studentDto.PhoneNumber,
studentDto.Country,
studentDto.City,
studentDto.DateOfBirth,
IsTwoFactorEnabled = isTwoFactorEnabled,
TwoFactorSecret = isTwoFactorEnabled ? twoFactorSecret : null
};

var jsonData = JsonSerializer.Serialize(updateStudentData);
Console.WriteLine($"Sending UpdateStudent request: {jsonData}");

// Update student details
await StudentsService.UpdateStudentAsync(
studentDto.Id,
studentDto.FirstName,
Expand All @@ -327,19 +303,23 @@
studentDto.DateOfBirth
);

// Update notification preferences
if (studentDto.EmailNotifications)
{
await StudentsService.UpdateUserNotificationPreferencesAsync(studentDto.Id, notificationPreferencesDto, studentDto.EmailNotifications);
}

await StudentsService.UpdateUserSettingsAsync(studentDto.Id, availableSettingsDto);

Snackbar.Add("Profile updated successfully.", Severity.Success);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Snackbar.Add($"Failed to update profile: {ex.Message}", Severity.Error);
}
}

StateHasChanged();
}

private async Task SaveTwoFactorSettingsAsync()
{
Expand All @@ -358,33 +338,37 @@
{
await IdentityService.DisableTwoFactorAsync(IdentityService.GetCurrentUserId());
}

Snackbar.Add("Two-factor authentication settings updated successfully.", Severity.Success);
}
catch (Exception ex)
{
Console.WriteLine($"Error saving 2FA settings: {ex.Message}");
Snackbar.Add($"Failed to update two-factor authentication settings: {ex.Message}", Severity.Error);
}
}

private async Task ToggleTwoFactor(bool enabled)
{
isTwoFactorEnabled = enabled;
if (!enabled)
try
{
await IdentityService.DisableTwoFactorAsync(IdentityService.GetCurrentUserId());
twoFactorSecret = null;
isTwoFactorEnabled = enabled;
if (!enabled)
{
await IdentityService.DisableTwoFactorAsync(IdentityService.GetCurrentUserId());
twoFactorSecret = null;
}
else
{
twoFactorSecret = await IdentityService.GenerateTwoFactorSecretAsync(IdentityService.GetCurrentUserId());
}

Snackbar.Add("Two-factor authentication toggled successfully.", Severity.Success);
StateHasChanged();
}
else
catch (Exception ex)
{
twoFactorSecret = await IdentityService.GenerateTwoFactorSecretAsync(IdentityService.GetCurrentUserId());
Snackbar.Add($"Failed to toggle two-factor authentication: {ex.Message}", Severity.Error);
}
StateHasChanged();
}

private async Task GenerateTwoFactorSecret()
{
var userId = IdentityService.GetCurrentUserId();
twoFactorSecret = await IdentityService.GenerateTwoFactorSecretAsync(userId);
StateHasChanged();
}

private async Task SaveNotificationPreferencesAsync()
Expand All @@ -393,11 +377,11 @@
{
var studentId = IdentityService.GetCurrentUserId();
await StudentsService.UpdateUserNotificationPreferencesAsync(studentId, notificationPreferencesDto, studentWithGalleryImagesDto.Student.EmailNotifications);
Console.WriteLine("Notification preferences updated successfully.");
Snackbar.Add("Notification preferences updated successfully.", Severity.Success);
}
catch (Exception ex)
{
Console.WriteLine($"Error saving notification preferences: {ex.Message}");
Snackbar.Add($"Failed to update notification preferences: {ex.Message}", Severity.Error);
}
}

Expand All @@ -412,17 +396,14 @@
}

await StudentsService.UpdateUserSettingsAsync(studentId, availableSettingsDto);

Console.WriteLine("Student settings updated successfully.");
Snackbar.Add("User settings updated successfully.", Severity.Success);
}
catch (Exception ex)
{
Console.WriteLine($"Error saving user settings: {ex.Message}");
Snackbar.Add($"Failed to update user settings: {ex.Message}", Severity.Error);
}
}



private void SetActiveTabIndex(int index)
{
activeTabIndex = index;
Expand Down
Loading

0 comments on commit 1707556

Please sign in to comment.