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

Visual bugfixes, refactor of code, hooks #227

Merged
merged 16 commits into from
Oct 31, 2023
Merged
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
4 changes: 2 additions & 2 deletions frontend/src/components/ActiveFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Filter } from "react-feather";
import { useFilteredConsultants } from "@/hooks/useFilteredConsultants";

export default function ActiveFilters() {
const { filteredDepartments, currentSearch, filteredYears } =
const { filteredDepartments, searchFilter, filteredYears } =
useFilteredConsultants();
const filterTextComponents: string[] = [];

if (currentSearch != "") filterTextComponents.push(` "${currentSearch}"`);
if (searchFilter != "") filterTextComponents.push(` "${searchFilter}"`);

if (filteredDepartments.length > 0)
filterTextComponents.push(
Expand Down
13 changes: 10 additions & 3 deletions frontend/src/components/NavBar/NavBarDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
"use client";
import React, { useState } from "react";
import React, { useRef, useState } from "react";
import { signOut } from "next-auth/react";
import { LogOut, User } from "react-feather";
import { LogOut } from "react-feather";
import { useOutsideClick } from "@/hooks/useOutsideClick";

export default function NavBarDropdown(props: { initials: string }) {
const [isOpen, setIsOpen] = useState<boolean>(false);

const menuRef = useRef(null);

useOutsideClick(menuRef, () => {
if (isOpen) setIsOpen(false);
});

return (
<>
<div className="relative">
<div className="relative" ref={menuRef}>
<button
className={`flex rounded-full border border-white h-9 min-w-[36px] justify-center items-center ${
props.initials.length > 3 && "px-1"
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/StaffingSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function StaffingSidebar() {
const [isSidebarHidden, setIsSidebarHidden] = useState(true);

return (
<div className="min-h-screen max-[720px]:fixed">
<div className="min-h-screen max-[720px]:fixed z-10">
<div
className={`bg-primary_l4 py-6 px-4 h-full min-h-screen w-[300px] ${
isSidebarHidden
Expand Down
154 changes: 79 additions & 75 deletions frontend/src/hooks/useFilteredConsultants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,48 @@ import { useCallback, useContext, useEffect, useState } from "react";
import { FilteredContext } from "@/components/FilteredConsultantProvider";
import { yearRanges } from "@/components/ExperienceFilter";

interface UpdateFilterParams {
search?: string;
departments?: string;
years?: string;
}

export function useFilteredConsultants() {
const { departments, consultants } = useContext(FilteredContext);
const router = useRouter();
const pathname = usePathname();
const searchParams = useSearchParams();
const currentSearch = searchParams.get("search") || "";

const searchFilter = searchParams.get("search") || "";
const departmentFilter = searchParams.get("depFilter") || "";
const yearFilter = searchParams.get("yearFilter") || "";

const [activeNameSearch, setActiveNameSearch] =
useState<string>(currentSearch);
useState<string>(searchFilter);
const [lastSearchKeyStrokeTime, setLastSearchKeyStrokeTime] =
useState<number>();

const updateRoute = useCallback(
Copy link
Collaborator

Choose a reason for hiding this comment

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

💯
Denne trengte vi! Veldig bra

(updateParams: UpdateFilterParams) => {
// If not defined, defaults to current value:
const { search = searchFilter } = updateParams;
const { departments = departmentFilter } = updateParams;
const { years = yearFilter } = updateParams;

router.push(
`${pathname}?search=${search}&depFilter=${departments}&yearFilter=${years}`,
);
},
[departmentFilter, pathname, router, searchFilter, yearFilter],
);

useEffect(() => {
let nameSearchDebounceTimer = setTimeout(() => {
if (
lastSearchKeyStrokeTime &&
Date.now() - lastSearchKeyStrokeTime > 250
) {
const currentDepartmentFilter = searchParams.get("depFilter") || "";
const currentYearFilter = searchParams.get("yearFilter") || "";

router.push(
`${pathname}?search=${activeNameSearch}&depFilter=${currentDepartmentFilter}&yearFilter=${currentYearFilter}`,
);
updateRoute({ search: activeNameSearch });
}
}, 250);

Expand All @@ -39,30 +57,21 @@ export function useFilteredConsultants() {
return () => {
clearTimeout(nameSearchDebounceTimer);
};
}, [
lastSearchKeyStrokeTime,
activeNameSearch,
searchParams,
router,
pathname,
]);

const currentDepartmentFilter = searchParams.get("depFilter") || "";
const currentYearFilter = searchParams.get("yearFilter") || "";

const filteredDepartments = currentDepartmentFilter
}, [activeNameSearch, lastSearchKeyStrokeTime, updateRoute]);

const filteredDepartments = departmentFilter
.split(",")
.map((id) => departments.find((d) => d.id === id))
.filter((dept) => dept !== undefined) as Department[];

const filteredYears = currentYearFilter
const filteredYears = yearFilter
.split(",")
.map((urlString) => yearRanges.find((y) => y.urlString === urlString))
.filter((year) => year !== undefined) as YearRange[];

const filteredConsultants = filterConsultants(
currentSearch,
currentDepartmentFilter,
searchFilter,
departmentFilter,
filteredYears,
consultants,
);
Expand All @@ -74,61 +83,20 @@ export function useFilteredConsultants() {

const toggleDepartmentFilter = useCallback(
(d: Department) => {
const currentSearch = searchParams.get("search") || "";
const currentFilter = searchParams.get("depFilter") || "";
const currentYearFilter = searchParams.get("yearFilter") || "";
const filters = currentFilter.split(",");
const filterIndex = filters.indexOf(d.id);
const newFilters = [...filters];
if (filterIndex === -1) {
newFilters.push(d.id);
} else {
newFilters.splice(filterIndex, 1);
}
const newFilterString = newFilters.join(",").replace(/^,/, "");
router.push(
`${pathname}?search=${currentSearch}&depFilter=${newFilterString}&yearFilter=${currentYearFilter}`,
);
const newDepartmentFilter = toggleValueFromFilter(departmentFilter, d.id);
updateRoute({ departments: newDepartmentFilter });
},
[searchParams, router, pathname],
[departmentFilter, updateRoute],
);

const clearDepartmentFilter = useCallback(() => {
const currentSearch = searchParams.get("search") || "";
const currentYearFilter = searchParams.get("yearFilter") || "";

router.push(
`${pathname}?search=${currentSearch}&depFilter=&yearFilter=${currentYearFilter}`,
);
}, [pathname, router, searchParams]);

const toggleYearFilter = useCallback(
(y: YearRange) => {
const currentSearch = searchParams.get("search") || "";
const currentDepartmentFilter = searchParams.get("depFilter") || "";
const currentYearFilter = searchParams.get("yearFilter") || "";

const filters = currentYearFilter.split(",");
const filterIndex = filters.indexOf(y.urlString);
const newFilters = [...filters];
if (filterIndex === -1) {
newFilters.push(y.urlString);
} else {
newFilters.splice(filterIndex, 1);
}
const newFilterString = newFilters.join(",").replace(/^,/, "");

router.push(
`${pathname}?search=${currentSearch}&depFilter=${currentDepartmentFilter}&yearFilter=${newFilterString}`,
);
const newYearFilter = toggleValueFromFilter(yearFilter, y.urlString);
updateRoute({ years: newYearFilter });
},
[pathname, router, searchParams],
[updateRoute, yearFilter],
);

const clearAll = useCallback(() => {
router.push(`${pathname}?search=&depFilter=&yearFilter=`);
}, [pathname, router]);

useEffect(() => {
function handleDepartmentHotkey(keyCode: string) {
departments
Expand All @@ -146,7 +114,7 @@ export function useFilteredConsultants() {
handleDepartmentHotkey(e.code);
}
if (e.code.includes("0")) {
clearDepartmentFilter();
updateRoute({ departments: "" });
}
}
document.addEventListener("keydown", keyDownHandler);
Expand All @@ -155,7 +123,7 @@ export function useFilteredConsultants() {
return () => {
document.removeEventListener("keydown", keyDownHandler);
};
}, [clearAll, clearDepartmentFilter, departments, toggleDepartmentFilter]);
}, [updateRoute, departments, toggleDepartmentFilter]);

return {
consultants,
Expand All @@ -164,15 +132,51 @@ export function useFilteredConsultants() {
filteredDepartments,
filteredYears,
currentNameSearch: activeNameSearch,
currentSearch,
searchFilter,
setNameSearch,
toggleDepartmentFilter,
toggleYearFilter,
clearDepartmentFilter,
clearAll,
};
}

class UrlStringFilter {
stringFilter: string;
arrayFilter: string[];
index: number;
valueAlreadyExist: boolean;
value: string;

constructor(stringFilter: string, value: string) {
this.stringFilter = stringFilter;
this.arrayFilter = this.stringFilter.split(",");
this.index = this.arrayFilter.indexOf(value);
this.valueAlreadyExist = this.index !== -1;
this.value = value;
}

removeFromFilter() {
this.arrayFilter.splice(this.index, 1);
return this.asJoinToString();
}

addToFilter() {
this.arrayFilter.push(this.value);
return this.asJoinToString();
}

asJoinToString() {
return this.arrayFilter.join(",").replace(/^,/, "");
}
}

function toggleValueFromFilter(stringFilters: string, value: string) {
const filter = new UrlStringFilter(stringFilters, value);

return filter.valueAlreadyExist
? filter.removeFromFilter()
: filter.addToFilter();
}

function filterConsultants(
search: string,
departmentFilter: string,
Expand Down
21 changes: 21 additions & 0 deletions frontend/src/hooks/useOutsideClick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use client";
import { useEffect, RefObject } from "react";

export function useOutsideClick(
ref: RefObject<HTMLElement>,
callback: () => void,
) {
useEffect(() => {
function handleClick(e: MouseEvent) {
if (ref.current && !ref.current.contains(e.target as Node)) {
callback();
}
}

document.addEventListener("click", handleClick);

return () => {
document.removeEventListener("click", handleClick);
};
}, [ref, callback]);
}
15 changes: 15 additions & 0 deletions frontend/src/screens.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const windowWidth = {
xs: 640,
sm: 768,
md: 1024,
lg: 1280,
xl: 1400,
};

export const screens = {
xs: `${windowWidth.xs}px`,
sm: `${windowWidth.sm}px`,
md: `${windowWidth.md}px`,
lg: `${windowWidth.lg}px`,
xl: `${windowWidth.xl}px`,
};
16 changes: 6 additions & 10 deletions frontend/tailwind.config.js → frontend/tailwind.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
import type { Config } from "tailwindcss";
import { screens } from "./src/screens.config";

export default {
content: ["./src/**/*.{js,jsx,ts,tsx}"],
theme: {
colors: {
Expand All @@ -15,16 +17,10 @@ module.exports = {
transparent: "transparent",
},
extend: {},
screens: {
xs: "640px",
sm: "768px",
md: "1024px",
lg: "1280px",
xl: "1400px",
},
screens,
boxShadow: {
md: "0 4px 4px 0 rgba(66, 61, 137, 0.10)",
},
},
plugins: [],
};
} satisfies Config;
Loading