using JobsJobsJobs.Shared; using JobsJobsJobs.Shared.Api; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.WebUtilities; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace JobsJobsJobs.Client.Pages.Profiles { public partial class Search : ComponentBase { /// /// Whether a search has been performed /// private bool Searched { get; set; } = false; /// /// Indicates whether a request for matching profiles is in progress /// private bool Searching { get; set; } = false; /// /// The search criteria /// private ProfileSearch Criteria { get; set; } = new ProfileSearch(); /// /// Error messages encountered while searching for profiles /// private IList ErrorMessages { get; } = new List(); /// /// All continents /// private IEnumerable Continents { get; set; } = Enumerable.Empty(); /// /// The search results /// private IEnumerable SearchResults { get; set; } = Enumerable.Empty(); protected override async Task OnInitializedAsync() { Continents = await state.GetContinents(http); // Determine if we have searched before var query = QueryHelpers.ParseQuery(nav.ToAbsoluteUri(nav.Uri).Query); if (query.TryGetValue("Searched", out var searched)) { Searched = Convert.ToBoolean(searched); void setPart(string part, Action func) { if (query.TryGetValue(part, out var partValue)) func(partValue); } setPart(nameof(Criteria.ContinentId), x => Criteria.ContinentId = x); setPart(nameof(Criteria.Skill), x => Criteria.Skill = x); setPart(nameof(Criteria.BioExperience), x => Criteria.BioExperience = x); setPart(nameof(Criteria.RemoteWork), x => Criteria.RemoteWork = x); await RetrieveProfiles(); } } /// /// Do a search /// /// This navigates with the parameters in the URL; this should trigger a search private async Task DoSearch() { var query = SearchQuery(); query.Add("Searched", "True"); nav.NavigateTo(QueryHelpers.AddQueryString("/profile/search", query)); await RetrieveProfiles(); } /// /// Retreive profiles matching the current search criteria /// private async Task RetrieveProfiles() { Searching = true; var searchResult = await ServerApi.RetrieveMany(http, QueryHelpers.AddQueryString("profile/search", SearchQuery())); if (searchResult.IsOk) { SearchResults = searchResult.Ok; } else { ErrorMessages.Add(searchResult.Error); } Searched = true; Searching = false; } /// /// Return a CSS class if the user is actively seeking work /// /// The result in question /// A string with the appropriate CSS class, if actively seeking work private static string? IsSeeking(ProfileSearchResult profile) => profile.SeekingEmployment ? "font-weight-bold" : null; /// /// Return "Yes" for true and "No" for false /// /// The condition in question /// "Yes" for true, "No" for false private static string YesOrNo(bool condition) => condition ? "Yes" : "No"; /// /// Create a search query string from the currently-entered criteria /// /// The query string for the currently-entered criteria private IDictionary SearchQuery() { var dict = new Dictionary(); if (Criteria.IsEmptySearch) return dict; void part(string name, Func func) { if (!string.IsNullOrEmpty(func(Criteria))) dict.Add(name, func(Criteria)); } part(nameof(Criteria.ContinentId), it => it.ContinentId); part(nameof(Criteria.Skill), it => it.Skill); part(nameof(Criteria.BioExperience), it => it.BioExperience); part(nameof(Criteria.RemoteWork), it => it.RemoteWork); return dict; } } }