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

fix(SILVA-535): updates to cleard advanced search component #464

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ dist
# The below expression will prevent user specific configuration files from being added to the repository
config/application-dev-*.yml
.checkstyle


temp/
config/*.jks
config/*.jks
zscaler-cgi.crt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { render, screen } from "@testing-library/react";
import { getByTestId, render, screen, waitFor } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import AdvancedSearchDropdown from "../../../../components/SilvicultureSearch/Openings/AdvancedSearchDropdown";
import { useOpeningFiltersQuery } from "../../../../services/queries/search/openingQueries";
import { useOpeningsSearch } from "../../../../contexts/search/OpeningsSearch";
import React from "react";
import React, { act } from "react";

// Mocking the toggleShowFilters function
const toggleShowFilters = vi.fn();
Expand All @@ -21,45 +21,42 @@ vi.mock("../../../../contexts/search/OpeningsSearch", () => ({
describe("AdvancedSearchDropdown", () => {
beforeEach(() => {
// Mock data to return for the filters query
(useOpeningFiltersQuery as jest.Mock).mockReturnValue({
(useOpeningFiltersQuery as vi.Mock).mockReturnValue({
data: {
categories: [{
code: "FTML",
description: "Forest Tenure - Major Licensee"
}, {
code: "CONT",
description: "SP as a part of contractual agreement"
}
],
categories: [{ code: "FTML", description: "" }, { code: "CONT", description: "" }],
orgUnits: [{
orgUnitCode:'DCK',
orgUnitName: 'Chilliwack Natural Resource District'
}, {
orgUnitCode:'DCR',
orgUnitName: 'Campbell River Natural Resource District'
}
],
"orgUnitNo": 15,
"orgUnitCode": "DCK",
"orgUnitName": "Chilliwack Natural Resource District"
},
{
"orgUnitNo": 43,
"orgUnitCode": "DCR",
"orgUnitName": "Campbell River Natural Resource District"
}],
dateTypes: ["Disturbance", "Free Growing"],
},
isLoading: false,
isError: false,
});

// Mock implementation of useOpeningsSearch context
(useOpeningsSearch as jest.Mock).mockReturnValue({
(useOpeningsSearch as vi.Mock).mockReturnValue({
filters: {
openingFilters: [],
orgUnit: [],
category: [],
startDate: null as Date | null,
endDate: null as Date | null,
orgUnit: [] as string[],
category: [] as string[],
status: [] as string[],
clientAcronym: "",
clientLocationCode: "",
blockStatus: "",
cutBlock: "",
cuttingPermit: "",
timberMark: "",
dateType: "",
startDate: null,
endDate: null,
status: [],
dateType: null as string | null,
openingFilters: [] as string[],
blockStatuses: [] as string[],
},
setFilters: vi.fn(),
clearFilters: vi.fn(),
Expand All @@ -79,23 +76,40 @@ describe("AdvancedSearchDropdown", () => {
).toBeInTheDocument();
});

it("displays the advanced search dropdown", () => {
render(<AdvancedSearchDropdown toggleShowFilters={toggleShowFilters} />);
expect(screen.getByText("Opening Filters")).toBeInTheDocument();
it("displays the advanced search dropdown", async () => {
let container;
await act(async () => {
({ container } = render(<AdvancedSearchDropdown toggleShowFilters={toggleShowFilters} />));
});
const element = container.querySelector('.d-block');
expect(element).toBeDefined();
});

it("displays the advanced search dropdown with filters", () => {
render(<AdvancedSearchDropdown toggleShowFilters={toggleShowFilters} />);
expect(screen.getByText("Opening Filters")).toBeInTheDocument();
expect(screen.getByText("Org Unit")).toBeInTheDocument();
expect(screen.getByText("Category")).toBeInTheDocument();
expect(screen.getByText("Client acronym")).toBeInTheDocument();
expect(screen.getByText("Client location code")).toBeInTheDocument();
expect(screen.getByText("Cut block")).toBeInTheDocument();
expect(screen.getByText("Cutting permit")).toBeInTheDocument();
expect(screen.getByText("Timber mark")).toBeInTheDocument();
expect(screen.getByLabelText("Start Date")).toBeInTheDocument();
expect(screen.getByLabelText("End Date")).toBeInTheDocument();
expect(screen.getByText("Status")).toBeInTheDocument();
it("clears the date filters when all filters are cleared", async () => {
// Mock implementation of useOpeningsSearch context
(useOpeningsSearch as vi.Mock).mockReturnValue({
filters: {
startDate: "1978-01-01",
endDate: "1978-01-01",
orgUnit: [] as string[],
category: [] as string[],
status: [] as string[],
clientAcronym: "",
clientLocationCode: "",
blockStatus: "",
cutBlock: "",
cuttingPermit: "",
timberMark: "",
dateType: "Disturbance",
openingFilters: [] as string[],
blockStatuses: [] as string[]
},
});
let container;
await act(async () => {
({ container } = render(<AdvancedSearchDropdown toggleShowFilters={toggleShowFilters} />));
});
const element = container.querySelector('.d-block');
expect(element).toBeDefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ import * as Icons from "@carbon/icons-react";
import { useOpeningFiltersQuery } from "../../../../services/queries/search/openingQueries";
import { useOpeningsSearch } from "../../../../contexts/search/OpeningsSearch";
import { TextValueData, sortItems } from "../../../../utils/multiSelectSortUtils";
import { formatDateForDatePicker } from "../../../../utils/DateUtils";

interface AdvancedSearchDropdownProps {
toggleShowFilters: () => void; // Function to be passed as a prop
}

const AdvancedSearchDropdown: React.FC<AdvancedSearchDropdownProps> = () => {
const { filters, setFilters } = useOpeningsSearch();
//TODO: pass this to parent and just pass the values as props
const { filters, setFilters, clearFilters } = useOpeningsSearch();
const { data, isLoading, isError } = useOpeningFiltersQuery();

// Initialize selected items for OrgUnit MultiSelect based on existing filters
const [selectedOrgUnits, setSelectedOrgUnits] = useState<any[]>([]);
// Initialize selected items for category MultiSelect based on existing filters
const [selectedCategories, setSelectedCategories] = useState<any[]>([]);

useEffect(() => {
console.log("Use Effect in child is being called.", filters);
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this console.log here

// Split filters.orgUnit into array and format as needed for selectedItems
if (filters.orgUnit) {
const orgUnitsArray = filters.orgUnit.map((orgUnit: string) => ({
Expand Down Expand Up @@ -62,6 +62,7 @@ const AdvancedSearchDropdown: React.FC<AdvancedSearchDropdownProps> = () => {
setFilters(newFilters);
};


const handleMultiSelectChange = (group: string, selectedItems: any) => {
const updatedGroup = selectedItems.map((item: any) => item.value);
if (group === "orgUnit")
Expand Down Expand Up @@ -275,8 +276,8 @@ const AdvancedSearchDropdown: React.FC<AdvancedSearchDropdownProps> = () => {
selectedItem={
filters.dateType
? dateTypeItems.find(
(item: any) => item.value === filters.dateType
)
(item: any) => item.value === filters.dateType
)
: ""
}
label="Date type"
Expand Down Expand Up @@ -314,10 +315,11 @@ const AdvancedSearchDropdown: React.FC<AdvancedSearchDropdownProps> = () => {
size="md"
labelText="Start Date"
placeholder={
filters.startDate !== null
filters.startDate
? filters.startDate // Display the date in YYYY-MM-DD format
: "yyyy/MM/dd"
}
value={formatDateForDatePicker(filters.startDate)}
/>
</DatePicker>

Expand Down Expand Up @@ -348,6 +350,7 @@ const AdvancedSearchDropdown: React.FC<AdvancedSearchDropdownProps> = () => {
? filters.endDate // Display the date in YYYY-MM-DD format
: "yyyy/MM/dd"
}
value={formatDateForDatePicker(filters.endDate)}
/>
</DatePicker>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useState, useRef } from "react";
import "./OpeningsSearchBar.scss";
import { Search, Button, FlexGrid, Row, Column, DismissibleTag, InlineNotification } from "@carbon/react";
import * as Icons from "@carbon/icons-react";
import AdvancedSearchDropdown from "../AdvancedSearchDropdown";
import SearchFilterBar from "../SearchFilterBar";
import { useOpeningsSearch } from "../../../../contexts/search/OpeningsSearch";
import { countActiveFilters } from "../../../../utils/searchUtils";
import { callbackify } from "util";
import { c } from "vite/dist/node/types.d-aGj9QkWt";

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this, please. This was probably added by vscode while you were typing.

interface IOpeningsSearchBar {
onSearchClick: () => void;
Expand All @@ -21,7 +23,7 @@ const OpeningsSearchBar: React.FC<IOpeningsSearchBar> = ({
const [searchInput, setSearchInput] = useState<string>("");
const [filtersCount, setFiltersCount] = useState<number>(0);
const [filtersList, setFiltersList] = useState(null);
const { filters, clearFilters, searchTerm, setSearchTerm } = useOpeningsSearch();
const { filters, clearFilters, searchTerm, setSearchTerm, clearIndividualField } = useOpeningsSearch();

const toggleDropdown = () => {
setIsOpen(!isOpen);
Expand All @@ -45,7 +47,8 @@ const OpeningsSearchBar: React.FC<IOpeningsSearchBar> = ({
const activeFiltersCount = countActiveFilters(filters);
setFiltersCount(activeFiltersCount); // Update the state with the active filters count
setFiltersList(filters);
};
}

useEffect(() => {
handleFiltersChanged();
}, [filters]);
Expand Down
1 change: 1 addition & 0 deletions frontend/src/contexts/search/OpeningsSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const OpeningsSearchProvider: React.FC<{ children: ReactNode }> = ({ chil

// Function to clear individual filter field by key
const clearIndividualField = (key: string) => {
console.log("Clearing individual field", key);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove this console.log

setFilters((prevFilters) => ({
...prevFilters,
[key]: defaultFilters[key as keyof typeof defaultFilters],
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/utils/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ export const dateStringToISO = (date: string): string => {
}
return '';
};

export const formatDateForDatePicker = (date: any) => {
console.log("date format: ", date);
let year, month, day;
if (date) {
[year, month, day] = date.split("-");
return `${month}/${day}/${year}`;
} else {
return "";
}
};
Loading