diff --git a/ui/src/Components/Dashboard/Dashboard.js b/ui/src/Components/Dashboard/Dashboard.js index 4d8b779..2d4e33a 100644 --- a/ui/src/Components/Dashboard/Dashboard.js +++ b/ui/src/Components/Dashboard/Dashboard.js @@ -1,10 +1,5 @@ -import Table from "./Table/Table" +import Table from "./Table/Table"; export default function Dashboard({ featureFlags, headers }) { - return ( - - )} - \ No newline at end of file + return
; +} diff --git a/ui/src/Components/Dashboard/Table/Table.js b/ui/src/Components/Dashboard/Table/Table.js index bdf61ea..580fe5e 100644 --- a/ui/src/Components/Dashboard/Table/Table.js +++ b/ui/src/Components/Dashboard/Table/Table.js @@ -1,39 +1,30 @@ -import {useState} from "react" -import TableHeader from "./TableHeader/TableHeader" -import TableRow from "./TableRow/TableRow" -import { compareValues } from "../../../Utils/helpers" +import { useState } from "react"; +import TableHeader from "./TableHeader/TableHeader"; +import TableRow from "./TableRow/TableRow"; +import { compareValues } from "../../../Utils/helpers"; export default function Table({ featureFlags, headers }) { + const [rowData, setRowData] = useState(featureFlags); -const [rowData,setRowData] = useState(featureFlags) - -const handleSorting = (type,onColumn) => { - - let rows = rowData; - rows.sort(compareValues(onColumn,type)) - setRowData([...rows]) - -} + const handleSorting = (type, onColumn) => { + let rows = rowData; + rows.sort(compareValues(onColumn, type)); + setRowData([...rows]); + }; return ( - -
- +
+ - {rowData.map(featureFlag => + {rowData.map((featureFlag) => ( - )} + /> + ))}
- ) -} \ No newline at end of file + ); +} diff --git a/ui/src/Components/Dashboard/Table/TableHeader/SortCursor.js b/ui/src/Components/Dashboard/Table/TableHeader/SortCursor.js index 8b4c6b7..bc77077 100644 --- a/ui/src/Components/Dashboard/Table/TableHeader/SortCursor.js +++ b/ui/src/Components/Dashboard/Table/TableHeader/SortCursor.js @@ -1,33 +1,21 @@ -import { useState } from "react"; import { TABLE } from "../../../../Constant/constant"; -export default function SortCursor({ header, handleSorting }) { - const [currentClickedCursor, setCurrentClickedCursor] = useState(null); - - const onCursorClick = (clickedCursorType, headerName) => { - setCurrentClickedCursor(clickedCursorType); - handleSorting(clickedCursorType, headerName); - }; - +export default function SortCursor({ currentClickedCursor }) { return (
{ - onCursorClick(TABLE.ASCENDING, header); - }} >
{ - onCursorClick(TABLE.DESCENDING, header); - }} >
); diff --git a/ui/src/Components/Dashboard/Table/TableHeader/TableHeader.js b/ui/src/Components/Dashboard/Table/TableHeader/TableHeader.js index 36a2c29..6f509a2 100644 --- a/ui/src/Components/Dashboard/Table/TableHeader/TableHeader.js +++ b/ui/src/Components/Dashboard/Table/TableHeader/TableHeader.js @@ -1,27 +1,48 @@ -import PreviousMap from "postcss/lib/previous-map"; +import { useState } from "react"; + import { camelCaseToNormal } from "../../../../Utils/helpers"; import SortCursor from "./SortCursor"; import { TABLE } from "../../../../Constant/constant"; export default function TableHeader({ headers, handleSorting }) { + const [currentClickedCursor, setCurrentClickedCursor] = useState(null); + const [currentSortedColumn, setCurrentSortedColumn] = useState(null); + + const onCursorClick = (headerName, isSortConfigured) => { + if (!isSortConfigured) return; + let clickedCursorType = + currentClickedCursor == TABLE.ASCENDING + ? TABLE.DESCENDING + : TABLE.ASCENDING; + setCurrentClickedCursor(clickedCursorType); + setCurrentSortedColumn(headerName); + handleSorting(clickedCursorType, headerName); + }; return ( {headers.map((heading, index) => ( - - - - {camelCaseToNormal(heading.headerName)} - - - {heading.sortable ? ( - - ) : null} - - + { + onCursorClick(heading.headerName, heading.sortable); + }} + > +
+ {camelCaseToNormal(heading.headerName)} + + {heading.sortable ? ( + + ) : null} +
))} diff --git a/ui/src/Utils/helpers.js b/ui/src/Utils/helpers.js index de68c97..09e50fc 100644 --- a/ui/src/Utils/helpers.js +++ b/ui/src/Utils/helpers.js @@ -1,15 +1,17 @@ -import {TABLE} from "../Constant/constant" +import { TABLE } from "../Constant/constant"; export function camelCaseToNormal(string) { - return string - // insert a space before all caps - .replace(/([A-Z])/g, ' $1') - // uppercase the first character - .replace(/^./, (str) => str.toUpperCase()) + return ( + string + // insert a space before all caps + .replace(/([A-Z])/g, " $1") + // uppercase the first character + .replace(/^./, (str) => str.toUpperCase()) + ); } /**** - * + * * Currying * https://javascript.info/currying-partials */ @@ -19,10 +21,8 @@ export function compareValues(key, order = TABLE.ASCENDING) { return 0; } - const varA = (typeof a[key] === 'string') - ? a[key].toUpperCase() : a[key]; - const varB = (typeof b[key] === 'string') - ? b[key].toUpperCase() : b[key]; + const varA = typeof a[key] === "string" ? a[key].toUpperCase() : a[key]; + const varB = typeof b[key] === "string" ? b[key].toUpperCase() : b[key]; let comparison = 0; if (varA > varB) { @@ -30,8 +30,6 @@ export function compareValues(key, order = TABLE.ASCENDING) { } else if (varA < varB) { comparison = -1; } - return ( - (order === TABLE.DESCENDING) ? (comparison * -1) : comparison - ); + return order === TABLE.DESCENDING ? comparison * -1 : comparison; }; -} \ No newline at end of file +} diff --git a/ui/src/index.css b/ui/src/index.css index dd99768..7e4ed3f 100644 --- a/ui/src/index.css +++ b/ui/src/index.css @@ -4,15 +4,15 @@ body { margin: 0; - font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', - 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', + font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", + "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } code { - font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', + font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", monospace; } @@ -21,26 +21,20 @@ code { background: transparent; border-bottom: solid 7px black; border-top-width: 0; - cursor: pointer; } -.clicked-u{ +.clicked-u { border-bottom: solid 7px #ad9f9f; } -.down-arrow{ +.down-arrow { border: solid 6px transparent; background: transparent; border-top: solid 7px black; border-bottom-width: 0; - margin-top:1px; - cursor: pointer; + margin-top: 1px; } -.clicked-d{ +.clicked-d { border-top: solid 7px #ad9f9f; } - - - - diff --git a/ui/src/test/SortCursor.test.js b/ui/src/test/SortCursor.test.js index 1ce8f78..03048b1 100644 --- a/ui/src/test/SortCursor.test.js +++ b/ui/src/test/SortCursor.test.js @@ -1,45 +1,19 @@ -import { fireEvent, render, screen } from "@testing-library/react"; +import {render, screen } from "@testing-library/react"; import SortCursor from "../Components/Dashboard/Table/TableHeader/SortCursor"; import { TABLE } from "../Constant/constant"; describe("SortCursor Component", () => { + it("Should render the SortCursor Component, which includes Ascending cursor", () => { + render(); - it("Should render the SortCursor Component with Ascending cursor", () => { - render() + const cursor = screen.getByTitle(TABLE.ASCENDING); + expect(cursor).toBeInTheDocument(); + }); - const cursor = screen.getByTitle(TABLE.ASCENDING) - expect(cursor).toBeInTheDocument(); - }) - - it("Should render the SortCursor Component with Descending cursor", () => { - render() - - const cursor = screen.getByTitle(TABLE.DESCENDING) - expect(cursor).toBeInTheDocument(); - - }) - - it("Should fire event on click ascending", async () => { - //const mockFn = sinon.spy(); - - const mockFn = jest.fn(); - render() - - const cursor = screen.getByTitle(TABLE.ASCENDING) - await fireEvent.click(cursor); - - - }) - - it("Should fire event on click descending", async () => { - const mockFn = jest.fn(); - render() - - const cursor = screen.getByTitle(TABLE.DESCENDING) - await fireEvent.click(cursor); - expect(mockFn.mock.calls.length).toEqual(1); - - }) - - }) + it("Should render the SortCursor Component, which includes Descending cursor", () => { + render(); + const cursor = screen.getByTitle(TABLE.DESCENDING); + expect(cursor).toBeInTheDocument(); + }); +}); diff --git a/ui/src/test/Table.test.js b/ui/src/test/Table.test.js new file mode 100644 index 0000000..e69de29