Iceshrimp.NET/Iceshrimp.Frontend/Components/FollowButton.razor

114 lines
No EOL
3.4 KiB
Text

@using Iceshrimp.Assets.PhosphorIcons
@using Iceshrimp.Frontend.Core.Services
@using Iceshrimp.Frontend.Localization
@using Iceshrimp.Shared.Schemas.Web
@using Microsoft.Extensions.Localization
@inject IStringLocalizer<Localization> Loc;
@inject ApiService Api;
<span class="follow-button">
@switch (_buttonType)
{
case ButtonType.Follow:
<button @onclick="Action" class="btn">
<span class="text">@Loc["Follow"]</span>
<Icon Name="Icons.Plus" class="icon"/>
</button>
break;
case ButtonType.Unfollow:
<button @onclick="Action" class="btn inverted">
<span class="text">@Loc["Unfollow"]</span>
<Icon Name="Icons.Minus" class="icon"/>
</button>
break;
case ButtonType.FollowBack:
<button @onclick="Action" class="btn">
<span class="text">@Loc["Follow back"]</span>
<Icon Name="Icons.Plus" class="icon"/>
</button>
break;
case ButtonType.CancelRequest:
<button @onclick="Action" class="btn inverted">
<span class="text">@Loc["Cancel request"]</span>
<Icon Name="Icons.X" class="icon"/>
</button>
break;
}
</span>
@code {
[Parameter] [EditorRequired] public required UserResponse User { get; set; }
[Parameter] [EditorRequired] public required UserProfileResponse UserProfile { get; set; }
private ButtonType _buttonType;
protected override void OnInitialized()
{
ChooseButton();
}
private enum ButtonType
{
Follow,
Unfollow,
FollowBack,
FollowLocked,
CancelRequest
}
private void Action()
{
switch (_buttonType)
{
case ButtonType.Follow:
Follow();
break;
case ButtonType.FollowBack:
Follow();
break;
case ButtonType.Unfollow or ButtonType.CancelRequest:
Unfollow();
break;
}
}
private async void Unfollow()
{
await Api.Users.UnfollowUser(User.Id);
UserProfile.Relations -= (int)Relations.Following;
ChooseButton();
StateHasChanged();
}
private async void Follow()
{
await Api.Users.FollowUser(User.Id);
UserProfile.Relations += (int)Relations.Requested;
ChooseButton();
StateHasChanged();
UserProfile = await Api.Users.GetUserProfile(UserProfile.Id) ?? throw new Exception("How did it stop existing");
ChooseButton();
StateHasChanged();
}
private void ChooseButton()
{
if (UserProfile.Relations == Relations.None)
{
_buttonType = ButtonType.Follow;
}
if (UserProfile.Relations.HasFlag(Relations.FollowedBy))
{
_buttonType = ButtonType.FollowBack;
}
if (UserProfile.Relations.HasFlag(Relations.Following))
{
_buttonType = ButtonType.Unfollow;
}
if (UserProfile.Relations.HasFlag(Relations.Requested))
{
_buttonType = ButtonType.CancelRequest;
}
}
}