/// <summary> /// Parses the response message and presents errors if neccessary. /// </summary> /// <param name="responseMessage">The response from the server.</param> /// <returns>The response message.</returns> private async Task <ControllerResponseDTO <T> > ParseResponse <T>(HttpResponseMessage responseMessage) { if (responseMessage == null) { throw new ArgumentNullException(nameof(responseMessage)); } if (!responseMessage.IsSuccessStatusCode) { // if we fail here fire an unknown sweet alert and return with an empty controller response. await SweetAlertService.FireAsync(AlertHelper.UnknownError()); return(new ControllerResponseDTO <T>()); } var json = await responseMessage.Content.ReadAsStringAsync(); var controllerResponse = JsonConvert.DeserializeObject <ControllerResponseDTO <T> >(json); if (controllerResponse.HasError) { await SweetAlertService.FireAsync(AlertHelper.ValidationError(controllerResponse.ErrorMessage)); } //todo: consider throwing custom errors here once solution becomes more robust return(controllerResponse); }
public async Task BuyMarketItemAsync(MarketItem marketItem, Action <MarketItem> onSuccessfullBuy = null) { var response = await httpClient.PostAsync($"api/marketitems/{marketItem.Id}/buy", null); switch (response.StatusCode) { case HttpStatusCode.NotFound: await swal.FireAsync("Not Found", "The item you were trying to buy could not be found", SweetAlertIcon.Error); break; case HttpStatusCode.Forbidden: await swal.FireAsync("Forbidden", "You are the seller of this item", SweetAlertIcon.Error); break; case HttpStatusCode.Gone: await swal.FireAsync("No Content", "The item you are trying to buy was already sold", SweetAlertIcon.Error); break; case HttpStatusCode.BadRequest: await swal.FireAsync("Bad Request", "You cannot afford buying this item", SweetAlertIcon.Error); break; case HttpStatusCode.Conflict: await swal.FireAsync("Conflict", "You have already reached the maximum number of active shoppings", SweetAlertIcon.Error); break; case HttpStatusCode.ServiceUnavailable: await swal.FireAsync("Service Unavailable", "Failed to communicate with game server, try again later", SweetAlertIcon.Error); break; case HttpStatusCode.InternalServerError: await swal.FireAsync("Internal Server Error", "Try again later", SweetAlertIcon.Error); break; case HttpStatusCode.Unauthorized: await swal.FireAsync("Unauthorized", "You have to sign in to be able to buy", SweetAlertIcon.Error); break; } if (response.StatusCode == HttpStatusCode.OK) { if (onSuccessfullBuy != null) { onSuccessfullBuy.Invoke(marketItem); } await swal.FireAsync("OK", $"You successfully bought {marketItem.Id} ({marketItem.ItemName}) for ${marketItem.Price} from {marketItem.SellerName}!", SweetAlertIcon.Success); await balanceService.UpdateBalanceAsync(); } }
public async Task HandleSuccess <TResponse, TAction>(QueryResponse <TResponse> response, TAction action, bool silent = false, string customMessage = "") { // Display message to UI switch (silent) { case true: break; case false: SweetAlertService.FireAsync("Success", string.IsNullOrEmpty(customMessage) ? $"Success: {response.Message}" : $"{customMessage}: {response.Message}"); break; } // If Success URL property is provided, navigate to the given URL if (!action.ContainsProperty("NavigateToOnSuccess")) { return; } var s = action.GetPropertyValue("NavigateToOnSuccess"); if (s is null) { return; } NavigationManager.NavigateTo(s.ToString()); }
public async void DeleteItem(Item itemToDelete) { // todo: sweet alert to confirm that cancellation is desired var result = await SweetAlertService.FireAsync(AlertHelper.ConfirmationWarning(string.Format("Are you sure you want to delete item {0}", itemToDelete.Name))); if (!string.IsNullOrEmpty(result.Value)) // ensures that deletion only occurs after confirmation { await ItemService.DeleteItem(itemToDelete); items = await ItemService.GetItems(); StateHasChanged(); } }
private bool HandleFailure(QueryResponse <ExistenceResponse> request, RegisterAction action) { if (request.HttpStatusCode is HttpStatusCode.Accepted) { return(false); } SweetAlertService.FireAsync($"An error occured while creating your account: {request.Message}", $"", SweetAlertIcon.Error); // If Fail URL property is provided, navigate to the given URL if (!string.IsNullOrEmpty(action.NavigateToOnFailure)) { NavigationManager.NavigateTo(action.NavigateToOnFailure); } return(true); }
private async Task <SweetAlertResult> SweetPopUptAsync(IJSRuntime jsRuntime, SweetAlertOptions sweetAlert) { try { Swal = new SweetAlertService(jsRuntime); SweetAlertResult result = await Swal.FireAsync(sweetAlert); return(result); } catch (Exception ex) { return(new SweetAlertResult() { Value = $"SweetError: {ex.Message}" }); } }
public async Task <bool> HandleFailure <TAction>(CmdResponse response, TAction action, bool silent = false, string customMessage = "") { if (response.HttpStatusCode is HttpStatusCode.Accepted) { return(false); } // Display message to UI switch (silent) { case true: SweetAlertService.FireAsync("Error", $"There was an error while trying to process your request, please try again later"); break; case false: SweetAlertService.FireAsync("Error", string.IsNullOrEmpty(customMessage) ? $"There was an error while trying to process your request: {response.Message}" : $"{customMessage}: {response.Message}"); break; } // Display error to the console Console.WriteLine($"Error from response: {response.Message}"); // If Fail URL property is provided, navigate to the given URL if (!action.ContainsProperty("NavigateToOnFailure")) { return(true); } var s = action.GetPropertyValue("NavigateToOnFailure"); if (s is null) { return(true); } NavigationManager.NavigateTo(s.ToString()); return(true); }
/// <summary> /// Maps the close dialog for the component. /// </summary> /// <param name="result">Dynamic class result in the Modals/DialogResult format.</param> public async void CloseDialog(dynamic result) { if (result != null && !result.IsCancellation) // if the modal is exited from any fashion other then cancel or save then the result will be null { await ItemService.SaveItem(result.Data); string successMessage; if (result.Data.Id != 0) // The dialog was opened for editing so a different message should be sent. { successMessage = string.Format("Item {0}, sucessfully edited!", result.Data.Name); } else { successMessage = string.Format("Item {0}, sucessfully created!", result.Data.Name); } await SweetAlertService.FireAsync(AlertHelper.ToastSuccess(successMessage)); items = await ItemService.GetItems(); StateHasChanged(); } }
public override async Task <Unit> Handle(RegisterAction action, CancellationToken aCancellationToken) { // Check If Any Given Data Are Already Existing if (await CheckDuplicateRecords(action)) { return(Unit.Value); } // Check If Passwords Are Correct if (!CurrentState.RegisterVm.Password.Equals(CurrentState.RegisterVm.PasswordConfirmation)) { SweetAlertService.FireAsync("Password does not match", $"", SweetAlertIcon.Error); return(Unit.Value); } // Create Guids var identityGuid = Guid.NewGuid(); var credentialGuid = Guid.NewGuid(); // Send Create Identity Request var identityRequest = CurrentState.RegisterVm.Adapt <CreateIdentityRequest>(); identityRequest.Guid = identityGuid; var identity = await IdentityServiceWrapper.CreateIdentity(identityRequest); if (HandleFailure(identity, action)) { return(Unit.Value); } // Send Create Credential Request var credentialRequest = CurrentState.RegisterVm.Adapt <CreateCredentialRequest>(); credentialRequest.Guid = credentialGuid; credentialRequest.IdentityGuid = identityGuid; credentialRequest.RoleEntity = Guid.Parse("fb2ec753-66b2-4259-b65f-1c6402e58209"); var credential = await IdentityServiceWrapper.CreateCredential(credentialRequest); if (HandleFailure(credential, action)) { return(Unit.Value); } // Send Create Phone Contact Request if (!string.IsNullOrEmpty(CurrentState.RegisterVm.PhoneNumber)) { var phoneContact = await IdentityServiceWrapper.CreateContact(new() { CredentialGuid = credentialGuid, ContactType = GenericContactType.Phone, Value = CurrentState.RegisterVm.PhoneNumber }); if (HandleFailure(phoneContact, action)) { return(Unit.Value); } } // Send Create Email Contact Request if (!string.IsNullOrEmpty(CurrentState.RegisterVm.EmailAddress)) { var emailContact = await IdentityServiceWrapper.CreateContact(new() { CredentialGuid = credentialGuid, ContactType = GenericContactType.Phone, Value = CurrentState.RegisterVm.EmailAddress }); if (HandleFailure(emailContact, action)) { return(Unit.Value); } } // If Success URL property is provided, navigate to the given URL await HandleSuccess(credential, action); return(Unit.Value); }