Files
WatchIt/WatchIt.Website/Components/Panels/Pages/UserPage/HeaderPanel.razor.cs

71 lines
1.9 KiB
C#
Raw Normal View History

2024-10-28 00:35:32 +01:00
using Microsoft.AspNetCore.Components;
using Refit;
using WatchIt.DTO.Models.Controllers.Accounts.Account;
using WatchIt.Website.Clients;
2024-10-30 23:28:47 +01:00
using WatchIt.Website.Services.Authentication;
2024-10-28 00:35:32 +01:00
namespace WatchIt.Website.Components.Panels.Pages.UserPage;
2024-10-28 00:35:32 +01:00
public partial class HeaderPanel : Component
2024-10-28 00:35:32 +01:00
{
2024-10-30 23:28:47 +01:00
#region SERVICES
[Inject] private IAuthenticationService AuthenticationService { get; set; } = default!;
[Inject] private IAccountsClient AccountsClient { get; set; } = default!;
2024-10-30 23:28:47 +01:00
#endregion
#region PARAMETERS
2024-11-10 14:33:41 +01:00
[Parameter] public required AccountResponse Data { get; set; }
[Parameter] public required List<AccountResponse> Followers { get; set; }
[Parameter] public AccountResponse? LoggedUserData { get; set; }
[Parameter] public Action<bool>? FollowingChanged { get; set; }
2024-10-30 23:28:47 +01:00
#endregion
#region FIELDS
2024-11-10 14:33:41 +01:00
private bool _followLoading;
2024-10-30 23:28:47 +01:00
#endregion
2024-11-10 14:33:41 +01:00
2024-10-30 23:28:47 +01:00
#region PRIVATE METHODS
2024-11-10 14:33:41 +01:00
private async Task Follow()
2024-10-30 23:28:47 +01:00
{
string token = await AuthenticationService.GetRawAccessTokenAsync() ?? string.Empty;
2024-11-10 14:33:41 +01:00
_followLoading = true;
IApiResponse response;
2024-11-10 14:33:41 +01:00
if (Followers.Any(x => x.Id == LoggedUserData!.Id))
{
response = await AccountsClient.DeleteAccountFollow(token, Data.Id);
if (response.IsSuccessful)
2024-11-10 14:33:41 +01:00
{
Followers.RemoveAll(x => x.Id == LoggedUserData!.Id);
FollowingChanged?.Invoke(false);
}
2024-11-10 14:33:41 +01:00
}
else
2024-10-30 23:28:47 +01:00
{
response = await AccountsClient.PostAccountFollow(token, Data.Id);
if (response.IsSuccessful)
2024-11-10 14:33:41 +01:00
{
Followers.Add(LoggedUserData);
FollowingChanged?.Invoke(true);
}
}
if (response.IsSuccessful)
{
_followLoading = false;
2024-10-30 23:28:47 +01:00
}
}
#endregion
2024-10-28 00:35:32 +01:00
}