diff --git a/cgap/static/search.js b/cgap/static/search.js index 925cd10..65f848a 100644 --- a/cgap/static/search.js +++ b/cgap/static/search.js @@ -20,11 +20,51 @@ app.component('ProposalSearch', { return [] } + // Create a dictionary of of org + proposal objects + const proposalGroupsObject = this.matches.reduce( + function (accumulator, match) { + if (!(match.organization.id in accumulator)) { + accumulator[match.organization.id] = { + organization: match.organization, + proposals: [], + matches: [], + } + } + accumulator[match.organization.id].proposals.push(match); + return accumulator; + }, + {}, + ) + const proposalGroups = Object.values(proposalGroupsObject) + .map((group) => { + if (hasMatch(group.organization.name, this.inputValue)) { + group.matches.push("Org Name"); + } + if (hasMatch(group.organization.website, this.inputValue)) { + group.matches.push("Website"); + } + if (hasMatch(group.organization.address, this.inputValue)) { + group.matches.push("Address"); + } + if (hasMatch(group.organization.phone, this.inputValue)) { + group.matches.push("Phone"); + } + if (hasMatch(group.organization.ceo_name, this.inputValue)) { + group.matches.push("CEO"); + } + + const matchedProposals = group.proposals.filter((proposal) => { + return hasMatch(proposal.description, this.inputValue) + || hasMatch(proposal.name, this.inputValue); + }) + if (matchedProposals.length > 0) { + group.matches.push("Proposal"); + } + return group; + }) + // We want to filter here as well to prevent common UX lag between API requests - return this.matches.filter((match) => { - return hasMatch(match.organization.name, this.inputValue) - || hasMatch(match['description'], this.inputValue); - }); + return proposalGroups.filter((match) => { return match.matches.length !== 0; }); }, hasMatches() { return this.filteredMatches.length >= 0 && this.inputValue !== ''; @@ -58,27 +98,35 @@ app.component('ProposalSearch', { handleInput(event) { this.fetchProposals(event.target.value); }, - generateViewUrl(id) { + generateProposalViewUrl(id) { return `/proposal/${id}`; + }, + generateOrganizationViewUrl(id) { + return `/organization/${id}`; } }, template: ` -