FPSMS-frontend
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

99 lines
2.5 KiB

  1. "use client";
  2. import { useCallback, useMemo, useState } from "react";
  3. import SearchBox, { Criterion } from "../SearchBox";
  4. import { ItemsResult } from "@/app/api/settings/item";
  5. import { useTranslation } from "react-i18next";
  6. import SearchResults, { Column } from "../SearchResults";
  7. import { EditNote } from "@mui/icons-material";
  8. import { useRouter, useSearchParams } from "next/navigation";
  9. import { GridDeleteIcon } from "@mui/x-data-grid";
  10. import { TypeEnum } from "@/app/utils/typeEnum";
  11. type Props = {
  12. items: ItemsResult[];
  13. };
  14. type SearchQuery = Partial<Omit<ItemsResult, "id">>;
  15. type SearchParamNames = keyof SearchQuery;
  16. const ItemsSearch: React.FC<Props> = ({ items }) => {
  17. const [filteredItems, setFilteredItems] = useState<ItemsResult[]>(items);
  18. const { t } = useTranslation("items");
  19. const router = useRouter();
  20. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
  21. () => {
  22. var searchCriteria: Criterion<SearchParamNames>[] = [
  23. { label: t("Code"), paramName: "code", type: "text" },
  24. { label: t("Name"), paramName: "name", type: "text" },
  25. ]
  26. return searchCriteria
  27. },
  28. [t, items]
  29. );
  30. const onDetailClick = useCallback(
  31. (item: ItemsResult) => {
  32. router.push(`/settings/items/edit?id=${item.id}`);
  33. },
  34. [router]
  35. );
  36. const onDeleteClick = useCallback(
  37. (item: ItemsResult) => {},
  38. [router]
  39. );
  40. const columns = useMemo<Column<ItemsResult>[]>(
  41. () => [
  42. {
  43. name: "id",
  44. label: t("Details"),
  45. onClick: onDetailClick,
  46. buttonIcon: <EditNote />,
  47. },
  48. {
  49. name: "code",
  50. label: t("Code"),
  51. },
  52. {
  53. name: "name",
  54. label: t("Name"),
  55. },
  56. {
  57. name: "action",
  58. label: t(""),
  59. buttonIcon: <GridDeleteIcon />,
  60. onClick: onDeleteClick,
  61. },
  62. ],
  63. [filteredItems]
  64. );
  65. const onReset = useCallback(() => {
  66. setFilteredItems(items);
  67. }, [items]);
  68. return (
  69. <>
  70. <SearchBox
  71. criteria={searchCriteria}
  72. onSearch={(query) => {
  73. setFilteredItems(
  74. items.filter((pm) => {
  75. return (
  76. pm.code.toLowerCase().includes(query.code.toLowerCase()) &&
  77. pm.name.toLowerCase().includes(query.name.toLowerCase())
  78. );
  79. })
  80. );
  81. }}
  82. onReset={onReset}
  83. />
  84. <SearchResults<ItemsResult> items={filteredItems} columns={columns} />
  85. </>
  86. );
  87. };
  88. export default ItemsSearch;