using Microsoft.AspNetCore.Components;
using System.Net.Http;
using System.Threading.Tasks;
namespace JobsJobsJobs.Client.Pages.SoLong
{
public partial class Options : ComponentBase
{
///
/// Extract an error phrase from a response similar to 404 - Not Found
///
/// The HTTP response
/// The formatted error code
private static string ErrorPhrase(HttpResponseMessage response) =>
$"{response.StatusCode}{(string.IsNullOrEmpty(response.ReasonPhrase) ? "" : $" - {response.ReasonPhrase }")}";
///
/// Delete the profile only; redirect to home page on success
///
private async Task DeleteProfile()
{
ServerApi.SetJwt(http, state);
var result = await http.DeleteAsync("/api/profile/");
if (result.IsSuccessStatusCode)
{
toast.ShowSuccess("Profile Deleted Successfully");
nav.NavigateTo("/citizen/dashboard");
}
else
{
toast.ShowError(ErrorPhrase(result));
}
}
///
/// Delete everything pertaining to the user's account
///
private async Task DeleteAccount()
{
ServerApi.SetJwt(http, state);
var result = await http.DeleteAsync("/api/citizen/");
if (result.IsSuccessStatusCode)
{
state.Jwt = "";
state.User = null;
nav.NavigateTo("/so-long/success");
}
else
{
toast.ShowError(ErrorPhrase(result));
}
}
}
}