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

links group refactored to select multiple categories #172

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 37 additions & 3 deletions src/app/catalog/_components/Category.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,48 @@
"use client";

import { getCategoryName } from "@/lib/categories-map";
import { GENDER_MAP } from "@/constants";

import { getCategoryName, groupCategories } from "@/lib/categories-map";
import { useDataContext } from "@/components/DataContext";
import { Text } from "@/components/ui/text";

import useFilterQueryParams from "./useFilterQueryParams";

export default function Category() {
const { defaultValue: defaultCategory } = useFilterQueryParams("category");
const { defaultValue: selectedCategories } = useFilterQueryParams("category");
const { defaultValue: gender } = useFilterQueryParams("gender");
const { dictionary } = useDataContext();
const selectedCategoryIds = selectedCategories?.split(",") || [];
const categoriesGroups = groupCategories(
dictionary?.categories?.map((v) => v.name as string) || [],
);

const getGenderKey = (value: string) =>
Object.entries(GENDER_MAP).find(([_, val]) => val === value)?.[0] || value;

const getGroupTitle = () => {
return (
Object.entries(categoriesGroups).find(([_, group]) => {
const groupCategoryIds = group.items.map((item) => item.id);
return (
groupCategoryIds.length === selectedCategoryIds.length &&
groupCategoryIds.every((id) => selectedCategoryIds.includes(id))
);
})?.[1]?.title || null
);
};

if (getGroupTitle() && gender) {
return (
<Text variant="uppercase">{`${getGenderKey(gender)}'s ${getGroupTitle()}`}</Text>
);
}

return (
<Text variant="uppercase">{`${getCategoryName(defaultCategory) || "all categories"}`}</Text>
<Text variant="uppercase">
{selectedCategories
? selectedCategoryIds.map((id) => getCategoryName(id)).join(", ")
: "all categories"}
</Text>
);
}
2 changes: 1 addition & 1 deletion src/app/catalog/_components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function getProductsPagedQueryParams({
sortFactors: sort ? [sort as common_SortFactor] : undefined,
orderFactor: order ? (order as common_OrderFactor) : undefined,
filterConditions: {
categoryIds: category ? [parseInt(category)] : undefined,
categoryIds: category ? category.split(",").map(id => parseInt(id)) : undefined,
// genderIds: gender ? [parseInt(gender)] : undefined,
sizesIds: size ? [parseInt(size)] : undefined,
from: undefined,
Expand Down
33 changes: 28 additions & 5 deletions src/components/ui/desktop-nav-menu.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Link from "next/link";
import { useSearchParams } from "next/navigation";
import { GENDER_MAP } from "@/constants";
import * as NavigationMenu from "@radix-ui/react-navigation-menu";

import { groupCategories } from "@/lib/categories-map";
import { cn } from "@/lib/utils";
import { useDataContext } from "@/components/DataContext";
import useFilterQueryParams from "@/app/catalog/_components/useFilterQueryParams";

import { Button } from "./button";
import { Text } from "./text";
Expand Down Expand Up @@ -37,6 +37,7 @@ export function DesktopNavigationMenu({ className }: { className?: string }) {
className="w-40"
key={key}
title={category.title}
gender={GENDER_MAP["men"]}
links={category.items.map((item) => ({
title: item.label.toLowerCase(),
href: `${item.href}&${men}`,
Expand Down Expand Up @@ -64,6 +65,7 @@ export function DesktopNavigationMenu({ className }: { className?: string }) {
className="w-40"
key={key}
title={category.title}
gender={GENDER_MAP["women"]}
links={category.items.map((item) => ({
title: item.label.toLowerCase(),
href: `${item.href}&${women}`,
Expand Down Expand Up @@ -96,17 +98,36 @@ function LinksGroup({
className,
title,
links,
gender,
}: {
groupIndex: number;
className?: string;
title: string;
gender: string;
links: {
title: string;
href: string;
id: string;
}[];
}) {
const category = useSearchParams().get("category");
const { defaultValue: category } = useFilterQueryParams("category");
const { defaultValue: currentGender } = useFilterQueryParams("gender");
const selectedCategories = category?.split(",") || [];
const genderCategories = currentGender === gender ? selectedCategories : [];
const isSelectedCategory = (id: string) => genderCategories.includes(id);

const getViewAllHref = () => {
const groupCategoryIds = links.map((link) => link.id);
return `/catalog?category=${encodeURIComponent(groupCategoryIds.join(","))}&gender=${gender}`;
};

const getNewHref = (id: string) => {
const newCategories = isSelectedCategory(id)
? genderCategories.filter((v) => v !== id)
: [...genderCategories, id];

return `/catalog?category=${encodeURIComponent(newCategories.join(","))}&gender=${gender}`;
};

return (
<div className={cn("space-y-4", className)}>
Expand All @@ -116,16 +137,18 @@ function LinksGroup({
<div className="space-y-2">
<div className="w-full">
<Button variant="simpleReverse" asChild>
<NavigationMenu.Link href="/catalog">view all</NavigationMenu.Link>
<NavigationMenu.Link href={getViewAllHref()}>
view all
</NavigationMenu.Link>
</Button>
</div>
{links.map((link) => (
<div className="w-full" key={link.href}>
<Button
variant={category === link.id ? "simple" : "simpleReverse"}
variant={isSelectedCategory(link.id) ? "simple" : "simpleReverse"}
asChild
>
<NavigationMenu.Link href={link.href}>
<NavigationMenu.Link href={getNewHref(link.id)}>
{link.title}
</NavigationMenu.Link>
</Button>
Expand Down
35 changes: 29 additions & 6 deletions src/components/ui/mobile-menu-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as DialogPrimitives from "@radix-ui/react-dialog";
import { groupCategories } from "@/lib/categories-map";
import CurrencyPopover from "@/app/_components/currency-popover";
import NewslatterForm from "@/app/_components/newslatter-form";
import useFilterQueryParams from "@/app/catalog/_components/useFilterQueryParams";

import { useDataContext } from "../DataContext";
import { Button } from "./button";
Expand Down Expand Up @@ -66,11 +67,30 @@ function Menu({
setActiveCategory: (category: "men" | "women" | undefined) => void;
}) {
const { dictionary } = useDataContext();

const { defaultValue: category } = useFilterQueryParams("category");
const { defaultValue: currentGender } = useFilterQueryParams("gender");
const categoriesGroups = groupCategories(
dictionary?.categories?.map((v) => v.name as string) || [],
);

const selectedCategories = category?.split(",") || [];
const genderCategories =
currentGender === GENDER_MAP[activeCategory!] ? selectedCategories : [];
const isSelectedCategory = (id: string) => genderCategories.includes(id);

const getViewAllHref = (items: { id: string }[]) => {
const groupCategoryIds = items.map((item) => item.id);
return `/catalog?category=${encodeURIComponent(groupCategoryIds.join(","))}&gender=${GENDER_MAP[activeCategory!]}`;
};

const getNewHref = (id: string) => {
const newCategories = isSelectedCategory(id)
? genderCategories.filter((v) => v !== id)
: [...genderCategories, id];

return `/catalog?category=${encodeURIComponent(newCategories.join(","))}&gender=${GENDER_MAP[activeCategory!]}`;
};

return (
<div className="flex h-full grow flex-col justify-between overflow-y-auto bg-bgColor p-2">
{activeCategory === undefined ? (
Expand Down Expand Up @@ -122,15 +142,18 @@ function Menu({
<div className="space-y-4">
<DialogPrimitives.Close asChild>
<Button variant="simpleReverse" asChild>
<Link href="/catalog">view all</Link>
<Link href={getViewAllHref(category.items)}>view all</Link>
</Button>
</DialogPrimitives.Close>
{category.items.map((item) => (
<DialogPrimitives.Close key={item.id} asChild>
<Button variant="simpleReverse" asChild>
<Link
href={`${item.href}&gender=${GENDER_MAP[activeCategory]}`}
>
<Button
variant={
isSelectedCategory(item.id) ? "simple" : "simpleReverse"
}
asChild
>
<Link href={getNewHref(item.id)}>
{item.label.toLowerCase()}
</Link>
</Button>
Expand Down