Esempio n. 1
0
        /// <summary>
        /// Custom server response handler.
        /// </summary>
        /// <param name="result">
        /// A reference to the <see cref="FormManagerBase{TModel}.SubmitResult"/> property.
        /// </param>
        private void ProcessCustomServerResponse(FormManagerSubmitResult result)
        {
            // Don't update the state because it will be done by the
            // form manager once execution of this method is finished.
            var xhr = result.XHR;

            if (xhr.IsJsonResponse)
            {
                if (IsDebug)
                {
                    Console.WriteLine($"Raw JSON result: {xhr.ResponseText}");
                }

                try
                {
                    var postResult = JsonSerializer.Deserialize <PostFormHttpResult>(
                        xhr.ResponseText,
                        CaseInsensitiveJson
                        );

                    if (postResult.Success)
                    {
                        SubmitResult = FormManagerSubmitResult.Success(result,
                                                                       postResult.Message ?? SUCCESS_MESSAGE);
                    }
                    else if (true == xhr.ExtraProperties?.ContainsKey("error"))
                    {
                        SubmitResult = FormManagerSubmitResult.Failed(result,
                                                                      xhr.ExtraProperties["error"]?.ToString());
                    }
                    else
                    {
                        SubmitResult = FormManagerSubmitResult.Failed(result,
                                                                      postResult.GetErrorDetails() ?? UNKNOWN_SUBMIT_ERROR);
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex);
                }
            }
            else if (IsDebug)
            {
                if (xhr.IsHtmlResponse)
                {
                    Console.WriteLine($"HTML result: {xhr.ResponseText}");
                }
                else
                {
                    Console.WriteLine($"Raw result: {xhr.ResponseText}");
                }
            }
        }
        /// <summary>
        /// Custom server response handler.
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="result">
        /// A reference to the <see cref="FormManagerBase{TModel}.SubmitResult"/> property.
        /// </param>
        public static void ProcessCustomServerResponse(this FormManagerBase manager, FormManagerSubmitResult result)
        {
            var xhr = result.XHR;

            if (xhr.IsJsonResponse)
            {
                try
                {
                    var postResult = JsonSerializer.Deserialize <PostFormHttpResult>(
                        xhr.ResponseText,
                        CaseInsensitiveJson
                        );

                    if (postResult.Success)
                    {
                        manager.SubmitResult = FormManagerSubmitResult.Success(result,
                                                                               postResult.Message ?? SUCCESS_MESSAGE);
                    }
                    else if (true == xhr.ExtraProperties?.ContainsKey("error"))
                    {
                        manager.SubmitResult = FormManagerSubmitResult.Failed(result,
                                                                              xhr.ExtraProperties["error"]?.ToString());
                    }
                    else
                    {
                        manager.SubmitResult = FormManagerSubmitResult.Failed(result,
                                                                              postResult.GetErrorDetails() ?? UNKNOWN_SUBMIT_ERROR);
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex);
                }
            }
            else if (manager.IsDebug && !result.Succeeded)
            {
                if (xhr.IsHtmlResponse)
                {
                    Console.WriteLine($"HTML result: {xhr.ResponseText}");
                }
                else
                {
                    Console.WriteLine($"Raw result: {xhr.ResponseText}");
                }
            }
        }
        private void HandleSubmitDone(FormManagerSubmitResult result)
        {
            // Succeeded means the server responded with a success status code.
            // But we still have to find out how the action really came out.
            if (result.Succeeded && result.XHR.IsJsonResponse)
            {
                try
                {
                    // Since the response is in JSON format, let's parse it and investigate.
                    var response = JsonSerializer.Deserialize <RegisterUserModelResult>(
                        result.XHR.ResponseText, CaseInsensitiveJson);

                    if (!response.Success)
                    {
                        manager.SubmitResult = FormManagerSubmitResult.Failed(result, response.Error);
                    }
                    else if (!string.IsNullOrEmpty(response.Message))
                    {
                        manager.SubmitResult = FormManagerSubmitResult.Success(result, response.Message);
                        if (response.SignedIn)
                        {
                            NavigationManager.NavigateTo("/account/update", true);
                        }
                        else
                        {
                            // invalidate the form by setting a new model
                            Model = new RegisterUserModel();
                            StateHasChanged();
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Trace.WriteLine(ex);
                }
            }
        }