Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
6.2 kB
2
Indexable
Never
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Components;
using MudBlazor;
using NutriPlanAI.Client.HttpRepository;
using NutriPlanAI.Shared.Models;

namespace NutriPlanAI.Client.Components.Forms
{
    public partial class PersonalDetailsForm : IBrowserViewportObserver
    {
        Guid IBrowserViewportObserver.Id { get; } = Guid.NewGuid();
        [Inject]
        private IUserInfoHttpRepository UserInfoHttpRepository { get; set; }
        [Inject]
        private IBrowserViewportService BrowserViewportService { get; set; }

        private RegisterAccountForm model = new();
        private bool toggleVertical;
        private bool formError = false;
        private string errorMessage = string.Empty;
        private bool _loading = false;
        private string searchConditionsString = string.Empty;
        private string searchAllergenString = string.Empty;
        private List<KrogerLocationDto>? krogerLocations = null;
        private List<string> preExistingConditions = new() { "Smoker", "Cancer", "Heart Disease", " Diabetes", "Asthma", "Depression", "Obesity", "Blindness", "Lactose Intolerant" };
        private List<string> fitnessLevels = new() { "Sedentary", "Beginner", "Intermediate", "Advanced", "Elite" };
        private List<string> fitnessGoals = new() { "Weight Loss", "Recomp", "Build Muscle", "Maintain" };
        private List<string> dietaryPreferences = new() { "None", "Vegetarian", "Vegan", "Pescatarian", "Keto" };
        private List<string> allergens = new() { "Milk", "Eggs", "Peanuts", "Soy", "Wheat" };

        protected override Task OnInitializedAsync()
        {


            return base.OnInitializedAsync();
        }

        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            if (firstRender)
            {
                await BrowserViewportService.SubscribeAsync(this, true);
            }
            await base.OnAfterRenderAsync(firstRender);
        }
        async Task IBrowserViewportObserver.NotifyBrowserViewportChangeAsync(BrowserViewportEventArgs browserViewportEventArgs)
        {
            var breakpoint = browserViewportEventArgs.Breakpoint;
            toggleVertical = breakpoint == Breakpoint.Xs || breakpoint == Breakpoint.Sm;
            await InvokeAsync(StateHasChanged);
        }
        private async void OnValidSubmit()
        {
            _loading = true;
            StateHasChanged();
            var res = await UserInfoHttpRepository.RegisterUserAsync(new UserInfoDto
            {
                Name = model.Name,
                Birthdate = model.Birthdate,
                Gender = model.Gender,
                FavoriteKrogerLocationId = model.Location.LocationId,
                Height = model.HeightFeet * 12 + model.HeightInches,
                Weight = model.Weight,
                FitnessLevel = model.FitnessLevel,
                FitnessGoal = model.FitnessGoal,
                PreExistingConditions = model.PreExistingConditions.ToList(),
                DietaryPreference = model.DietaryPreference,
                Allergens = model.Allergens.ToList()

            });
            if (res.Succeeded)
            {
                formError = false;
            }
            else
            {
                formError = true;
                errorMessage = res.Message;
            }
            _loading = false;
            StateHasChanged();
        }

        private void OnInValidSubmit()
        {
            formError = true;
            StateHasChanged();
        }

        private void LocationSearchHandler(List<KrogerLocationDto>? locations)
        {
            krogerLocations = locations;
            StateHasChanged();
        }

        private async Task<IEnumerable<KrogerLocationDto>> SearchKrogers(string value, CancellationToken token)
        {
            await Task.Delay(5, token);

            if (string.IsNullOrEmpty(value)) return krogerLocations ?? new();

            return krogerLocations.Where(l =>
                        $"{l.Name} {l.AddressLine} {l.City} {l.State} {l.ZipCode}"
                            .Contains(value, StringComparison.InvariantCultureIgnoreCase));
        }
        private bool SearchConditionFunc(st+ring str)
        {
            if (string.IsNullOrWhiteSpace(searchConditionsString))
            {
                return true;
            }
            if (str.Contains(searchConditionsString, StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
            return false;
        }

        private bool SearchAllergenFunc(string str)
        {
            if (string.IsNullOrWhiteSpace(searchAllergenString))
            {
                return true;
            }
            if (str.Contains(searchAllergenString, StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
            return false;
        }

        public class RegisterAccountForm
        {
            [Required]
            public string Name { get; set; }
            [Required]
            public DateTime? Birthdate { get; set; }
            [Required]
            public string? Gender { get; set; }
            [Required]
            [Range(2, 8)]
            public int? HeightFeet { get; set; }
            [Required]
            [Range(0, 11)]
            public int? HeightInches { get; set; }
            [Required]
            [Range(40, 1250)]
            public int? Weight { get; set; }
            [Required]
            public KrogerLocationDto Location { get; set; } = null;
            public HashSet<string> PreExistingConditions { get; set; } = new();
            [Required]
            public string FitnessLevel { get; set; }
            [Required]
            public string FitnessGoal { get; set; }
            [Required]
            public string DietaryPreference { get; set; }
            public HashSet<string> Allergens { get; set; } = new();
        }
    }
}
Leave a Comment