feat: add product name filtering and retrieval in product management
This commit is contained in:
parent
f008e46f54
commit
fe0c565196
@ -24,10 +24,11 @@ public function index(PaginatedRequest $request): Response
|
||||
return Inertia::render('admin/master/product/index', [
|
||||
'products' => $this->service->paginated(
|
||||
...$request->validatedWithDefaults(),
|
||||
filters: $request->only(['status', 'stock', 'category']),
|
||||
filters: $request->only(['status', 'stock', 'category', 'name']),
|
||||
),
|
||||
'categories' => $this->categoryService->getAll(),
|
||||
'filters' => $request->only(['status', 'stock', 'category']),
|
||||
'productNames' => $this->service->getNames(),
|
||||
'filters' => $request->only(['status', 'stock', 'category', 'name']),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@ -19,6 +19,16 @@ public function __construct(
|
||||
private StockMutationService $stockMutationService,
|
||||
) {}
|
||||
|
||||
public function getNames(): array
|
||||
{
|
||||
return Product::where('status', '!=', 'deleted')
|
||||
->orderBy('name')
|
||||
->pluck('name')
|
||||
->unique()
|
||||
->values()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
public function getAll(array $filters = []): Collection
|
||||
{
|
||||
$products = Product::select(['id', 'name', 'slug', 'description', 'status'])
|
||||
@ -53,6 +63,7 @@ public function paginated(int $perPage = 25, string $search = '', string $sort =
|
||||
'productVariants.productPrices:id,variant_id,type,price',
|
||||
])
|
||||
->when($search, fn ($q) => $q->where('name', 'like', "%{$search}%"))
|
||||
->when($filters['name'] ?? null, fn ($q, $name) => $q->where('name', 'like', "%{$name}%"))
|
||||
->when($filters['status'] ?? null, fn ($q, $status) => $q->where('status', $status))
|
||||
->when($filters['category'] ?? null, fn ($q, $categoryId) => $q->whereHas('categories', function ($cq) use ($categoryId) {
|
||||
$cq->where('categories.id', $categoryId);
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { router } from '@inertiajs/react';
|
||||
import { Link, router } from '@inertiajs/react';
|
||||
import { CardTable } from '@/components/card-table';
|
||||
import { DeleteConfirmDialog } from '@/components/delete-confirm-dialog';
|
||||
import { FilterPopover } from '@/components/filter-popover';
|
||||
@ -34,7 +34,7 @@ import {
|
||||
edit as variantEdit,
|
||||
} from '@/routes/admin/master/products/variants';
|
||||
import { Head } from '@inertiajs/react';
|
||||
import { Link, Plus } from 'lucide-react';
|
||||
import { Plus } from 'lucide-react';
|
||||
import { useMemo, useState } from 'react';
|
||||
import type { Product, ProductVariant } from './columns';
|
||||
import { ProductCardRow } from './product-card';
|
||||
@ -52,6 +52,7 @@ type Props = {
|
||||
id: number;
|
||||
name: string;
|
||||
}[];
|
||||
productNames: string[];
|
||||
filters: {
|
||||
status?: string;
|
||||
name?: string;
|
||||
@ -60,7 +61,7 @@ type Props = {
|
||||
};
|
||||
};
|
||||
|
||||
export default function ProductIndex({ products, categories, filters }: Props) {
|
||||
export default function ProductIndex({ products, categories, productNames, filters }: Props) {
|
||||
const { can } = useCan();
|
||||
const [deleting, setDeleting] = useState<Product | null>(null);
|
||||
const [deletingVariant, setDeletingVariant] = useState<{
|
||||
@ -92,11 +93,10 @@ export default function ProductIndex({ products, categories, filters }: Props) {
|
||||
filterWithParams: false,
|
||||
});
|
||||
|
||||
const productNames = useMemo(() => {
|
||||
const names = products.data.map((p) => p.name);
|
||||
|
||||
return [...new Set(names)].sort();
|
||||
}, [products.data]);
|
||||
const sortedProductNames = useMemo(
|
||||
() => [...productNames].sort(),
|
||||
[productNames],
|
||||
);
|
||||
|
||||
const selectedCategory = useMemo(
|
||||
() => categories.find((c) => String(c.id) === filters.category) ?? null,
|
||||
@ -147,7 +147,7 @@ export default function ProductIndex({ products, categories, filters }: Props) {
|
||||
Nama Produk
|
||||
</label>
|
||||
<Combobox
|
||||
items={productNames}
|
||||
items={sortedProductNames}
|
||||
value={filters.name ?? ''}
|
||||
onValueChange={(value) =>
|
||||
applyFilter('name', (value as string) ?? '')
|
||||
|
||||
Loading…
Reference in New Issue
Block a user