refactor: ♻️ set generic wrapper for crud filters
This commit is contained in:
parent
99b610c333
commit
bede89f62c
9 changed files with 197 additions and 126 deletions
|
|
@ -7,7 +7,6 @@ use Illuminate\Http\Request;
|
|||
|
||||
class DocumentHelper
|
||||
{
|
||||
|
||||
/**
|
||||
* Extract the Issuu embed URL from an iframe or raw Issuu input.
|
||||
*
|
||||
|
|
@ -51,7 +50,7 @@ class DocumentHelper
|
|||
/**
|
||||
* Return a document query with all the request filters applied
|
||||
*
|
||||
* @param Request $request
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Illuminate\Database\Eloquent\Builder<\App\Models\Document> $query
|
||||
* @return \Illuminate\Database\Eloquent\Builder<\App\Models\Document> $query
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -265,21 +265,22 @@ return [
|
|||
]
|
||||
],
|
||||
"refresh_original" => "reset to original state",
|
||||
"model_filters" => [
|
||||
"button_label" => "filters",
|
||||
"select_status" => [
|
||||
"model_filters" => [
|
||||
"button_tooltip" => "open the filters window",
|
||||
"sheet_title" => "filters window",
|
||||
"select_status" => [
|
||||
"label" => "status",
|
||||
"placeholder" => "choose a status"
|
||||
],
|
||||
"has_no_link" => [
|
||||
"has_no_link" => [
|
||||
"label" => "filter document with no usable link"
|
||||
],
|
||||
"categories" => [
|
||||
"label" => "filter by category",
|
||||
"categories" => [
|
||||
"label" => "filter by category",
|
||||
"placeholder" => "select one or more categories"
|
||||
],
|
||||
"tags" => [
|
||||
"label" => "filter by tag",
|
||||
"tags" => [
|
||||
"label" => "filter by tag",
|
||||
"placeholder" => "select one or more tags"
|
||||
],
|
||||
],
|
||||
|
|
|
|||
|
|
@ -267,20 +267,21 @@ return [
|
|||
],
|
||||
"refresh_original" => "remettre à l'état d'origine",
|
||||
"model_filters" => [
|
||||
"button_label" => "filtres",
|
||||
"select_status" => [
|
||||
"label" => "statut",
|
||||
"button_tooltip" => "ouvrir la fenêtre des filtres",
|
||||
"sheet_title" => "fenêtre des filtres",
|
||||
"select_status" => [
|
||||
"label" => "filtrer par statut",
|
||||
"placeholder" => "selectionner un statut"
|
||||
],
|
||||
"has_no_link" => [
|
||||
"has_no_link" => [
|
||||
"label" => "filtrer les documents qui n'ont aucun lien d'accès"
|
||||
],
|
||||
"categories" => [
|
||||
"label" => "filtrer par categorie",
|
||||
"categories" => [
|
||||
"label" => "filtrer par catégorie",
|
||||
"placeholder" => "selectionner une ou plusieurs catégories"
|
||||
],
|
||||
"tags" => [
|
||||
"label" => "filtrer par étiquette",
|
||||
"tags" => [
|
||||
"label" => "filtrer par étiquette",
|
||||
"placeholder" => "selectionner une ou plusieurs étiquettes"
|
||||
],
|
||||
],
|
||||
|
|
|
|||
100
resources/ts/back/Components/Documents/Filters.tsx
Normal file
100
resources/ts/back/Components/Documents/Filters.tsx
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
// * Packages
|
||||
import { usePage } from "@inertiajs/react";
|
||||
// * Other
|
||||
import useTrans from "@/hooks/use-translations";
|
||||
import str from "@/hooks/use-string";
|
||||
import EnumFilter from "@/back/Components/Filters/EnumFilter";
|
||||
import SwitchFilter from "@/back/Components/Filters/SwitchFilter";
|
||||
import MultiSelectRelationFilter from "@/back/Components/Filters/MultiSelectRelationFilter";
|
||||
import FiltersWrapper from "@/back/Components/FiltersWrapper";
|
||||
import {
|
||||
DocumentCategoryModel,
|
||||
DocumentTagModel,
|
||||
LaravelEnum,
|
||||
} from "@/back/types/global";
|
||||
|
||||
const Filters = () => {
|
||||
const modelName = usePage().props.modelName as string;
|
||||
const categories = usePage().props.categories as DocumentCategoryModel[];
|
||||
const tags = usePage().props.tags as DocumentTagModel[];
|
||||
const enumDocumentStatus = usePage().props.enumDocumentStatus as LaravelEnum;
|
||||
|
||||
return (
|
||||
<FiltersWrapper>
|
||||
<EnumFilter
|
||||
enums={enumDocumentStatus}
|
||||
modelName={modelName}
|
||||
fieldName="status"
|
||||
label={str(
|
||||
useTrans(
|
||||
"back.model_filters.select_status.label",
|
||||
),
|
||||
)
|
||||
.ucFirst()
|
||||
.value()}
|
||||
placeholder={str(
|
||||
useTrans(
|
||||
"back.model_filters.select_status.placeholder",
|
||||
),
|
||||
)
|
||||
.ucFirst()
|
||||
.value()}
|
||||
/>
|
||||
<SwitchFilter
|
||||
fieldName="hasNoLink"
|
||||
modelName={modelName}
|
||||
label={str(
|
||||
useTrans(
|
||||
"back.model_filters.has_no_link.label",
|
||||
),
|
||||
)
|
||||
.ucFirst()
|
||||
.value()}
|
||||
/>
|
||||
<MultiSelectRelationFilter
|
||||
fieldName="categories"
|
||||
modelName={modelName}
|
||||
label={str(
|
||||
useTrans(
|
||||
"back.model_filters.categories.label",
|
||||
),
|
||||
)
|
||||
.ucFirst()
|
||||
.value()}
|
||||
placeholder={str(
|
||||
useTrans(
|
||||
"back.model_filters.categories.placeholder",
|
||||
),
|
||||
)
|
||||
.ucFirst()
|
||||
.value()}
|
||||
options={categories}
|
||||
modelTranslatable={true}
|
||||
optionName="name"
|
||||
/>
|
||||
<MultiSelectRelationFilter
|
||||
fieldName="tags"
|
||||
modelName={modelName}
|
||||
label={str(
|
||||
useTrans(
|
||||
"back.model_filters.tags.label",
|
||||
),
|
||||
)
|
||||
.ucFirst()
|
||||
.value()}
|
||||
placeholder={str(
|
||||
useTrans(
|
||||
"back.model_filters.tags.placeholder",
|
||||
),
|
||||
)
|
||||
.ucFirst()
|
||||
.value()}
|
||||
options={tags}
|
||||
modelTranslatable={true}
|
||||
optionName="name"
|
||||
/>
|
||||
</FiltersWrapper>
|
||||
);
|
||||
};
|
||||
|
||||
export default Filters;
|
||||
62
resources/ts/back/Components/FiltersWrapper.tsx
Normal file
62
resources/ts/back/Components/FiltersWrapper.tsx
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
// * Packages
|
||||
import { Filter } from "lucide-react";
|
||||
import { ReactNode } from "react";
|
||||
// * Components shadcn
|
||||
import {
|
||||
Sheet,
|
||||
SheetContent,
|
||||
SheetDescription,
|
||||
SheetHeader,
|
||||
SheetTitle,
|
||||
SheetTrigger,
|
||||
} from "@/components/ui/sheet";
|
||||
// * Other
|
||||
import BtnOptionnalLinkWithTooltip from "@/back/Components/BtnOptionnalLinkWithTooltip";
|
||||
import useTrans from "@/hooks/use-translations";
|
||||
import str from "@/hooks/use-string";
|
||||
|
||||
const FiltersWrapper = ({ children }: { children: ReactNode }) => {
|
||||
return (
|
||||
<div className="flex justify-between items-center">
|
||||
<div className="flex justify-end gap-2">
|
||||
<Sheet>
|
||||
<SheetTrigger asChild>
|
||||
<BtnOptionnalLinkWithTooltip
|
||||
content={<Filter />}
|
||||
tooltipContent={
|
||||
<p>
|
||||
{str(
|
||||
useTrans(
|
||||
"back.model_filters.button_tooltip",
|
||||
),
|
||||
)
|
||||
.ucFirst()
|
||||
.value()}
|
||||
</p>
|
||||
}
|
||||
type="button"
|
||||
size="icon"
|
||||
variant="outline"
|
||||
/>
|
||||
</SheetTrigger>
|
||||
<SheetContent>
|
||||
<SheetHeader className="mb-4">
|
||||
<SheetTitle>
|
||||
{str(useTrans("back.model_filters.sheet_title"))
|
||||
.ucFirst()
|
||||
.value()}
|
||||
</SheetTitle>
|
||||
</SheetHeader>
|
||||
<SheetDescription asChild>
|
||||
<div className="flex flex-col gap-3">
|
||||
{children}
|
||||
</div>
|
||||
</SheetDescription>
|
||||
</SheetContent>
|
||||
</Sheet>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default FiltersWrapper;
|
||||
|
|
@ -1057,6 +1057,7 @@ export const MultiSelect = forwardRef<MultiSelectRef, MultiSelectProps>(
|
|||
onClick={handleTogglePopover}
|
||||
disabled={disabled}
|
||||
role="combobox"
|
||||
variant="outline"
|
||||
aria-expanded={isPopoverOpen}
|
||||
aria-haspopup="listbox"
|
||||
aria-controls={
|
||||
|
|
@ -1067,10 +1068,9 @@ export const MultiSelect = forwardRef<MultiSelectRef, MultiSelectProps>(
|
|||
getAllOptions().length
|
||||
} options selected. ${placeholder}`}
|
||||
className={cn(
|
||||
"flex rounded-md border min-h-10 h-auto items-center justify-between bg-inherit hover:bg-inherit [&_svg]:pointer-events-auto gap-2 px-4 py-2",
|
||||
"group/button flex rounded-md border min-h-10 h-auto items-center justify-between [&_svg]:pointer-events-auto gap-2 px-4 py-2",
|
||||
autoSize ? "w-auto" : "w-full",
|
||||
responsiveSettings.compactMode &&
|
||||
"min-h-8 text-sm",
|
||||
responsiveSettings.compactMode && "min-h-8 text-sm",
|
||||
screenSize === "mobile" && "min-h-12 text-base",
|
||||
disabled && "opacity-50 cursor-not-allowed",
|
||||
className,
|
||||
|
|
@ -1255,8 +1255,9 @@ export const MultiSelect = forwardRef<MultiSelectRef, MultiSelectProps>(
|
|||
)}
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<div
|
||||
<Button
|
||||
role="button"
|
||||
variant="ghost"
|
||||
tabIndex={0}
|
||||
onClick={(event) => {
|
||||
event.stopPropagation();
|
||||
|
|
@ -1273,26 +1274,24 @@ export const MultiSelect = forwardRef<MultiSelectRef, MultiSelectProps>(
|
|||
}
|
||||
}}
|
||||
aria-label={`Clear all ${selectedValues.length} selected options`}
|
||||
className="flex items-center justify-center h-4 w-4 me-2 cursor-pointer text-muted-foreground hover:text-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-1 rounded-sm"
|
||||
className="flex items-center justify-center h-4 w-4 p-0 me-2 cursor-pointer transition-opacity opacity-50 group-hover:opacity-60 hover:opacity-100 rounded-sm"
|
||||
>
|
||||
<XIcon className="h-4 w-4" />
|
||||
</div>
|
||||
</Button>
|
||||
<Separator
|
||||
orientation="vertical"
|
||||
className="flex min-h-4 h-full"
|
||||
/>
|
||||
<ChevronsUpDownIcon
|
||||
className="h-4 ms-2 cursor-pointer text-muted-foreground"
|
||||
className="h-4 ms-2 cursor-pointer opacity-50 group-hover/button:opacity-60"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex items-center justify-between w-full mx-auto">
|
||||
<span className="text-sm text-muted-foreground">
|
||||
{placeholder}
|
||||
</span>
|
||||
<ChevronsUpDownIcon className="h-4 cursor-pointer text-muted-foreground" />
|
||||
<div className="flex items-center justify-between w-full mx-auto gap-2">
|
||||
<p className="text-start text-sm whitespace-pre-line">{placeholder}</p>
|
||||
<ChevronsUpDownIcon className="h-4 cursor-pointer opacity-50 group-hover/button:opacity-60" />
|
||||
</div>
|
||||
)}
|
||||
</Button>
|
||||
|
|
@ -1401,7 +1400,7 @@ export const MultiSelect = forwardRef<MultiSelectRef, MultiSelectProps>(
|
|||
)}
|
||||
aria-hidden="true"
|
||||
>
|
||||
<CheckIcon className="h-4 w-4" />
|
||||
<CheckIcon className="h-4 w-4 text-white" strokeWidth={4} />
|
||||
</div>
|
||||
<span>
|
||||
(
|
||||
|
|
@ -1467,7 +1466,7 @@ export const MultiSelect = forwardRef<MultiSelectRef, MultiSelectProps>(
|
|||
)}
|
||||
aria-hidden="true"
|
||||
>
|
||||
<CheckIcon className="h-4 w-4" />
|
||||
<CheckIcon className="h-4 w-4 text-white" strokeWidth={4} />
|
||||
</div>
|
||||
{option.icon && (
|
||||
<option.icon
|
||||
|
|
@ -1524,7 +1523,7 @@ export const MultiSelect = forwardRef<MultiSelectRef, MultiSelectProps>(
|
|||
)}
|
||||
aria-hidden="true"
|
||||
>
|
||||
<CheckIcon className="h-4 w-4" />
|
||||
<CheckIcon className="h-4 w-4 text-white" strokeWidth={4} />
|
||||
</div>
|
||||
{option.icon && (
|
||||
<option.icon
|
||||
|
|
|
|||
|
|
@ -1,36 +0,0 @@
|
|||
// * Packages
|
||||
import { Filter } from "lucide-react";
|
||||
// * Components shadcn
|
||||
import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetTrigger } from "@/components/ui/sheet";
|
||||
import { Button } from "@/components/ui/button";
|
||||
// * Other
|
||||
import Filters from "@/back/Pages/Document/Actions/Filters";
|
||||
import useTrans from "@/hooks/use-translations";
|
||||
import str from "@/hooks/use-string";
|
||||
|
||||
const ButtonTrigger = () => {
|
||||
return (
|
||||
<div className="flex justify-between items-center">
|
||||
<div className="flex justify-end gap-2">
|
||||
<Sheet>
|
||||
<SheetTrigger asChild>
|
||||
<Button variant="secondary">
|
||||
<Filter />
|
||||
{str(useTrans("back.model_filters.button_label")).ucFirst().value()}
|
||||
</Button>
|
||||
</SheetTrigger>
|
||||
<SheetContent className="[&>.absolute]:top-[1.5rem] [&>.absolute]:right-[1.5rem] w-[400px] sm:w-[540px] overflow-y-scroll">
|
||||
<SheetHeader className="mb-4">
|
||||
<SheetTitle>
|
||||
{str(useTrans("back.model_filters.button_label")).ucFirst().value()}
|
||||
</SheetTitle>
|
||||
</SheetHeader>
|
||||
<Filters />
|
||||
</SheetContent>
|
||||
</Sheet>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default ButtonTrigger;
|
||||
|
|
@ -1,55 +0,0 @@
|
|||
// * Packages
|
||||
import { usePage } from "@inertiajs/react";
|
||||
// * Components shadcn
|
||||
import { SheetDescription } from "@/components/ui/sheet";
|
||||
// * Other
|
||||
import EnumFilter from "@/back/Components/Filters/EnumFilter";
|
||||
import SwitchFilter from "@/back/Components/Filters/SwitchFilter";
|
||||
import MultiSelectRelationFilter from "@/back/Components/Filters/MultiSelectRelationFilter";
|
||||
import { DocumentCategoryModel, DocumentTagModel, LaravelEnum } from "@/back/types/global";
|
||||
import useTrans from "@/hooks/use-translations";
|
||||
import str from "@/hooks/use-string";
|
||||
|
||||
export default function Filters() {
|
||||
const modelName = usePage().props.modelName as string;
|
||||
const categories = usePage().props.categories as DocumentCategoryModel[];
|
||||
const tags = usePage().props.tags as DocumentTagModel[];
|
||||
const enumDocumentStatus = usePage().props.enumDocumentStatus as LaravelEnum;
|
||||
|
||||
return (
|
||||
<SheetDescription asChild>
|
||||
<div className="flex flex-col gap-3">
|
||||
<EnumFilter
|
||||
enums={enumDocumentStatus}
|
||||
modelName={modelName}
|
||||
fieldName="status"
|
||||
label={str(useTrans('back.model_filters.select_status.label')).ucFirst().value()}
|
||||
placeholder={str(useTrans('back.model_filters.select_status.placeholder')).ucFirst().value()}
|
||||
/>
|
||||
<SwitchFilter
|
||||
fieldName="hasNoLink"
|
||||
modelName={modelName}
|
||||
label={str(useTrans('back.model_filters.has_no_link.label')).ucFirst().value()}
|
||||
/>
|
||||
<MultiSelectRelationFilter
|
||||
fieldName="categories"
|
||||
modelName={modelName}
|
||||
label={str(useTrans('back.model_filters.categories.label')).ucFirst().value()}
|
||||
placeholder={str(useTrans('back.model_filters.categories.placeholder')).ucFirst().value()}
|
||||
options={categories}
|
||||
modelTranslatable={true}
|
||||
optionName="name"
|
||||
/>
|
||||
<MultiSelectRelationFilter
|
||||
fieldName="tags"
|
||||
modelName={modelName}
|
||||
label={str(useTrans('back.model_filters.tags.label')).ucFirst().value()}
|
||||
placeholder={str(useTrans('back.model_filters.tags.placeholder')).ucFirst().value()}
|
||||
options={tags}
|
||||
modelTranslatable={true}
|
||||
optionName="name"
|
||||
/>
|
||||
</div>
|
||||
</SheetDescription>
|
||||
);
|
||||
}
|
||||
|
|
@ -16,9 +16,9 @@ import DataTable from "@/back/Components/Table/DataTable";
|
|||
import DataTableSearch from "@/back/Components/Table/DataTableSearch";
|
||||
import DataTableFilterButton from "@/back/Components/Table/DataTableFilterButton";
|
||||
import BtnOptionnalLinkWithTooltip from "@/back/Components/BtnOptionnalLinkWithTooltip";
|
||||
import ButtonTrigger from "@/back/Pages/Document/Actions/ButtonTrigger";
|
||||
import DocumentStatusBadge from "@/back/Components/Documents/DocumentStatusBadge";
|
||||
import DocumentFinanceTagBadge from "@/back/Components/Documents/DocumentFinanceTagBadge";
|
||||
import Filters from "@/back/Components/Documents/Filters";
|
||||
import { cn, Views } from "@/lib/utils";
|
||||
import { renderTranslationCell } from "@/lib/translationCellFallback";
|
||||
import { useFormatedDate } from "@/hooks/use-formated-value";
|
||||
|
|
@ -260,7 +260,7 @@ export default function Index({
|
|||
searchText={search}
|
||||
searchFields={searchFields}
|
||||
>
|
||||
<ButtonTrigger />
|
||||
<Filters />
|
||||
</DataTableSearch>
|
||||
<DataTable
|
||||
columns={columns}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue