[frontend/pages] Add update avatar and banner to profile settings

This commit is contained in:
pancakes 2024-11-28 11:47:37 +10:00 committed by Laura Hausmann
parent d56b7760df
commit 6656fbac18
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 25 additions and 0 deletions

View file

@ -1,5 +1,6 @@
using Iceshrimp.Frontend.Core.Services;
using Iceshrimp.Shared.Schemas.Web;
using Microsoft.AspNetCore.Components.Forms;
namespace Iceshrimp.Frontend.Core.ControllerModels;
@ -14,12 +15,18 @@ internal class ProfileControllerModel(ApiClient api)
public Task<string> GetAvatarUrlAsync() =>
api.CallAsync<string>(HttpMethod.Get, "/profile/avatar");
public Task UpdateAvatarAsync(IBrowserFile file) =>
api.CallAsync(HttpMethod.Post, "/profile/avatar", data: file);
public Task DeleteAvatarAsync() =>
api.CallAsync(HttpMethod.Delete, "/profile/avatar");
public Task<string> GetBannerUrlAsync() =>
api.CallAsync<string>(HttpMethod.Get, "/profile/banner");
public Task UpdateBannerAsync(IBrowserFile file) =>
api.CallAsync(HttpMethod.Post, "/profile/banner", data: file);
public Task DeleteBannerAsync() =>
api.CallAsync(HttpMethod.Delete, "/profile/banner");

View file

@ -32,6 +32,7 @@
<label for="delete-banner">@Loc["Remove Banner"]</label>
</div>
}
<InputFile class="input" OnChange="OnBannerFileChange" accept="image/*" disabled="@DelBanner"/>
</div>
<div class="section">
<h3>@Loc["Avatar"]</h3>
@ -43,6 +44,7 @@
<label for="delete-avatar">@Loc["Remove Avatar"]</label>
</div>
}
<InputFile class="input" OnChange="OnAvatarFileChange" accept="image/*" disabled="@DelAvatar"/>
</div>
<div class="section">
<h3>@Loc["Display Name"]</h3><input class="input" @bind="@DisplayName"/>
@ -109,8 +111,10 @@
private string FieldName { get; set; } = "";
private string FieldValue { get; set; } = "";
private StateButton SaveButton { get; set; } = null!;
private IBrowserFile? AvatarFile { get; set; } = null;
private string? AvatarUrl { get; set; } = null;
private bool DelAvatar { get; set; } = false;
private IBrowserFile? BannerFile { get; set; } = null;
private string? BannerUrl { get; set; } = null;
private bool DelBanner { get; set; } = false;
private string DisplayName { get; set; } = "";
@ -153,8 +157,12 @@
await Api.Profile.UpdateDisplayNameAsync(DisplayName);
if (DelAvatar)
await Api.Profile.DeleteAvatarAsync();
else if (AvatarFile != null)
await Api.Profile.UpdateAvatarAsync(AvatarFile);
if (DelBanner)
await Api.Profile.DeleteBannerAsync();
else if (BannerFile != null)
await Api.Profile.UpdateBannerAsync(BannerFile);
SaveButton.State = StateButton.StateEnum.Success;
}
catch (ApiException e)
@ -163,4 +171,14 @@
SaveButton.State = StateButton.StateEnum.Failed;
}
}
private void OnAvatarFileChange(InputFileChangeEventArgs e)
{
AvatarFile = e.GetMultipleFiles().First(p => p.ContentType.StartsWith("image/"));
}
private void OnBannerFileChange(InputFileChangeEventArgs e)
{
BannerFile = e.GetMultipleFiles().First(p => p.ContentType.StartsWith("image/"));
}
}