Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

102 строки
3.4 KiB

  1. "use client";
  2. import { Customer } from "@/app/api/customer";
  3. import React, { useCallback, useEffect, useMemo, useState } from "react";
  4. import SearchBox, { Criterion } from "../SearchBox";
  5. import { useTranslation } from "react-i18next";
  6. import SearchResults, { Column } from "../SearchResults";
  7. import EditNote from "@mui/icons-material/EditNote";
  8. import DeleteIcon from '@mui/icons-material/Delete';
  9. import { useRouter, useSearchParams } from "next/navigation";
  10. import { deleteDialog, successDialog } from "../Swal/CustomAlerts";
  11. import { deleteCustomer } from "@/app/api/customer/actions";
  12. import { MAINTAIN_CLIENT } from "@/middleware";
  13. interface Props {
  14. customers: Customer[];
  15. abilities: String[];
  16. }
  17. type SearchQuery = Partial<Omit<Customer, "id">>;
  18. type SearchParamNames = keyof SearchQuery;
  19. const CustomerSearch: React.FC<Props> = ({ customers, abilities }) => {
  20. const { t } = useTranslation();
  21. const router = useRouter()
  22. const searchParams = useSearchParams()
  23. const maintainClient = abilities.includes(MAINTAIN_CLIENT)
  24. const [filteredCustomers, setFilteredCustomers] = useState(customers);
  25. useEffect(() => {
  26. setFilteredCustomers(customers)
  27. }, [customers])
  28. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
  29. () => [
  30. { label: t("Customer Code"), paramName: "code", type: "text" },
  31. { label: t("Customer Name"), paramName: "name", type: "text" },
  32. ],
  33. [t],
  34. );
  35. const onReset = useCallback(() => {
  36. setFilteredCustomers(customers);
  37. }, [customers]);
  38. const onTaskClick = useCallback((customer: Customer) => {
  39. const params = new URLSearchParams(searchParams.toString())
  40. params.set("id", customer.id.toString())
  41. router.push(`/settings/customer/edit?${params.toString()}`);
  42. }, []);
  43. const onDeleteClick = useCallback((customer: Customer) => {
  44. deleteDialog(async() => {
  45. await deleteCustomer(customer.id)
  46. successDialog("Delete Success", t)
  47. }, t)
  48. }, []);
  49. const columns = useMemo<Column<Customer>[]>(
  50. () => [
  51. {
  52. name: "id",
  53. label: t("Details"),
  54. onClick: onTaskClick,
  55. buttonIcon: <EditNote />,
  56. isHidden: !maintainClient
  57. },
  58. { name: "code", label: t("Customer Code") },
  59. { name: "name", label: t("Customer Name") },
  60. {
  61. name: "id",
  62. label: t("Delete"),
  63. onClick: onDeleteClick,
  64. buttonIcon: <DeleteIcon />,
  65. color: "error",
  66. isHidden: !maintainClient
  67. },
  68. ],
  69. [onTaskClick, t],
  70. );
  71. return (
  72. <>
  73. <SearchBox
  74. criteria={searchCriteria}
  75. onSearch={(query) => {
  76. setFilteredCustomers(
  77. customers.filter(
  78. (customer) =>
  79. customer.code.toLowerCase().includes(query.code.toLowerCase()) &&
  80. customer.name.toLowerCase().includes(query.name.toLowerCase()),
  81. ),
  82. );
  83. }}
  84. onReset={onReset}
  85. />
  86. <SearchResults items={filteredCustomers} columns={columns} />
  87. </>
  88. );
  89. };
  90. export default CustomerSearch;