2023-06-26 10:07:12 +00:00
|
|
|
"use client";
|
|
|
|
|
|
2023-10-16 09:20:40 +02:00
|
|
|
import { FC, useEffect, useState } from "react";
|
2023-06-07 08:02:29 +00:00
|
|
|
import useSWR from "swr";
|
2023-06-26 10:07:12 +00:00
|
|
|
import { TicketList } from "./TicketList";
|
2023-11-10 14:17:09 +01:00
|
|
|
import { getTicketOverviewCountsQuery } from "app/_graphql/getTicketOverviewCountsQuery";
|
2023-10-23 13:27:42 +02:00
|
|
|
import { getTicketsByOverviewQuery } from "app/_graphql/getTicketsByOverviewQuery";
|
2023-06-26 10:07:12 +00:00
|
|
|
|
|
|
|
|
type ZammadOverviewProps = {
|
|
|
|
|
name: string;
|
|
|
|
|
};
|
2022-12-02 17:45:14 +01:00
|
|
|
|
2023-11-10 14:17:09 +01:00
|
|
|
export const ZammadOverview: FC<ZammadOverviewProps> = ({ name }) => {
|
|
|
|
|
const [overviewID, setOverviewID] = useState(null);
|
2023-10-16 09:20:40 +02:00
|
|
|
const [tickets, setTickets] = useState([]);
|
|
|
|
|
const [error, setError] = useState(null);
|
2023-11-10 14:17:09 +01:00
|
|
|
|
|
|
|
|
const { data: overviewData, error: overviewError }: any = useSWR(
|
|
|
|
|
{
|
|
|
|
|
document: getTicketOverviewCountsQuery,
|
|
|
|
|
},
|
|
|
|
|
{ refreshInterval: 10000 },
|
|
|
|
|
);
|
2023-06-07 08:02:29 +00:00
|
|
|
const { data: ticketData, error: ticketError }: any = useSWR(
|
|
|
|
|
{
|
|
|
|
|
document: getTicketsByOverviewQuery,
|
2023-11-10 14:17:09 +01:00
|
|
|
variables: { overviewId: overviewID, pageSize: 250 },
|
2023-06-07 08:02:29 +00:00
|
|
|
},
|
2023-07-18 08:16:16 +00:00
|
|
|
{ refreshInterval: 10000 },
|
2023-06-07 08:02:29 +00:00
|
|
|
);
|
2023-11-10 14:17:09 +01:00
|
|
|
const overviewLookup = {
|
|
|
|
|
Assigned: "My Assigned Tickets",
|
|
|
|
|
Open: "Open Tickets",
|
|
|
|
|
Urgent: "Escalated Tickets",
|
|
|
|
|
Unassigned: "Unassigned & Open Tickets",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const findOverviewByName = (name: string) => {
|
|
|
|
|
const fullName = overviewLookup[name];
|
|
|
|
|
return overviewData?.ticketOverviews?.edges?.find(
|
|
|
|
|
(overview: any) => overview.node.name === fullName,
|
|
|
|
|
)?.node?.id;
|
|
|
|
|
};
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (overviewData) {
|
|
|
|
|
setOverviewID(findOverviewByName(name));
|
|
|
|
|
}
|
|
|
|
|
}, [overviewData, name]);
|
|
|
|
|
console.log({
|
|
|
|
|
name,
|
|
|
|
|
overviewID,
|
|
|
|
|
overviewData,
|
|
|
|
|
overviewError,
|
|
|
|
|
ticketData,
|
|
|
|
|
ticketError,
|
|
|
|
|
});
|
2023-06-07 08:02:29 +00:00
|
|
|
|
2023-10-16 09:20:40 +02:00
|
|
|
const restFetcher = (url: string) => fetch(url).then((r) => r.json());
|
|
|
|
|
const { data: recent } = useSWR("/api/v1/recent_view", restFetcher);
|
2023-06-07 08:02:29 +00:00
|
|
|
|
2023-10-16 09:20:40 +02:00
|
|
|
const sortTickets = (tickets: any) => {
|
|
|
|
|
return tickets.sort((a: any, b: any) => {
|
|
|
|
|
if (a.internalId < b.internalId) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if (a.internalId > b.internalId) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (name != "Recent") {
|
|
|
|
|
const edges = ticketData?.ticketsByOverview?.edges;
|
|
|
|
|
if (edges) {
|
|
|
|
|
const nodes = edges.map((edge: any) => edge.node);
|
2023-11-10 14:17:09 +01:00
|
|
|
console.log({ nodes });
|
|
|
|
|
setError(null);
|
2023-10-16 09:20:40 +02:00
|
|
|
setTickets(sortTickets(nodes));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ticketError) {
|
|
|
|
|
setError(ticketError);
|
|
|
|
|
}
|
2023-10-02 14:22:48 +02:00
|
|
|
}
|
2023-10-16 09:20:40 +02:00
|
|
|
}, [ticketData, ticketError]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const fetchRecentTickets = async () => {
|
|
|
|
|
if (name === "Recent" && recent) {
|
|
|
|
|
let allTickets = [];
|
|
|
|
|
for (const rec of recent) {
|
|
|
|
|
const res = await fetch(`/api/v1/tickets/${rec.o_id}`);
|
|
|
|
|
const tkt = await res.json();
|
|
|
|
|
allTickets.push({
|
|
|
|
|
...tkt,
|
|
|
|
|
internalId: tkt.id,
|
|
|
|
|
createdAt: tkt.created_at,
|
|
|
|
|
updatedAt: tkt.updated_at,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
setTickets(sortTickets(allTickets));
|
|
|
|
|
console.log({ allTickets });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
fetchRecentTickets();
|
|
|
|
|
}, [name]);
|
|
|
|
|
|
|
|
|
|
const shouldRender = tickets && !error;
|
2023-11-10 14:17:09 +01:00
|
|
|
console.log({ shouldRender, tickets, error });
|
2023-10-16 09:20:40 +02:00
|
|
|
return shouldRender && <TicketList title={name} tickets={tickets} />;
|
2023-06-07 08:02:29 +00:00
|
|
|
};
|