Skip to content

Commit

Permalink
fixes #1002
Browse files Browse the repository at this point in the history
  • Loading branch information
iammukeshm committed Aug 22, 2024
1 parent 39a673a commit 2c082f2
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 16 deletions.
10 changes: 8 additions & 2 deletions src/apps/blazor/client/Components/ApiHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FSH.Starter.Blazor.Infrastructure.Api;
using Microsoft.AspNetCore.Components;
using MudBlazor;

namespace FSH.Starter.Blazor.Client.Components;
Expand All @@ -8,6 +9,7 @@ public static class ApiHelper
public static async Task<T?> ExecuteCallGuardedAsync<T>(
Func<Task<T>> call,
ISnackbar snackbar,
NavigationManager navigationManager,
FshValidation? customValidation = null,
string? successMessage = null)
{
Expand All @@ -23,8 +25,12 @@ public static class ApiHelper

return result;
}
catch (Exception ex)
catch (ApiException ex)
{
if (ex.StatusCode == 401)
{
navigationManager.NavigateTo("/logout");
}
var message = ex.Message switch
{
"TypeError: Failed to fetch" => "Unable to Reach API",
Expand Down Expand Up @@ -61,4 +67,4 @@ public static async Task<bool> ExecuteCallGuardedAsync(

return false;
}
}
}
3 changes: 1 addition & 2 deletions src/apps/blazor/client/Components/Dialogs/Logout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@

async Task Submit()
{
await AuthService.LogoutAsync();
Toast.Add("Logged out", Severity.Warning);
Navigation.NavigateTo("/logout");
MudDialog.Close(DialogResult.Ok(true));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private async Task LocalLoadDataAsync()
Loading = true;

if (await ApiHelper.ExecuteCallGuardedAsync(
() => Context.ClientContext.LoadDataFunc(), Toast)
() => Context.ClientContext.LoadDataFunc(), Toast, Navigation)
is List<TEntity> result)
{
_entityList = result;
Expand Down Expand Up @@ -144,7 +144,7 @@ private async Task<TableData<TEntity>> ServerReload(TableState state, Cancellati
var filter = GetPaginationFilter(state);

if (await ApiHelper.ExecuteCallGuardedAsync(
() => Context.ServerContext.SearchFunc(filter), Toast)
() => Context.ServerContext.SearchFunc(filter), Toast, Navigation)
is { } result)
{
_totalItems = result.TotalCount;
Expand Down Expand Up @@ -213,7 +213,7 @@ private async Task InvokeModal(TEntity? entity = default)
requestModel =
Context.GetDefaultsFunc is not null
&& await ApiHelper.ExecuteCallGuardedAsync(
() => Context.GetDefaultsFunc(), Toast)
() => Context.GetDefaultsFunc(), Toast, Navigation)
is { } defaultsResult
? defaultsResult
: new TRequest();
Expand All @@ -234,7 +234,7 @@ Context.GetDefaultsFunc is not null
Context.GetDetailsFunc is not null
&& await ApiHelper.ExecuteCallGuardedAsync(
() => Context.GetDetailsFunc(id!),
Toast)
Toast, Navigation)
is { } detailsResult
? detailsResult
: entity!.Adapt<TRequest>();
Expand Down
11 changes: 11 additions & 0 deletions src/apps/blazor/client/Pages/Auth/Logout.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@page "/logout"
@attribute [AllowAnonymous]
@inject IAuthenticationService AuthService

@code{
protected override async Task OnInitializedAsync()
{
await AuthService.LogoutAsync();
Toast.Add("Logged out", Severity.Error);
}
}
2 changes: 1 addition & 1 deletion src/apps/blazor/client/Pages/Auth/SelfRegister.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private async Task SubmitAsync()

var response = await ApiHelper.ExecuteCallGuardedAsync(
() => UsersClient.SelfRegisterUserEndpointAsync(Tenant, _createUserRequest),
Toast,
Toast, Navigation,
_customValidation);

if (response != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected override async Task OnInitializedAsync()
_canSearchRoleClaims = await AuthService.HasPermissionAsync(state.User, FshAction.View, FshResource.RoleClaims);

if (await ApiHelper.ExecuteCallGuardedAsync(
() => RolesClient.GetRolePermissionsEndpointAsync(Id), Toast)
() => RolesClient.GetRolePermissionsEndpointAsync(Id), Toast, Navigation)
is RoleDto role && role.Permissions is not null)
{
_title = string.Format("{0} Permissions", role.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private async Task ToggleUserStatus()
protected override async Task OnInitializedAsync()
{
if (await ApiHelper.ExecuteCallGuardedAsync(
() => UsersClient.GetUserEndpointAsync(Id!), Toast)
() => UsersClient.GetUserEndpointAsync(Id!), Toast, Navigation)
is UserDetail user)
{
_firstName = user.FirstName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ protected override async Task OnInitializedAsync()
_canSearchRoles = await AuthService.HasPermissionAsync(state.User, FshAction.View, FshResource.UserRoles);

if (await ApiHelper.ExecuteCallGuardedAsync(
() => UsersClient.GetUserEndpointAsync(Id!), Toast)
() => UsersClient.GetUserEndpointAsync(Id!), Toast, Navigation)
is UserDetail user)
{
_title = $"{user.FirstName} {user.LastName}'s Roles";
_description = string.Format("Manage {0} {1}'s Roles", user.FirstName, user.LastName);

if (await ApiHelper.ExecuteCallGuardedAsync(
() => UsersClient.GetUserRolesEndpointAsync(user.Id.ToString()), Toast)
() => UsersClient.GetUserRolesEndpointAsync(user.Id.ToString()), Toast, Navigation)
is ICollection<UserRoleDetail> response)
{
_userRolesList = response.ToList();
Expand Down
4 changes: 2 additions & 2 deletions src/apps/blazor/client/Pages/Multitenancy/Tenants.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private async Task DeactivateTenantAsync(string id)
{
if (await ApiHelper.ExecuteCallGuardedAsync(
() => ApiClient.DisableTenantEndpointAsync(id),
Toast,
Toast, Navigation,
null,
"Tenant Deactivated.") is not null)
{
Expand All @@ -106,7 +106,7 @@ private async Task ActivateTenantAsync(string id)
{
if (await ApiHelper.ExecuteCallGuardedAsync(
() => ApiClient.ActivateTenantEndpointAsync(id),
Toast,
Toast, Navigation,
null,
"Tenant Activated.") is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
Request.ExtendedExpiryDate = date.HasValue ? date.Value : Request.ExtendedExpiryDate;
if (await ApiHelper.ExecuteCallGuardedAsync(
() => TenantsClient.UpgradeSubscriptionEndpointAsync(Request),
Toast,
Toast, Navigation,
null,
"Upgraded Subscription.") is not null)
{
Expand Down

0 comments on commit 2c082f2

Please sign in to comment.