"use client"; import React, { useCallback, useEffect, useMemo, useState } from "react"; import SearchBox, { Criterion } from "../SearchBox/index"; import { useTranslation } from "react-i18next"; import SearchResults, { Column } from "../SearchResults/index"; import EditNote from "@mui/icons-material/EditNote"; import DeleteIcon from "@mui/icons-material/Delete"; import { useRouter } from "next/navigation"; import { deleteDialog, successDialog } from "../Swal/CustomAlerts"; import { SkillResult } from "@/app/api/skill"; import { deleteSkill } from "@/app/api/skill/actions"; import { MAINTAIN_SKILL } from "@/middleware"; interface Props { skill: SkillResult[]; abilities: String[]; } type SearchQuery = Partial>; type SearchParamNames = keyof SearchQuery; const SkillSearch: React.FC = ({ skill, abilities }) => { const { t } = useTranslation(); const [filteredSkill, setFilteredSkill] = useState(skill); const router = useRouter(); // translation const skillName = t("Skill Name") const skillCode = t("Skill Code") const deleteSuccess = t("Delete Success") const edit = t("Actions") const name = t("name") const code = t("code") const description = t("description") const maintainSkill = abilities.includes(MAINTAIN_SKILL) const searchCriteria: Criterion[] = useMemo( () => [ { label: skillName, paramName: "name", type: "text", }, { label: skillCode, paramName: "code", type: "text", }, ], [t] ); const onSkillClick = useCallback( (skill: SkillResult) => { console.log(skill); const id = skill.id; router.push(`/settings/skill/edit?id=${id}`); }, [router, t] ); const deleteClick = useCallback((skill: SkillResult) => { deleteDialog(async () => { await deleteSkill(skill.id); successDialog(deleteSuccess, t); }, t); }, []); useEffect(() => { setFilteredSkill(skill) }, [skill]); const columns = useMemo[]>( () => [ { name: "action", label: edit, onClick: onSkillClick, buttonIcon: , isHidden: !maintainSkill, }, { name: "name", label: name }, { name: "code", label: code }, { name: "description", label: description }, { name: "delete", label: t("Delete"), onClick: deleteClick, buttonIcon: , color: "error", isHidden: !maintainSkill, }, ], [t, onSkillClick, deleteClick] ); return ( <> { setFilteredSkill( skill.filter( (s) => s.name.toLowerCase().includes(query.name.toLowerCase()) && s.code.toLowerCase().includes(query.code.toLowerCase()) ) ); }} /> items={filteredSkill} columns={columns} /> ); }; export default SkillSearch;