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-568): Bugfix/clear date filters #495

Merged
merged 16 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
1 change: 1 addition & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
.vscode

# dependencies
/node_modules
Expand Down
23 changes: 5 additions & 18 deletions frontend/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
{
paulushcgcj marked this conversation as resolved.
Show resolved Hide resolved
"workbench.colorCustomizations": {
"activityBar.activeBackground": "#1f6fd0",
"activityBar.background": "#1f6fd0",
"activityBar.foreground": "#e7e7e7",
"activityBar.inactiveForeground": "#e7e7e799",
"activityBarBadge.background": "#ee90bb",
"activityBarBadge.foreground": "#15202b",
"commandCenter.border": "#e7e7e799",
"sash.hoverBorder": "#1f6fd0",
"statusBar.background": "#1857a4",
"statusBar.foreground": "#e7e7e7",
"statusBarItem.hoverBackground": "#1f6fd0",
"statusBarItem.remoteBackground": "#1857a4",
"statusBarItem.remoteForeground": "#e7e7e7",
"titleBar.activeBackground": "#1857a4",
"titleBar.activeForeground": "#e7e7e7",
"titleBar.inactiveBackground": "#1857a499",
"titleBar.inactiveForeground": "#e7e7e799"
},
"titleBar.activeForeground": "#000",
"titleBar.inactiveForeground": "#000000cc",
"titleBar.activeBackground": "#5d9857",
"titleBar.inactiveBackground": "#5d9857",
},
"peacock.color": "#1857a4"
}
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,19 +19,18 @@ 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(() => {
Expand Down Expand Up @@ -62,6 +61,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 +275,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 +314,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 +349,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,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useState} from "react";
import "./OpeningsSearchBar.scss";
import { Search, Button, FlexGrid, Row, Column, DismissibleTag, InlineNotification } from "@carbon/react";
import * as Icons from "@carbon/icons-react";
Expand All @@ -21,7 +21,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 +45,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
10 changes: 10 additions & 0 deletions frontend/src/utils/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@ export const dateStringToISO = (date: string): string => {
}
return '';
};

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