Neighbourhood.omg.lol/Components/PicList.razor

52 lines
1.4 KiB
Text
Raw Permalink Normal View History

@implements IDisposable
@inject IJSRuntime JS
2024-06-13 00:26:43 +00:00
@inject State State
2024-06-11 07:24:52 +00:00
@if (Editable) {
<EditPicDialog @ref="Dialog" id="EditPicModal"></EditPicDialog>
2024-06-11 07:24:52 +00:00
}
@if (pics != null) foreach (Pic pic in pics) {
<PicCard Pic="pic" Editable="Editable" Dialog="Dialog"></PicCard>
}
2024-06-11 07:24:52 +00:00
<LoadingCard id="pics-loading" icon="fa-solid fa-images"></LoadingCard>
2024-06-05 12:41:08 +00:00
@code {
[Parameter]
2024-07-02 00:13:52 +00:00
public Func<bool, Task<List<Pic>?>>? PicsFunc { get; set; }
2024-06-07 04:25:21 +00:00
[Parameter]
public bool Editable { get; set; } = false;
2024-06-11 07:24:52 +00:00
public EditPicDialog? Dialog { get; set; }
2024-06-11 07:24:52 +00:00
private List<Pic>? pics;
2024-06-11 07:24:52 +00:00
// TODO: There is a noticable rendering delay between the pics loading and the page rendering
protected override async Task OnInitializedAsync() {
await base.OnInitializedAsync();
2024-07-02 00:13:52 +00:00
if (PicsFunc == null) return;
if (pics == null || pics.Count == 0) pics = await PicsFunc(false);
State.PropertyChanged += StateChanged;
State.CanRefresh = true;
await InvokeAsync(StateHasChanged);
await JS.InvokeVoidAsync("removeElementById", "pics-loading");
2024-06-11 07:24:52 +00:00
}
private async void StateChanged(object? sender, PropertyChangedEventArgs e) {
2024-07-02 00:13:52 +00:00
if (PicsFunc == null) return;
if (e.PropertyName == nameof(State.IsRefreshing) && State.IsRefreshing) {
2024-06-24 04:52:45 +00:00
using (State.GetRefreshToken()){
pics = await PicsFunc(true);
await InvokeAsync(StateHasChanged);
}
}
}
public void Dispose() {
State.PropertyChanged -= StateChanged;
State.CanRefresh = false;
}
2024-06-11 07:24:52 +00:00
}