Use server actions instead of client-side API calls

This commit is contained in:
Darren Clarke 2024-08-05 23:31:15 +02:00
parent 5a3127dcb0
commit aa453954ed
30 changed files with 703 additions and 462 deletions

View file

@ -1,56 +1,78 @@
"use server";
import { revalidatePath } from "next/cache";
import { getTicketQuery } from "app/_graphql/getTicketQuery";
import { getTicketArticlesQuery } from "app/_graphql/getTicketArticlesQuery";
import { createTicketMutation } from "app/_graphql/createTicketMutation";
import { updateTicketMutation } from "app/_graphql/updateTicketMutation";
import { updateTagsMutation } from "app/_graphql/updateTagsMutation";
// import { executeMutation } from "app/_lib/graphql";
// import { AddAssetMutation } from "../_graphql/AddAssetMutation";
import { executeGraphQL, executeREST } from "app/_lib/zammad";
export const createTicketAction = async (
currentState: any,
formData: FormData,
) => {
/*
const createTicket = async () => {
await fetcher({
document: createTicketMutation,
variables: {
input: {
ticket,
},
},
});
closeDialog();
setBody("");
};
try {
const ticket = {
groupId: formData.get("groupId"),
customerId: `gid://zammad/User/3`, // { email: formData.get("customerId") },
title: formData.get("title"),
article: {
internal: true,
body: formData.get("details"),
},
};
const result = await executeMutation({
project,
mutation: AddAssetMutation,
console.log({ ticket });
const result = await executeGraphQL({
query: createTicketMutation,
variables: {
input: asset,
input: ticket,
},
});
revalidatePath(`/${project}/assets`);
console.log({ result });
return {
...currentState,
values: { ...asset, ...result.addAsset, project },
values: ticket,
success: true,
};
} catch (e: any) {
return { success: false, message: e?.message ?? "Unknown error" };
console.log({ e });
return {
success: false,
values: {},
message: e?.message ?? "Unknown error",
};
}
};
export const createTicketArticleAction = async (
ticketID: string,
article: Record<string, any>,
) => {
try {
const result = await executeGraphQL({
query: updateTicketMutation,
variables: {
ticketId: `gid://zammad/Ticket/${ticketID}`,
input: { article },
},
});
console.log({ result });
return {
result,
success: true,
};
} catch (e: any) {
console.log({ e });
return {
success: false,
message: e?.message ?? "Unknown error",
};
}
*/
};
export const updateTicketAction = async (
@ -85,6 +107,24 @@ export const updateTicketAction = async (
*/
};
export const getTicketAction = async (id: string) => {
const ticketData = await executeGraphQL({
query: getTicketQuery,
variables: { ticketId: `gid://zammad/Ticket/${id}` },
});
return ticketData?.ticket;
};
export const getTicketArticlesAction = async (id: string) => {
const ticketData = await executeGraphQL({
query: getTicketArticlesQuery,
variables: { ticketId: `gid://zammad/Ticket/${id}` },
});
return ticketData?.ticketArticles;
};
export const updateTicketTagsAction = async (
currentState: any,
formData: FormData,
@ -116,3 +156,27 @@ export const updateTicketTagsAction = async (
}
*/
};
export const getTicketStatesAction = async () => {
const states = await executeREST({
path: "/api/v1/ticket_states",
});
return states;
};
export const getTicketTagsAction = async () => {
const states = await executeREST({
path: "/api/v1/tags",
});
return states;
};
export const getTicketPrioritiesAction = async () => {
const priorities = await executeREST({
path: "/api/v1/ticket_priorities",
});
return priorities;
};