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

[DEV-37] Entities filter and sort #13

Merged
merged 2 commits into from
Jul 18, 2024
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
73 changes: 67 additions & 6 deletions apps/app/src/app/[locale]/(app)/entities/_components/filter.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
"use client";

import { usePathname } from "@/lib/navigation";
import Field from "@repo/ds/form/field";
import Form from "@repo/ds/form/form";
import { Button } from "@repo/ds/ui/button";
import { Calendar } from "@repo/ds/ui/calendar";
import { Input } from "@repo/ds/ui/input";
import { Popover, PopoverContent, PopoverTrigger } from "@repo/ds/ui/popover";
import { parseToDate, stringifyDate } from "@repo/ds/utils/date";
import { CalendarIcon, Trash } from "lucide-react";
import { useTranslations } from "next-intl";
import { useRouter } from "next/navigation";
import { useRef } from "react";
Expand All @@ -14,29 +18,86 @@ interface Props {

export function Filter(props: Props) {
const router = useRouter();
const pathname = usePathname();
const timer = useRef<NodeJS.Timeout>();
const t = useTranslations("entities");

return (
<Form
data={props.data}
onChange={(_, data) => {
className="bg-white *:m-0 p-2 rounded-lg shadow flex gap-4 flex-col"
data={{
...props.data,
date_disappeared: parseToDate(props.data?.date_disappeared as string),
}}
onSubmit={async (_, data) => {
if (timer.current) clearTimeout(timer.current);

timer.current = setTimeout(() => {
const values = new URLSearchParams(data as Record<string, string>);
router.push(`${pathname}?${values.toString()}`);
const url = new URL(window.location.href);
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
const payload = data as Record<string, any>;

url.searchParams.set("q", payload.q || "");
url.searchParams.set(
"date_disappeared",
payload.date_disappeared
? stringifyDate(payload.date_disappeared)
: "",
);

router.push(`${url.pathname}?${url.searchParams.toString()}`);
}, 150);
}}
>
<Field
name="q"
label={t("search")}
errorless
render={({ field }) => {
return <Input {...field} />;
}}
/>

<Field
name="date_disappeared"
label={t("date_disappeared")}
errorless
render={({ field }) => (
<Popover>
<PopoverTrigger asChild>
<Button
className="w-full justify-start px-2 gap-2"
variant="outline"
>
<CalendarIcon />

{(() => {
if (!field.value?.to)
return field.value?.from?.toLocaleString().split(",")[0];

return `${field.value?.from?.toLocaleString().split(",")[0]}-${field.value?.to?.toLocaleString().split(",")[0]}`;
})()}
</Button>
</PopoverTrigger>
<PopoverContent>
<Calendar
mode="range"
selected={field.value}
onSelect={field.onChange}
{...field}
/>
</PopoverContent>
</Popover>
)}
/>

<div className="flex gap-2">
<Button type="submit" className="w-full">
Aplicar
</Button>
<Button type="button" variant="destructive">
<Trash />
</Button>
</div>
</Form>
);
}
31 changes: 29 additions & 2 deletions apps/app/src/app/[locale]/(app)/entities/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Sort } from "@/components/sort";
import { Link } from "@/lib/navigation";
import { buttonVariants } from "@repo/ds/ui/button";
import Card from "@repo/ds/ui/card";
import { Pagination } from "@repo/ds/ui/pagination";
import type { Pagination as PaginationType } from "@repo/schemas/pagination";
import entityService from "@repo/services/entity";
import { UserRoundX } from "lucide-react";
import { getTranslations } from "next-intl/server";
import parallel from "../../../../lib/parallel";
import { Filter } from "./_components/filter";
Expand All @@ -15,15 +19,38 @@ export default async function Page(props: { searchParams: PaginationType }) {
return (
<div className="flex flex-col sm:flex-row grow container my-4 gap-4">
<aside className="w-full sm:w-1/3">
<h5 className="w-full text-xl mb-4">{t("filter")}</h5>
<h5 className="w-full text-xl py-4 pl-4 font-bold">
{t("entities")} - {t("filter")}
</h5>

<Filter data={props.searchParams} />
</aside>
<main className="w-full flex flex-col justify-between">
<div className="flex flex-wrap justify-center sm:justify-between gap-4 my-4">
<Sort
className="flex gap-2 bg-white rounded-lg *:m-0 p-2 shadow"
data={props.searchParams}
order={{
name: t("new.general.name"),
date_created: t("date_created"),
date_disappeared: t("date_disappeared"),
}}
/>

<div className="flex grow flex-wrap justify-center sm:justify-between gap-4 my-4">
{data.map((entity) => {
return <Card key={entity.id} {...entity} />;
})}

{data.length === 0 && (
<div className="flex justify-center items-center gap-4 flex-col text-center my-auto w-full">
<UserRoundX size={64} />
<h2 className="text-2xl font-bold">{t("empty")}</h2>
<p>Gostaria de registrar um novo desaparecido?</p>
<Link className={buttonVariants()} href="/entities/new">
Registrar
</Link>
</div>
)}
</div>

<Pagination page={pagination.page || 1} pages={pagination.pages} />
Expand Down
83 changes: 83 additions & 0 deletions apps/app/src/components/sort.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"use client";

import Field from "@repo/ds/form/field";
import Form from "@repo/ds/form/form";
import { Button, buttonVariants } from "@repo/ds/ui/button";
import { Checkbox } from "@repo/ds/ui/checkbox";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@repo/ds/ui/select";
import { ArrowDownAz, ArrowUpAz } from "lucide-react";
import { useRouter } from "next/navigation";
import { useRef } from "react";

interface Props {
data?: Record<string, unknown>;
order?: Record<string, string>;

className?: string;
}

export function Sort(props: Props) {
const router = useRouter();
const timer = useRef<NodeJS.Timeout>();

return (
<Form
className={props.className}
data={{ order: Object.keys(props.order || {})[0], ...props.data }}
onChange={(_, data) => {
if (timer.current) clearTimeout(timer.current);

const url = new URL(window.location.href);

Object.entries(data).map(([key, value]) =>
url.searchParams.set(key, (value as string) || ""),
);
router.push(`${url.pathname}?${url.searchParams.toString()}`);
}}
>
{props.order && (
<Field
name="order"
errorless
render={({ field }) => (
<Select {...field}>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
{Object.entries(props.order as Record<string, string>).map(
([key, value]) => (
<SelectItem key={key} value={key}>
{value}
</SelectItem>
),
)}
</SelectContent>
</Select>
)}
/>
)}
<Field
name="sort"
errorless
render={({ field: { onChange, ...field } }) => (
<>
<Checkbox
{...field}
onClick={() => onChange(field.value === "asc" ? "desc" : "asc")}
className={buttonVariants({ className: "*:absolute w-10" })}
>
{field.value === "asc" ? <ArrowDownAz /> : <ArrowUpAz />}
</Checkbox>
</>
)}
/>
</Form>
);
}
3 changes: 1 addition & 2 deletions apps/app/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Ratelimit } from "@upstash/ratelimit";
import { ipAddress } from "@vercel/edge";
import { kv } from "@vercel/kv";
import createMiddleware from "next-intl/middleware";
import { getLocale } from "next-intl/server";
import { NextResponse } from "next/server";
import { generate } from "./app/manifest";
import { locales } from "./i18n";
Expand Down Expand Up @@ -52,7 +51,7 @@ const clerk = clerkMiddleware(
if (!preferredLanguage) return NextResponse.json(generate("en-US"));
const favoriteLanguages = preferredLanguage.split(",");
const locale = favoriteLanguages.find((t) =>
["en-US", "pt-BR"].includes(t.split(";")[0]),
locales.includes(t.split(";")[0] as (typeof locales)[number]),
);
return NextResponse.json(await generate(locale || "en-US"));
}
Expand Down
Binary file modified bun.lockb
Binary file not shown.
107 changes: 56 additions & 51 deletions packages/ds/package.json
Original file line number Diff line number Diff line change
@@ -1,53 +1,58 @@
{
"name": "@repo/ds",
"version": "1.0.0",
"private": true,
"sideEffects": ["**/*.css"],
"exports": {
"./*": "./src/components/*.tsx",
"./utils": "./src/lib/utils.ts",
"./tw": "./tailwind.config.ts",
"./hooks/*": "./src/hooks/*.ts",
"./react": "./src/lib/react.ts"
},
"license": "MIT",
"scripts": {
"lint": "biome lint --error-on-warnings ./src",
"verify": "biome check --error-on-warnings ./src",
"format": "biome format --error-on-warnings ./src"
},
"devDependencies": {
"@repo/config": "0.0.0",
"@types/react": "^18.2.61",
"autoprefixer": "^10.4.18",
"postcss": "^8.4.35",
"react": "^18.2.0",
"tailwindcss": "^3.4.4",
"tailwindcss-animate": "^1.0.7",
"typescript": "^5.5.2",
"vite": "^5.3.1",
"webpack": "^5.92.0"
},
"dependencies": {
"@hookform/resolvers": "^3.6.0",
"@radix-ui/react-avatar": "^1.1.0",
"@radix-ui/react-dialog": "^1.1.1",
"@radix-ui/react-dropdown-menu": "^2.1.1",
"@radix-ui/react-label": "^2.1.0",
"@radix-ui/react-select": "^2.1.1",
"@radix-ui/react-slot": "^1.1.0",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
"embla-carousel-react": "^8.1.6",
"lucide-react": "^0.394.0",
"next": "^14.2.4",
"next-intl": "^3.15.2",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-dropzone": "^14.2.3",
"react-hook-form": "^7.52.0",
"tailwind-merge": "^2.2.2",
"vaul": "^0.9.1",
"zod": "^3.22.4"
}
"name": "@repo/ds",
"version": "1.0.0",
"private": true,
"sideEffects": ["**/*.css"],
"exports": {
"./*": "./src/components/*.tsx",
"./utils": "./src/lib/utils.ts",
"./utils/*": "./src/lib/*.ts",
"./tw": "./tailwind.config.ts",
"./hooks/*": "./src/hooks/*.ts",
"./react": "./src/lib/react.ts"
},
"license": "MIT",
"scripts": {
"lint": "biome lint --error-on-warnings ./src",
"verify": "biome check --error-on-warnings ./src",
"format": "biome format --error-on-warnings ./src"
},
"devDependencies": {
"@repo/config": "0.0.0",
"@types/react": "^18.2.61",
"autoprefixer": "^10.4.18",
"postcss": "^8.4.35",
"react": "^18.2.0",
"tailwindcss": "^3.4.4",
"tailwindcss-animate": "^1.0.7",
"typescript": "^5.5.2",
"vite": "^5.3.1",
"webpack": "^5.92.0"
},
"dependencies": {
"@hookform/resolvers": "^3.6.0",
"@radix-ui/react-avatar": "^1.1.0",
"@radix-ui/react-checkbox": "^1.1.1",
"@radix-ui/react-dialog": "^1.1.1",
"@radix-ui/react-dropdown-menu": "^2.1.1",
"@radix-ui/react-label": "^2.1.0",
"@radix-ui/react-popover": "^1.1.1",
"@radix-ui/react-select": "^2.1.1",
"@radix-ui/react-slot": "^1.1.0",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
"date-fns": "^3.6.0",
"embla-carousel-react": "^8.1.6",
"lucide-react": "^0.394.0",
"next": "^14.2.4",
"next-intl": "^3.15.2",
"react": "^18.0.0",
"react-day-picker": "^8.10.1",
"react-dom": "^18.0.0",
"react-dropzone": "^14.2.3",
"react-hook-form": "^7.52.0",
"tailwind-merge": "^2.2.2",
"vaul": "^0.9.1",
"zod": "^3.22.4"
}
}
Loading