-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨filter and sort CANs list by user (#2867)
- Loading branch information
Showing
10 changed files
with
167 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
frontend/src/components/CANs/CANTable/CANTableRow.helpers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Display active period in years | ||
* @param {number} activePeriod | ||
* @returns {string} | ||
*/ | ||
export const displayActivePeriod = (activePeriod) => { | ||
switch (activePeriod) { | ||
case 0: | ||
return "TBD"; | ||
case 1: | ||
return "1 year"; | ||
default: | ||
return `${activePeriod} years`; | ||
} | ||
}; |
22 changes: 22 additions & 0 deletions
22
frontend/src/components/CANs/CANTable/CANTableRow.helpers.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { displayActivePeriod } from "./CANTableRow.helpers"; | ||
|
||
describe("displayActivePeriod", () => { | ||
it('should return "TBD" when activePeriod is 0', () => { | ||
expect(displayActivePeriod(0)).toBe("TBD"); | ||
}); | ||
|
||
it('should return "1 year" when activePeriod is 1', () => { | ||
expect(displayActivePeriod(1)).toBe("1 year"); | ||
}); | ||
|
||
it('should return "X years" for any activePeriod greater than 1', () => { | ||
expect(displayActivePeriod(2)).toBe("2 years"); | ||
expect(displayActivePeriod(5)).toBe("5 years"); | ||
expect(displayActivePeriod(10)).toBe("10 years"); | ||
}); | ||
|
||
it("should handle negative values", () => { | ||
expect(displayActivePeriod(-2)).toBe("-2 years"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Sorts an array of CANs by obligateBy date in descending order. | ||
* @typedef {import("../../../components/CANs/CANTypes").CAN} CAN | ||
* @param {CAN[]} cans - The array of CANs to sort. | ||
* @param {boolean} myCANsUrl - The URL parameter to filter by "my-CANs". | ||
* @param {import("../../../components/Users/UserTypes").User} activeUser - The active user. | ||
* @returns {CAN[] | undefined} - The sorted array of CANs. | ||
*/ | ||
export const sortAndFilterCANs = (cans, myCANsUrl, activeUser) => { | ||
if (!cans || cans.length === 0) { | ||
return []; | ||
} | ||
let filteredCANs = myCANsUrl ? cans.filter((can) => can.portfolio.division_id === activeUser.division) : cans; | ||
|
||
return sortCANs(filteredCANs); | ||
}; | ||
|
||
/** | ||
* Sorts an array of CANs by obligateBy date in descending order. | ||
* @param {CAN[]} cans - The array of CANs to sort. | ||
* @returns {CAN[] | []} - The sorted array of CANs. | ||
*/ | ||
const sortCANs = (cans) => { | ||
if (!cans) { | ||
return []; | ||
} | ||
|
||
return [...cans].sort((a, b) => { | ||
const dateA = a.obligate_by ? new Date(a.obligate_by).getTime() : 0; | ||
const dateB = b.obligate_by ? new Date(b.obligate_by).getTime() : 0; | ||
return dateB - dateA; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { sortAndFilterCANs } from "./CanList.helpers"; | ||
|
||
describe("sortAndFilterCANs", () => { | ||
const mockUser = { division: 1 }; | ||
|
||
const mockCANs = [ | ||
{ id: 1, obligate_by: "2023", portfolio: { division_id: 1 } }, | ||
{ id: 2, obligate_by: "2023", portfolio: { division_id: 2 } }, | ||
{ id: 3, obligate_by: "2023", portfolio: { division_id: 1 } }, | ||
{ id: 4, obligate_by: null, portfolio: { division_id: 1 } } | ||
]; | ||
|
||
it("should return an empty array when input is null or empty", () => { | ||
expect(sortAndFilterCANs(null, false, mockUser)).toEqual([]); | ||
expect(sortAndFilterCANs([], false, mockUser)).toEqual([]); | ||
}); | ||
|
||
it("should sort CANs by obligate_by date in descending order", () => { | ||
const result = sortAndFilterCANs(mockCANs, false, mockUser); | ||
expect(result.map((can) => can.id)).toEqual([1, 2, 3, 4]); | ||
}); | ||
|
||
it("should filter CANs by user division when myCANsUrl is true", () => { | ||
const result = sortAndFilterCANs(mockCANs, true, mockUser); | ||
expect(result.length).toBe(3); | ||
expect(result.every((can) => can.portfolio.division_id === 1)).toBe(true); | ||
}); | ||
|
||
it("should not filter CANs when myCANsUrl is false", () => { | ||
const result = sortAndFilterCANs(mockCANs, false, mockUser); | ||
expect(result.length).toBe(4); | ||
}); | ||
|
||
it("should handle CANs with null obligate_by dates", () => { | ||
const result = sortAndFilterCANs(mockCANs, false, mockUser); | ||
expect(result[result.length - 1].id).toBe(4); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters