Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue 2448] use v1 search endpoint and frontend fetch pattern refactors #2518

Open
wants to merge 9 commits into
base: feature/frontend-opensearch
Choose a base branch
from

Conversation

doug-s-nava
Copy link
Collaborator

Summary

Fixes #2448

Time to review: 45 mins

Changes proposed

DISCLAIMER: there is a lot of scope creep in this PR. Feel free to push back on anything you feel goes to far here.

The primary purpose of this change is to change the search API to use the v1 version of the search endpoint in order to activate the use of Open Search on the backend. This purpose is accomplished in the first commit, which can be split into a separate PR or commit if called for.

The rest of the PR contains refactors to the API fetch system that:

  • should reduce redundancy and make better use of the classical setup in place here
  • makes better use of available TS functionality
  • better encapsulates domain specific logic (eg moving search specific functionality out of the base class)
  • fixes bugs

Context for reviewers

Test Steps

  1. visit the search page, and perform all manner of searches with different filters, queries, and sort options
  2. VALIDATE: everything works as expected
  3. visit various opportunity pages
  4. VALIDATE: everything works as expected

Additional information

Screenshots, GIF demos, code examples or output to help show the changes working as expected.

@@ -8,6 +8,7 @@
"scripts": {
"build": "next build",
"dev": "next dev",
"debug": "NODE_OPTIONS='--inspect' next dev",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not able to get this to work to use the chrome node debugger, and ended up using the VS code debugger, which did work to debug node based / server side next functions. If anyone else can get this to work, we should probably keep this, or we can just get rid of it.

see vercel/next.js#48767 and https://nextjs.org/docs/pages/building-your-application/configuring/debugging

@@ -15,32 +15,26 @@ import {
ValidationError,
} from "src/errors";
import { QueryParamData } from "src/services/search/searchfetcher/SearchFetcher";
// TODO (#1682): replace search specific references (since this is a generic API file that any
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe I've accomplished this now, though the ticket mentioned here was already closed.

// Root path of API resource without leading slash.
abstract get basePath(): string;
// Root path of API resource without leading slash, can be overridden by implementing API classes as necessary
get basePath() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we are using the same basePath everywhere, I figured we may as well define it at a higher level

return headers;
}

/**
* Send an API request.
*/
async request(
async request<ResponseType extends APIResponse>(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a generic to get around having to hardcode search specific typing

@@ -149,14 +132,14 @@ export function createRequestUrl(
let url = [...cleanedPaths].join("/");
if (method === "GET" && body && !(body instanceof FormData)) {
// Append query string to URL
const body: { [key: string]: string } = {};
const newBody: { [key: string]: string } = {};
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was completely broken before, but was unused so nobody noticed

method: ApiMethod,
basePath: string,
namespace: string,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can reference these as class variables, no need to pass in

eligibility: "applicant_type",
agency: "agency",
category: "funding_category",
} as const;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these types are all to support the refactor of buildFilters, which is... admittedly a lot of scope creep for this ticket

async searchOpportunities(searchInputs: QueryParamData) {
const { query } = searchInputs;
const filters = this.buildFilters(searchInputs);
const pagination = this.buildPagination(searchInputs);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these function do not reference the class at all, so can be split out

if (sortby) {
sort_direction = sortby?.endsWith("Desc") ? "descending" : "ascending";
}
const sort_direction =
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the diff is tough here since I removed these functions from the class, but the only real change I made to the pagination function is here, simplifying the sort_direction logic slightly.

});

describe("searchOpportunities", () => {
beforeEach(() => {
global.fetch = mockFetch({});
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test was mocking down to the fetch level, while the existing opportunity listing API test was just mocking the request function on the class. In the interest of using inheritance to our benefit and keeping the tests somewhat uniform, I changed this to use the request mocking approach, and left the fetch mocking approach for the base class

@doug-s-nava doug-s-nava marked this pull request as ready for review October 18, 2024 15:18
@doug-s-nava doug-s-nava changed the base branch from main to feature/frontend-opensearch October 18, 2024 15:23
Copy link
Collaborator

@acouch acouch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cleanup looks great. Out of scope but we could also remove the mock API at some point as I don't think we are using it anywhere and it was never fully built AFAICT.

@github-actions github-actions bot added the ci/cd label Oct 18, 2024
@doug-s-nava doug-s-nava force-pushed the dschrashun/2448-v1-search-endpoint branch 2 times, most recently from 2695b69 to b41c04c Compare October 18, 2024 20:55
… timing racy condition thing with promise.all
@doug-s-nava doug-s-nava force-pushed the dschrashun/2448-v1-search-endpoint branch from b41c04c to 2915e8b Compare October 18, 2024 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Switch frontend to use v1 Search endpoint
2 participants