using JobsJobsJobs.Shared; using JobsJobsJobs.Shared.Api; using Microsoft.AspNetCore.Components; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Http.Json; using System.Threading.Tasks; namespace JobsJobsJobs.Client.Pages.Citizen { /// /// Profile edit page (called EditProfile so as not to create naming conflicts) /// public partial class EditProfile : ComponentBase { /// /// Counter for IDs when "Add a Skill" button is clicked /// private int _newSkillCounter = 0; /// /// A flag that indicates all the required API calls have completed, and the form is ready to be displayed /// private bool AllLoaded { get; set; } = false; /// /// The form for this page /// private ProfileForm ProfileForm { get; set; } = new ProfileForm(); /// /// All continents /// private IEnumerable Continents { get; set; } = Enumerable.Empty(); /// /// Error messages from API access /// private IList ErrorMessages { get; } = new List(); /// /// HTTP client instance to use for API access /// [Inject] private HttpClient Http { get; set; } = default!; /// /// Application state /// [Inject] private AppState State { get; set; } = default!; protected override async Task OnInitializedAsync() { ServerApi.SetJwt(Http, State); var continentTask = ServerApi.RetrieveMany(Http, "continent/all"); var profileTask = ServerApi.RetrieveProfile(Http, State); var skillTask = ServerApi.RetrieveMany(Http, "profile/skills"); await Task.WhenAll(continentTask, profileTask, skillTask); if (continentTask.Result.IsOk) { Continents = continentTask.Result.Ok; } else { ErrorMessages.Add(continentTask.Result.Error); } if (profileTask.Result.IsOk) { ProfileForm = (profileTask.Result.Ok == null) ? new ProfileForm() : ProfileForm.FromProfile(profileTask.Result.Ok); } else { ErrorMessages.Add(profileTask.Result.Error); } if (skillTask.Result.IsOk) { foreach (var skill in skillTask.Result.Ok) { ProfileForm.Skills.Add(new SkillForm { Id = skill.Id.ToString(), Description = skill.Description, Notes = skill.Notes ?? "" }); } if (ProfileForm.Skills.Count == 0) AddNewSkill(); } else { ErrorMessages.Add(skillTask.Result.Error); } AllLoaded = true; } /// /// Add a new skill to the form /// private void AddNewSkill() => ProfileForm.Skills.Add(new SkillForm { Id = $"new{_newSkillCounter++}" }); /// /// Remove the skill for the given ID /// /// The ID of the skill to remove private void RemoveSkill(string skillId) => ProfileForm.Skills.Remove(ProfileForm.Skills.First(s => s.Id == skillId)); /// /// Save changes to the current profile /// public async Task SaveProfile() { // Remove any skills left blank var blankSkills = ProfileForm.Skills .Where(s => string.IsNullOrEmpty(s.Description) && string.IsNullOrEmpty(s.Notes)) .ToList(); foreach (var blankSkill in blankSkills) ProfileForm.Skills.Remove(blankSkill); var res = await Http.PostAsJsonAsync("/api/profile/save", ProfileForm); if (res.IsSuccessStatusCode) { // TODO: success notification } else { // TODO: probably not the best way to handle this... ErrorMessages.Add(await res.Content.ReadAsStringAsync()); } } } }