-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add api call for departments * Add new font * Avoid search overwriting filter * Add transparent color to tailwind * Add department filter to list of consultants * Use ReactQuery to fetch departments * Use db data, remove comma at beginning of string * fix useDepartmentsApi and add mockDepartments --------- Co-authored-by: Mathilde Haukø Haugum <[email protected]>
- Loading branch information
1 parent
def83ef
commit fa49f79
Showing
14 changed files
with
199 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Database.DatabaseContext; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
[Route("/departments")] | ||
[ApiController] | ||
public class DepartmentController : ControllerBase { | ||
|
||
[HttpGet] | ||
public ActionResult<List<DepartmentReadModel>> Get(ApplicationContext applicationContext){ | ||
|
||
return applicationContext.Department.Select(d => new DepartmentReadModel(d.Id, d.Name)).ToList(); | ||
|
||
} | ||
} | ||
|
||
public record DepartmentReadModel(string Id, string Name); |
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,8 @@ | ||
import { Department, Variant } from "@/types"; | ||
|
||
export const MockDepartments: Department[] = [ | ||
{ | ||
id: "myDepartment", | ||
name: "My Department", | ||
}, | ||
]; |
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
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,27 @@ | ||
"use client"; | ||
import FilterButton from "./FilterButton"; | ||
import useDepartmentsApi from "@/hooks/useDepartmentsApi"; | ||
import { CircularProgress } from "@mui/material"; | ||
|
||
export default function DepartmentFilter() { | ||
const { data, isLoading } = useDepartmentsApi(); | ||
|
||
if (isLoading) { | ||
<CircularProgress />; | ||
} | ||
|
||
if (data) { | ||
return ( | ||
<div> | ||
<div className="flex flex-col gap-2"> | ||
<p className="body-small">Avdelinger</p> | ||
<div className="flex flew-row flex-wrap gap-2"> | ||
{data?.map((department, index) => ( | ||
<FilterButton key={index} filterName={department.name} /> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} |
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,44 @@ | ||
"use client"; | ||
import { useRouter, useSearchParams } from "next/navigation"; | ||
import { useState } from "react"; | ||
|
||
export default function FilterButton({ filterName }: { filterName: string }) { | ||
const router = useRouter(); | ||
const searchParams = useSearchParams(); | ||
const [isButtonActive, setIsButtonActive] = useState(checkFilterInUrl); | ||
|
||
function handleFilterClick() { | ||
setIsButtonActive((prevState) => !prevState); | ||
|
||
const currentSearch = searchParams.get("search"); | ||
const currentFilter = searchParams.get("filter") || ""; | ||
const filters = currentFilter.split(","); | ||
const filterIndex = filters.indexOf(filterName); | ||
const newFilters = [...filters]; | ||
if (filterIndex === -1) { | ||
newFilters.push(filterName); | ||
} else { | ||
newFilters.splice(filterIndex, 1); | ||
} | ||
const newFilterString = newFilters.join(",").replace(/^,/, ""); | ||
router.push(`/bemanning?search=${currentSearch}&filter=${newFilterString}`); | ||
} | ||
|
||
function checkFilterInUrl() { | ||
const currentFilter = searchParams.get("filter") || ""; | ||
return currentFilter.includes(filterName); | ||
} | ||
|
||
return ( | ||
<button | ||
onClick={() => handleFilterClick()} | ||
className={`px-3 py-2 border-2 rounded-full ${ | ||
isButtonActive | ||
? "bg-primary_default text-white border-transparent " | ||
: "border-primary_default border-opacity-50 text-primary_default hover:bg-primary_default hover:bg-opacity-10" | ||
}`} | ||
> | ||
<p className="interaction-chip">{filterName}</p> | ||
</button> | ||
); | ||
} |
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,32 @@ | ||
"use client"; | ||
|
||
import { useIsAuthenticated } from "@azure/msal-react"; | ||
import { useQuery } from "react-query"; | ||
import { Department } from "@/types"; | ||
import { fetchWithToken } from "@/auth/fetchWithToken"; | ||
|
||
function useDepartmentsApi() { | ||
const isAuthenticated = | ||
useIsAuthenticated() || process.env.NEXT_PUBLIC_NO_AUTH; | ||
|
||
return useQuery({ | ||
queryKey: "departments", | ||
queryFn: async () => { | ||
if (isAuthenticated) { | ||
try { | ||
const response: Department[] = | ||
await fetchWithToken(`/api/departments`); | ||
return response; | ||
} catch (err) { | ||
console.error(err); | ||
return []; | ||
} | ||
} | ||
// If not authenticated, return an empty array | ||
return []; | ||
}, | ||
refetchOnWindowFocus: false, | ||
}); | ||
} | ||
|
||
export default useDepartmentsApi; |
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