Files
PlaylistShared/PlaylistShared.Pwa/Components/Profile/YandexTokenDialog.razor

94 lines
4.1 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.
@using System.Text.RegularExpressions
@using PlaylistShared.Shared.DTO
@inject HttpClient Http
@inject ISnackbar Snackbar
<MudDialog>
<TitleContent>
<MudText Typo="Typo.h6">Подключение Яндекс.Музыки</MudText>
</TitleContent>
<DialogContent>
<MudStepper @bind-ActiveIndex="_index" Vertical CenterLabels CompletedStepColor="Color.Success" @onwheel="HandleWheel">
<ChildContent>
<MudStep Title="Вход" Skippable>
<MudText Typo="Typo.body2" Class="mb-4">Нажмите на кнопку и разрешите доступ приложению.</MudText>
<MudButton Variant="Variant.Filled"
Color="Color.Primary"
StartIcon="@PlaylistShared.Pwa.CustomIcons.Yandex"
Href="https://oauth.yandex.ru/authorize?response_type=token&client_id=23cabbbdc6cd418abb4b39c32c41195d"
Target="_blank"
@onclick="() => {_index++;}"
FullWidth>
Войти в Яндекс
</MudButton>
</MudStep>
<MudStep Title="Добавление токена">
Скопируйте значение <code>access_token</code> или <code>весь URL</code> из адресной строки после перенаправления.
<MudAlert Severity="Severity.Info" Class="mt-4">
https://music.yandex.ru/#access_token=<code>ВАШ_ТОКЕН</code>&...
</MudAlert>
<MudTextField @bind-Value="_rawInput" @bind-Value:after="Submit" Label="Вставьте скопированную ссылку или токен" Variant="Variant.Outlined" Margin="Margin.Dense" Error="_tokenErr" />
<MudButton Variant="Variant.Filled" Color="Color.Success" OnClick="Submit" FullWidth Class="mt-4">
Сохранить
</MudButton>
</MudStep>
</ChildContent>
<ActionContent Context="stepper">
<MudIconButton OnClick="@(() => stepper.PreviousStepAsync())" Icon="@Icons.Material.Filled.ArrowBack" Color="Color.Primary" Disabled="@(_index <= 0)" />
<MudSpacer />
<MudIconButton OnClick="@(() => stepper.NextStepAsync())" Icon="@Icons.Material.Filled.ArrowForward" Color="Color.Primary" Disabled="@(_index >= 1)" />
</ActionContent>
</MudStepper>
</DialogContent>
</MudDialog>
@code {
[CascadingParameter] IMudDialogInstance MudDialog { get; set; }
private string _rawInput = "";
private int _index;
private bool _tokenErr = false;
private async Task HandleWheel(WheelEventArgs e)
{
if (e.DeltaY > 0 && _index < 1) // Прокрутка вниз -> Вперед
{
_index++;
}
else if (e.DeltaY < 0 && _index > 0) // Прокрутка вверх -> Назад
{
_index--;
}
}
private async Task Submit()
{
var token = ExtractToken(_rawInput);
if (string.IsNullOrWhiteSpace(token))
{
_tokenErr = true;
Snackbar.Add("Токен не найден", Severity.Error);
return;
}
_tokenErr = false;
var response = await Http.PostAsJsonAsync("/api/yandextoken/set", new SetYandexTokenRequest { Token = token });
if (response.IsSuccessStatusCode)
{
Snackbar.Add("Токен успешно обновлен", Severity.Success);
MudDialog.Close(DialogResult.Ok(true));
}
else
{
Snackbar.Add("Ошибка обновления токена. Повторите позже.", Severity.Error);
}
}
private string ExtractToken(string input) =>
input.Contains("access_token=") ? Regex.Match(input, @"access_token=([^&]+)").Groups[1].Value : input.Trim();
public class SetYandexTokenRequest { public string Token { get; set; } }
}