feat:update agent

This commit is contained in:
Rama Priyanto 2026-02-12 15:09:54 +07:00
parent 8e88205bf0
commit 8ce5d3e8dc
3 changed files with 156 additions and 2 deletions

View File

@ -2,11 +2,38 @@
import { useEffect, useMemo, useState } from "react";
import Navbar from "../navbar";
import { close, loading } from "@/config/swal";
import { getAllAgent } from "@/service/agent";
import {
createAgentData,
getAgentByAgentId,
getAllAgent,
} from "@/service/agent";
import { convertDateFormat, getCookiesDecrypt } from "@/utils/globals";
import Link from "next/link";
import CustomPagination from "../custom-pagination";
import { Switch } from "../ui/switch";
import { Button } from "../ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "../ui/dialog";
import { Input } from "../ui/input";
interface AgentDetail {
agent_id: string;
description: string;
instructions: string;
is_active: boolean;
name: string;
status: boolean;
type: string;
}
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
export default function AgentsManagement() {
const roleId = getCookiesDecrypt("urie");
@ -20,6 +47,8 @@ export default function AgentsManagement() {
const [page, setPage] = useState(1);
const [limit, setLimit] = useState(5);
const [totalData, setTotalData] = useState(0);
const [agentDetail, setAgentDetail] = useState<AgentDetail[]>([]);
const [isModalOpen, setIsModalOpen] = useState(false);
const ITEMS_PER_PAGE = 6;
@ -55,10 +84,111 @@ export default function AgentsManagement() {
const totalPages = Math.ceil(filteredagents.length / limit);
const getDataAgent = async () => {
loading();
const res = await fetch(
"https://narasiahli.com/ai/api/v1/agents/?skip=0&limit=100&include_teams=true",
);
const json = await res.json();
const temp = [];
for (const element of json) {
const data = {
agent_id: element.id,
description: element.description,
instructions: element.instruction ?? "",
is_active: true,
name: element.name,
status: true,
type: "",
};
const now = await checkAgent(element.id);
if (now) {
temp.push(data);
}
}
setAgentDetail(temp);
close();
setIsModalOpen(true);
};
const checkAgent = async (id: string) => {
const res = await getAgentByAgentId(id);
return res.error ? true : false;
};
const handleExpertiseChange = (index: number, value: string) => {
setAgentDetail((prev) => {
const updated = [...prev];
updated[index] = {
...updated[index],
type: value,
};
return updated;
});
};
const handleSave = async () => {
loading();
for (const element of agentDetail) {
const data = {
agent_id: element.agent_id,
description: element.description,
instructions: element.instructions ?? "",
is_active: true,
name: element.name,
status: true,
type: element.type.toLocaleLowerCase(),
};
await createAgentData(data);
await sleep(5000);
}
close();
};
return (
<div className="flex-1 overflow-hidden flex flex-col h-[90vh]">
<Navbar title="Tenaga Ahli" />
<div className="border rounded-md overflow-auto mt-10">
<Button onClick={getDataAgent} className="mt-10 w-fit">
Perbarui Data
</Button>
<Dialog open={isModalOpen} onOpenChange={setIsModalOpen}>
{/* <DialogTrigger asChild>
<Button variant="outline">Scrollable Content</Button>
</DialogTrigger> */}
<DialogContent>
<DialogHeader>
<DialogTitle>Perbarui Data Tenaga Ahli</DialogTitle>
</DialogHeader>
<div className="no-scrollbar -mx-4 max-h-[50vh] overflow-y-auto px-4">
{agentDetail.map((agent, index) => (
<div key={agent.agent_id} className="mb-4 flex flex-col gap-1">
<p className="text-sm">
Nama : <span className="font-semibold">{agent.name}</span>
</p>
<p className="text-sm">Deskripsi : {agent.description}</p>
<p className="text-sm">
Instruksi :{" "}
{agent.instructions == "" ? "-" : agent.instructions}
</p>
<p className="text-sm">Keahlian :</p>
<Input
id="name"
placeholder="Contoh : Ahli Hukum"
className="h-12 rounded-none focus-visible:ring-0 focus-visible:ring-offset-0 focus:outline-none"
value={agent.type}
onChange={(e) => handleExpertiseChange(index, e.target.value)}
/>
</div>
))}
</div>
<DialogFooter>
<Button onClick={handleSave}>Save</Button>
</DialogFooter>
</DialogContent>
</Dialog>
<div className="border rounded-md overflow-auto mt-3">
<table className="w-full text-sm">
<thead>
<tr className="bg-gray-50 text-left">

View File

@ -29,6 +29,8 @@ import {
SelectTrigger,
SelectValue,
} from "../ui/select";
import Swal from "sweetalert2";
import withReactContent from "sweetalert2-react-content";
type User = {
id: number;
@ -66,6 +68,7 @@ export default function ManajemenUser() {
const [totalData, setTotalData] = useState(0);
const [totalPage, setTotalPage] = useState(1);
const [userType, setUserType] = useState("0");
const MySwal = withReactContent(Swal);
useEffect(() => {
initFetch();
@ -186,8 +189,24 @@ export default function ManajemenUser() {
return false;
}
close();
successSubmit();
};
function successSubmit() {
MySwal.fire({
title: "Sukses",
icon: "success",
confirmButtonColor: "#3085d6",
confirmButtonText: "OK",
}).then((result) => {
if (result.isConfirmed) {
initFetch();
} else {
initFetch();
}
});
}
let typingTimer: NodeJS.Timeout;
const doneTypingInterval = 1500;

View File

@ -26,3 +26,8 @@ export async function updateAgentById(data: any, id: string) {
const response = await httpPutInterceptor(`/agent/${id}`, data);
return response;
}
export async function getAgentByAgentId(id: string) {
const response = await httpGetInterceptor(`/agent/agent_id/${id}`);
return response;
}