Files
PlaylistShared/PlaylistShared.Pwa/Pages/MyPlaylists.razor
2026-04-14 01:05:06 +03:00

126 lines
4.9 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
@page "/my-playlists"
@attribute [Authorize]
@using PlaylistShared.Shared.DTO
@using PlaylistShared.Shared.Playlist
@inject HttpClient Http
@inject ISnackbar Snackbar
@inject NavigationManager Navigation
<MudContainer MaxWidth="MaxWidth.Large" Class="mt-8">
<MudCard>
<MudCardHeader>
<CardHeaderContent>
<MudText Typo="Typo.h5">Мои плейлисты</MudText>
</CardHeaderContent>
<CardHeaderActions>
<!-- Явно указываем T="bool" для MudSwitch -->
<MudSwitch T="bool" @bind-Checked="_showOnlyShared" Color="Color.Primary" Label="Только расшаренные" />
<MudIconButton Icon="@Icons.Material.Filled.Refresh" OnClick="LoadPlaylists" />
</CardHeaderActions>
</MudCardHeader>
<MudCardContent>
@if (_loading)
{
<MudProgressCircular Indeterminate />
}
else if (_playlists == null || !_playlists.Any())
{
<MudText>Плейлисты не найдены. Убедитесь, что вы сохранили корректный токен Яндекс.Музыки.</MudText>
}
else
{
<MudTable Items="@FilteredPlaylists" Hover="true" Breakpoint="Breakpoint.Sm">
<HeaderContent>
<MudTh>Название</MudTh>
<MudTh>Треков</MudTh>
<MudTh>Статус</MudTh>
<MudTh></MudTh>
</HeaderContent>
<RowTemplate>
<MudTd><MudText>@context.Title</MudText></MudTd>
<MudTd>@context.TrackCount</MudTd>
<MudTd>
<!-- Явно указываем T="string" для MudChip -->
@if (context.IsShared)
{
<MudChip T="string" Size="Size.Small" Color="Color.Success">Расшарен</MudChip>
}
else
{
<MudChip T="string" Size="Size.Small" Color="Color.Default">Не расшарен</MudChip>
}
</MudTd>
<MudTd>
@if (!context.IsShared)
{
<MudButton Variant="Variant.Text" Color="Color.Primary" OnClick="() => SharePlaylist(context)">Поделиться</MudButton>
}
else
{
<MudButton Variant="Variant.Text" Color="Color.Secondary" OnClick="() => GoToShared(context)">Управлять</MudButton>
}
</MudTd>
</RowTemplate>
</MudTable>
}
</MudCardContent>
</MudCard>
</MudContainer>
@code {
private List<YandexPlaylistInfo> _playlists;
private bool _loading = true;
private bool _showOnlyShared = false;
private List<YandexPlaylistInfo> FilteredPlaylists => _showOnlyShared ? _playlists?.Where(p => p.IsShared).ToList() : _playlists;
protected override async Task OnInitializedAsync()
{
await LoadPlaylists();
}
private async Task LoadPlaylists()
{
_loading = true;
try
{
var response = await Http.GetFromJsonAsync<ApiResponse<List<YandexPlaylistInfo>>>("/api/playlists");
if (response?.Success == true)
_playlists = response.Data;
else
Snackbar.Add("Ошибка загрузки плейлистов", Severity.Error);
}
catch (Exception ex)
{
Snackbar.Add($"Ошибка: {ex.Message}", Severity.Error);
}
finally
{
_loading = false;
StateHasChanged();
}
}
private async Task SharePlaylist(YandexPlaylistInfo playlist)
{
var request = new SharePlaylistRequest { Kind = playlist.Kind, OwnerUid = playlist.OwnerUid };
var response = await Http.PostAsJsonAsync("/api/playlists/share", request);
if (response.IsSuccessStatusCode)
{
Snackbar.Add("Плейлист расшарен", Severity.Success);
await LoadPlaylists();
}
else
{
Snackbar.Add("Ошибка расшаривания", Severity.Error);
}
}
private void GoToShared(YandexPlaylistInfo playlist)
{
if (!string.IsNullOrEmpty(playlist.ShareToken))
Navigation.NavigateTo($"/shared/{playlist.ShareToken}");
else
Snackbar.Add("Ошибка: токен расшаривания не найден", Severity.Error);
}
}