Skip to content

Commit

Permalink
fix: adds style to show more button
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelbr committed Nov 29, 2024
1 parent 05cb84d commit fdf5f0d
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 30 deletions.
3 changes: 2 additions & 1 deletion messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"Design": "Design",
"Utvikling": "Development",
"Administasjon": "Administration",
"field": "Field"
"field": "Field",
"showMore": "Show all"
}
}
3 changes: 2 additions & 1 deletion messages/no.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"Design": "Design",
"Utvikling": "Utvikling",
"Administasjon": "Administrasjon",
"field": "Fag"
"field": "Fag",
"showMore": "Vis alle"
}
}
3 changes: 2 additions & 1 deletion messages/se.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"Design": "Design",
"Utvikling": "Utveckling",
"Administasjon": "Administration",
"field": "Fält"
"field": "Fält",
"showMore": "Vis alla"
}
}
6 changes: 6 additions & 0 deletions public/_assets/arrow-down.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
101 changes: 74 additions & 27 deletions src/components/sections/employees/EmployeeList.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"use client";

import Link from "next/link";
import Image from "next/image";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { useTranslations } from "next-intl";
import { use, useState, useTransition } from "react";

import EmployeeCard, {
EmployeeCardSkeleton,
} from "src/components/employeeCard/EmployeeCard";
import EmployeeCard from "src/components/employeeCard/EmployeeCard";
import { Tag } from "src/components/tag";
import Text from "src/components/text/Text";
import { ChewbaccaEmployee, Competence } from "src/types/employees";
Expand Down Expand Up @@ -41,22 +40,18 @@ export default function EmployeeList({
language,
employeesPageSlug,
}: EmployeesProps) {
let [isPending, startTransition] = useTransition();

const currentPath = usePathname();
const searchParams = useSearchParams();
const { replace } = useRouter();
const limitEmployees = !searchParams.has("showAll");
console.log(searchParams, searchParams.has("showAll"));

const employeesRes = use(employeesPromise);
const employees = employeesRes.ok ? employeesRes.value : [];
const [filteredEmployees, setFilteredEmployees] =
useState<ChewbaccaEmployee[]>(employees);

const limitedEmployees = limitEmployees
? filteredEmployees.slice(0, DEFAULT_LIMIT)
: filteredEmployees;
const {
isShowMore,
isShowMorePending,
limitedEmployees,
showMoreHandler,
showMoreHref,
} = useShowAll(filteredEmployees);

const locations = Array.from(new Set(employees.map((e) => e.officeName)));
const t = useTranslations("employee_card");
Expand Down Expand Up @@ -175,24 +170,76 @@ export default function EmployeeList({
/>
))}
</div>
{isPending && <EmployeeListSkeleton />}
<Link
onClick={() =>
startTransition(() => {
replace(`${currentPath}?showAll`);
})
}
href={`${currentPath}?showAll`}
shallow
scroll={false}
>
Show more
</Link>
{isShowMorePending && <EmployeeListSkeleton />}

{(!isShowMore || isShowMorePending) && (
<ShowMoreButton
showMoreHandler={showMoreHandler}
showMoreHref={showMoreHref}
/>
)}
</div>
</>
);
}

function ShowMoreButton({
showMoreHandler,
showMoreHref,
}: {
showMoreHandler: () => void;
showMoreHref: string;
}) {
const t = useTranslations("employee_card");

// @TODO Replace with Button component when actually implemented
return (
<div className={styles.showMore}>
<Link
className={styles.showMore__button}
onClick={showMoreHandler}
href={showMoreHref}
shallow
scroll={false}
>
{t("showMore")}{" "}
<Image
src="/_assets/arrow-down.svg"
alt=""
role="none"
width={24}
height={24}
/>
</Link>
</div>
);
}

function useShowAll(filteredEmployees: ChewbaccaEmployee[]) {
const [isPending, startTransition] = useTransition();

const currentPath = usePathname();
const searchParams = useSearchParams();
const { replace } = useRouter();
const limitEmployees = !searchParams.has("showAll");
const limitedEmployees = limitEmployees
? filteredEmployees.slice(0, DEFAULT_LIMIT)
: filteredEmployees;

const showMoreHandler = () =>
startTransition(() => {
replace(`${currentPath}?showAll`);
});

return {
limitedEmployees,
showMoreHandler,
isShowMorePending: isPending,
isShowMore: !limitEmployees,
showMoreHref: `${currentPath}?showAll`,
};
}

function sortAlphabetically(filter: (string | null | undefined)[]) {
return filter.sort((a, b) => a?.localeCompare(b ?? "") ?? 0);
}
Expand Down
1 change: 1 addition & 0 deletions src/components/sections/employees/EmployeeSkeleton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EmployeeCardSkeleton } from "src/components/employeeCard/EmployeeCard";

import styles from "./employees.module.css";

export function EmployeeListSkeleton() {
Expand Down
42 changes: 42 additions & 0 deletions src/components/sections/employees/employees.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,45 @@
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 1.5rem;
}

.showMore {
display: flex;
align-items: center;
justify-content: center;
position: relative;
margin-top: 2rem;
}

.showMore::before {
content: "";
display: block;
height: 1px;
background: var(--background-button-outline-dark);
width: 100%;
position: absolute;
top: 50%;
left: 0;
z-index: 1;
}

.showMore__button {
line-height: 1;
display: flex;
align-items: center;
justify-content: center;

padding: 0.25rem 0.5rem;
gap: 0.25rem;

border-radius: var(--radius-small);
border: 1px solid var(--background-button-outline-dark);
background: var(--background-bg-light-primary);
text-decoration: none;
z-index: 2;
position: relative;
}
.showMore__button img {
display: block;
height: 1.5rem;
width: 1.5rem;
}

0 comments on commit fdf5f0d

Please sign in to comment.