Skip to content

Commit

Permalink
Feat/497 connect backend and frontend for client search (#831)
Browse files Browse the repository at this point in the history
* fix: client search on edit seedlot info

* feat: integrate endpoint

* feat: refactor client search table and modal

* fix: better constants
  • Loading branch information
craigyu authored Feb 6, 2024
1 parent a950eab commit 0ad4cc3
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 395 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,15 @@ public List<ForestClientLocationDto> fetchLocationsByClientNumber(
"fetchLocationsByClientNumber",
page);

if (!shouldFetchAll) {
int responseSize = response.getBody().size();

if (!shouldFetchAll || responseSize == 0) {
return response.getBody();
}

totalCount = Integer.parseInt(response.getHeaders().get("x-total-count").get(0));

totalFetched += response.getBody().size();
totalFetched += responseSize;

page += 1;

Expand Down Expand Up @@ -285,9 +287,16 @@ public List<ForestClientDto> fetchClientsByClientName(String clientName) {
"fetchClientsByClientName",
page);

int responseSize = response.getBody().size();

// Empty response will not have a x-total-count header so we return the empty array here.
if (responseSize == 0) {
return result;
}

totalCount = Integer.parseInt(response.getHeaders().get("x-total-count").get(0));

totalFetched += response.getBody().size();
totalFetched += responseSize;

page += 1;

Expand Down
160 changes: 7 additions & 153 deletions frontend/src/api-service/forestClientsAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ApiConfig from './ApiConfig';
import api from './api';
import { ForestClientType } from '../types/ForestClientTypes/ForestClientType';
import ApplicantAgenciesItems from '../mock-server/fixtures/ApplicantAgenciesItems';
import { ForestClientDisplayType } from '../types/ForestClientTypes/ForestClientDisplayType';
import { ForestClientSearchType } from '../types/ForestClientTypes/ForestClientSearchType';
import { ClientSearchOptions } from '../components/ApplicantAgencyFields/ClientSearchModal/definitions';

export const getForestClientLocation = (clientNumber: string, locationCode: string) => {
Expand Down Expand Up @@ -37,159 +37,13 @@ export const getAllAgencies = (): string[] => {
return options;
};

// Leaving this here for testing/demo on the FE
// REMOVE AFTER REAL API IS DONE
const forestClientMockData: Array<ForestClientDisplayType> = [
{
acronym: 'WFP',
number: '00149081',
fullName: 'WESTERN FOREST PRODUCTS INC.',
locationCode: '28',
location: '#118-1334 ISLAND HIGHWAY',
city: 'CAMPBELL RIVER'
},
{
acronym: 'WFP',
number: '00149081',
fullName: 'WESTERN FOREST PRODUCTS INC.',
locationCode: '25',
location: '#201 - 65 FRONT STREET',
city: 'NANAIMO'
},
{
acronym: 'WFP',
number: '00149081',
fullName: 'WESTERN FOREST PRODUCTS INC.',
locationCode: '26',
location: '800 1055 WEST GEORGIA STREET',
city: 'VANCOUVER'
},
{
acronym: 'MOF',
number: '00012797',
fullName: 'MINISTRY OF FORESTS',
locationCode: '00',
location: '18793 32ND AVENUE',
city: 'SURREY'
},
{
acronym: 'MOF',
number: '00012797',
fullName: 'MINISTRY OF FORESTS',
locationCode: '02',
location: '17000 DOMANO BLVD',
city: 'PRINCE GEORGE'
},
{
acronym: 'MOF',
number: '00012797',
fullName: 'MINISTRY OF FORESTS',
locationCode: '07',
location: '4300 NORTH ROAD',
city: 'VICTORIA'
},
{
acronym: 'MOF',
number: '00012797',
fullName: 'MINISTRY OF FORESTS',
locationCode: '08',
location: '9800 140TH STREET',
city: 'SURREY'
},
{
acronym: 'TBA',
number: '00132184',
fullName: 'TIMBER SALES MANAGER BABINE',
locationCode: '01',
location: 'BURNS LAKE TSO',
city: 'BURNS LAKE'
},
{
acronym: 'TBA',
number: '00132184',
fullName: 'TIMBER SALES MANAGER BABINE',
locationCode: '00',
location: 'BURNS LAKE TIMBER SALES OFFICE BOX 999',
city: 'BURNS LAKE'
},
{
acronym: 'TCC',
number: '00132197',
fullName: 'TIMBER SALES MANAGER CARIBOO-CHILCOTIN',
locationCode: '02',
location: 'TCC - WILLIAMS LAKE FIELD TEAM 200-640 BORLAND ST',
city: 'WILLIAMS LAKE'
},
{
acronym: 'TCC',
number: '00132197',
fullName: 'TIMBER SALES MANAGER CARIBOO-CHILCOTIN',
locationCode: '01',
location: 'TCC - QUESNEL FIELD TEAM 322 JOHNSTON AVENUE',
city: 'QUESNEL'
},
{
acronym: 'TCC',
number: '00132197',
fullName: 'TIMBER SALES MANAGER CARIBOO-CHILCOTIN',
locationCode: '00',
location: 'CARIBOO CHILCOTIN TIMBER SALES OFFICE 200-640 BORLAND ST',
city: 'WILLIAMS LAKE'
},
{
acronym: 'TCH',
number: '00132186',
fullName: 'TIMBER SALES MANAGER CHINOOK',
locationCode: '03',
location: '7077 DUNCAN STREET',
city: 'POWELL RIVER'
},
{
acronym: 'TCH',
number: '00132186',
fullName: 'TIMBER SALES MANAGER CHINOOK',
locationCode: '01',
location: '101-42000 LOGGERS LANE',
city: 'SQUAMISH'
},
{
acronym: 'TCH',
number: '00132186',
fullName: 'TIMBER SALES MANAGER CHINOOK',
locationCode: '02',
location: '1229 CEMETERY ROAD PO BOX 39',
city: 'QUEEN CHARLOTTE CITY'
},
{
acronym: 'TCH',
number: '00132186',
fullName: 'TIMBER SALES MANAGER CHINOOK',
locationCode: '00',
location: '46360 AIRPORT ROAD',
city: 'CHILLIWACK'
}
];

export const searchForestClients = (
searchWord: string,
searchOption: ClientSearchOptions
searchQuery: string,
searchType: ClientSearchOptions
) => {
const returnPromise = new Promise((resolve, reject) => {
setTimeout(() => {
const filteredResults = forestClientMockData.filter(
(client: ForestClientDisplayType) => client[searchOption].includes(searchWord.toUpperCase())
);
if (searchWord === 'all') {
resolve(forestClientMockData);
} else if (searchWord !== 'error') {
resolve(filteredResults);
} else {
reject(new Error('Error on fake backend :('));
}
}, 3000);
});

returnPromise.then((res: any): ForestClientDisplayType[] => res.data);
const url = new URL(`${ApiConfig.forestClient}/search`);
url.searchParams.append('type', searchType);
url.searchParams.append('query', searchQuery);

return returnPromise;
return api.get(url.toString()).then((res) => (res.data) as ForestClientSearchType[]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ const ClientSearchFields = ({
className="client-search-button"
disabled={mutationFn.isLoading}
onClick={
() => mutationFn.mutate({ word: searchWord, option: searchOption.option })
searchWord.length
? () => mutationFn.mutate()
: null
}
>
Search
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ClientSearchDropdown } from './definitions';

export const clientSearchOptions: Array<ClientSearchDropdown> = [
{
label: 'Full name',
option: 'client_name'
},
{
label: 'Acronym',
option: 'acronym'
},
{
label: 'Number',
option: 'client_number'
}
];

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
import { UseMutationResult } from '@tanstack/react-query';
import { ForestClientSearchType } from '../../../types/ForestClientTypes/ForestClientSearchType';

export interface ClientSearchModalProps {
linkText: string;
modalLabel: string;
applySelectedClient: Function;
}

export type ClientSearchOptions = 'acronym' | 'fullName' | 'number';
export type ClientSearchOptions = 'acronym' | 'client_name' | 'client_number';

export type ClientSearchDropdown = {
label: string;
option: ClientSearchOptions
};

export type MutationParams = {
word: string;
option: ClientSearchOptions
};

export type ClientSearchFieldsProps = {
searchWord: string,
setSearchWord: Function,
searchOption: ClientSearchDropdown,
setSearchOption: Function,
mutationFn: UseMutationResult<unknown, Error, MutationParams, unknown>
mutationFn: UseMutationResult<ForestClientSearchType[], unknown, void, unknown>
};
Loading

0 comments on commit 0ad4cc3

Please sign in to comment.