Ticket edit updates

This commit is contained in:
Darren Clarke 2024-08-07 15:25:53 +02:00
parent 2568547384
commit 87724bb7b8
9 changed files with 297 additions and 352 deletions

View file

@ -151,7 +151,7 @@ const MenuItem = ({
} }
/> />
)} )}
{badge && badge > 0 ? ( {open && badge && badge > 0 ? (
<ListItemSecondaryAction> <ListItemSecondaryAction>
<Typography <Typography
color="textSecondary" color="textSecondary"
@ -197,7 +197,6 @@ export const Sidebar: FC<SidebarProps> = ({
useEffect(() => { useEffect(() => {
const fetchCounts = async () => { const fetchCounts = async () => {
const counts = await getOverviewTicketCountsAction(); const counts = await getOverviewTicketCountsAction();
console.log({ counts });
setOverviewCounts(counts); setOverviewCounts(counts);
}; };
@ -422,8 +421,9 @@ export const Sidebar: FC<SidebarProps> = ({
/> />
<Collapse <Collapse
in={ in={
pathname.startsWith("/overview") || open &&
pathname.startsWith("/tickets") (pathname.startsWith("/overview") ||
pathname.startsWith("/tickets"))
} }
timeout="auto" timeout="auto"
unmountOnExit unmountOnExit
@ -512,7 +512,7 @@ export const Sidebar: FC<SidebarProps> = ({
/> />
)} )}
<Collapse <Collapse
in={pathname.startsWith("/leafcutter")} in={open && pathname.startsWith("/leafcutter")}
timeout="auto" timeout="auto"
unmountOnExit unmountOnExit
onClick={undefined} onClick={undefined}
@ -575,7 +575,7 @@ export const Sidebar: FC<SidebarProps> = ({
open={open} open={open}
/> />
<Collapse <Collapse
in={pathname.startsWith("/admin/")} in={open && pathname.startsWith("/admin/")}
timeout="auto" timeout="auto"
unmountOnExit unmountOnExit
onClick={undefined} onClick={undefined}
@ -588,7 +588,7 @@ export const Sidebar: FC<SidebarProps> = ({
open={open} open={open}
/> />
<Collapse <Collapse
in={pathname.startsWith("/admin/bridge")} in={open && pathname.startsWith("/admin/bridge")}
timeout="auto" timeout="auto"
unmountOnExit unmountOnExit
onClick={undefined} onClick={undefined}

View file

@ -20,7 +20,7 @@ export const ZammadOverview: FC<ZammadOverviewProps> = ({ name }) => {
fetchTickets(); fetchTickets();
const interval = setInterval(fetchTickets, 20000); const interval = setInterval(fetchTickets, 10000);
return () => clearInterval(interval); return () => clearInterval(interval);
}, [name]); }, [name]);

View file

@ -61,8 +61,9 @@ export const TicketDetail: FC<TicketDetailProps> = ({ id }) => {
const shouldRender = !!ticket && !!ticketArticles; const shouldRender = !!ticket && !!ticketArticles;
return ( return (
shouldRender && ( <Box sx={{ height: "100%", width: "100%", background: veryLightGray }}>
<Box sx={{ height: "100%", width: "100%" }}> {shouldRender && (
<>
<MainContainer> <MainContainer>
<ChatContainer> <ChatContainer>
<ConversationHeader> <ConversationHeader>
@ -167,7 +168,8 @@ export const TicketDetail: FC<TicketDetailProps> = ({ id }) => {
kind={articleKind} kind={articleKind}
recipient={recipient} recipient={recipient}
/> />
</>
)}
</Box> </Box>
)
); );
}; };

View file

@ -1,18 +1,15 @@
"use client"; "use client";
import { FC, useEffect, useState } from "react"; import { FC, useEffect, useState } from "react";
import { Grid, Box, MenuItem } from "@mui/material"; import { Grid, Box } from "@mui/material";
import { useFormState } from "react-dom";
import { Select, Button } from "@link-stack/ui"; import { Select, Button } from "@link-stack/ui";
import { MuiChipsInput } from "mui-chips-input"; import { MuiChipsInput } from "mui-chips-input";
import { getTicketQuery } from "app/_graphql/getTicketQuery";
import { DatePicker } from "@mui/x-date-pickers/DatePicker"; import { DatePicker } from "@mui/x-date-pickers/DatePicker";
import { import {
updateTicketAction, updateTicketAction,
getTicketAction, getTicketAction,
getTicketStatesAction, getTicketStatesAction,
getTicketPrioritiesAction, getTicketPrioritiesAction,
getTicketTagsAction,
} from "app/_actions/tickets"; } from "app/_actions/tickets";
import { getAgentsAction } from "app/_actions/users"; import { getAgentsAction } from "app/_actions/users";
import { getGroupsAction } from "app/_actions/groups"; import { getGroupsAction } from "app/_actions/groups";
@ -23,56 +20,51 @@ interface TicketEditProps {
export const TicketEdit: FC<TicketEditProps> = ({ id }) => { export const TicketEdit: FC<TicketEditProps> = ({ id }) => {
const [ticket, setTicket] = useState<any>(); const [ticket, setTicket] = useState<any>();
const [hasChanges, setHasChanges] = useState(false);
const [formState, setFormState] = useState({
values: {
group: null,
owner: null,
priority: null,
pendingTime: null,
state: null,
tags: [],
},
});
const [ticketStates, setTicketStates] = useState<any>(); const [ticketStates, setTicketStates] = useState<any>();
const [ticketPriorities, setTicketPriorities] = useState<any>(); const [ticketPriorities, setTicketPriorities] = useState<any>();
const [groups, setGroups] = useState<any>(); const [groups, setGroups] = useState<any>();
const [tags, setTags] = useState<any>();
const [agents, setAgents] = useState<any>(); const [agents, setAgents] = useState<any>();
const selectedTags = []; const [pendingVisible, setPendingVisible] = useState(false);
const pendingVisible = false;
const pendingDate = new Date();
const handleDelete = () => {
console.info("You clicked the delete icon.");
};
const filteredStates = ticketStates?.filter( const filteredStates =
(state: any) => !["new", "merged", "removed"].includes(state.name), ticketStates?.filter(
); (state: any) => !["new", "merged", "removed"].includes(state.label),
) ?? [];
useEffect(() => { useEffect(() => {
const fetchAgents = async () => { const fetchAgents = async () => {
const result = await getAgentsAction(); const result = await getAgentsAction();
console.log({ agents: result });
setAgents(result); setAgents(result);
}; };
const fetchGroups = async () => { const fetchGroups = async () => {
const result = await getGroupsAction(); const result = await getGroupsAction();
console.log({ groups: result });
setGroups(result); setGroups(result);
}; };
const fetchTicketStates = async () => { const fetchTicketStates = async () => {
const result = await getTicketStatesAction(); const result = await getTicketStatesAction();
console.log({ ticketStates: result });
setTicketStates(result); setTicketStates(result);
}; };
const fetchTicketPriorities = async () => { const fetchTicketPriorities = async () => {
const result = await getTicketPrioritiesAction(); const result = await getTicketPrioritiesAction();
console.log({ ticketPriorities: result });
setTicketPriorities(result); setTicketPriorities(result);
}; };
const fetchTicketTags = async () => {
const result = await getTicketTagsAction();
console.log({ tags: result });
setTags(result);
};
fetchTicketStates(); fetchTicketStates();
fetchTicketPriorities(); fetchTicketPriorities();
fetchTicketTags();
fetchAgents(); fetchAgents();
fetchGroups(); fetchGroups();
}, []); }, []);
@ -80,76 +72,46 @@ export const TicketEdit: FC<TicketEditProps> = ({ id }) => {
useEffect(() => { useEffect(() => {
const fetchTicket = async () => { const fetchTicket = async () => {
const result = await getTicketAction(id); const result = await getTicketAction(id);
console.log({ result });
setTicket(result); setTicket(result);
setFormState({
values: {
...formState.values,
group: result?.group?.id,
owner: result?.owner?.id,
priority: result?.priority?.id,
state: result?.state?.id,
tags: result?.tags,
},
});
}; };
fetchTicket(); fetchTicket();
}, []); }, []);
/* const updateFormState = (name: string, value: any) => {
useEffect(() => { setFormState({
values: {
...formState.values,
[name]: value,
},
});
const stateName = filteredStates?.find( const stateName = filteredStates?.find(
(state: any) => state.id === selectedState, (state: any) => state.id === formState.values.state,
)?.name; )?.name;
setPendingVisible(stateName?.includes("pending") ?? false); setPendingVisible(stateName?.includes("pending") ?? false);
}, [selectedState]); setHasChanges(true);
*/
const updateTicket = async (input: any) => {
/*
console.log({ input });
const res = await fetcher({
document: updateTicketMutation,
variables: {
ticketId: `gid://zammad/Ticket/${id}`,
input,
},
});
console.log({ res });
*/
}; };
const updateTags = async (tags: string[]) => {
/* const updateTicket = async () => {
console.log({ tags }); await updateTicketAction(id, formState.values);
const res = await fetcher({ setHasChanges(false);
document: updateTagsMutation,
variables: {
objectId: `gid://zammad/Ticket/${id}`,
tags,
},
});
console.log({ res });
*/
}; };
const initialState = {
messages: [],
errors: [],
values: {
customer: "",
group: ticket?.group?.id?.split("/").pop(),
owner: ticket?.owner?.id?.split("/").pop(),
priority: ticket?.priority?.id?.split("/").pop(),
state: ticket?.state?.id?.split("/").pop(),
tags: [],
title: "",
article: {
body: "",
type: "note",
internal: true,
},
},
};
const [formState, formAction] = useFormState(
updateTicketAction,
initialState,
);
const shouldRender = !!ticket; const shouldRender = !!ticket;
return ( return (
shouldRender && (
<Box sx={{ height: "100vh", background: "#ddd", p: 2 }}> <Box sx={{ height: "100vh", background: "#ddd", p: 2 }}>
<form action={formAction}> {shouldRender && (
<Grid container direction="column" spacing={3}> <Grid container direction="column" spacing={3}>
<Grid item> <Grid item>
<Box sx={{ m: 1 }}>Group</Box> <Box sx={{ m: 1 }}>Group</Box>
@ -157,6 +119,7 @@ export const TicketEdit: FC<TicketEditProps> = ({ id }) => {
name="group" name="group"
label="Group" label="Group"
formState={formState} formState={formState}
updateFormState={updateFormState}
getOptions={() => groups} getOptions={() => groups}
/> />
</Grid> </Grid>
@ -166,6 +129,7 @@ export const TicketEdit: FC<TicketEditProps> = ({ id }) => {
name="owner" name="owner"
label="Owner" label="Owner"
formState={formState} formState={formState}
updateFormState={updateFormState}
getOptions={() => agents} getOptions={() => agents}
/> />
</Grid> </Grid>
@ -175,22 +139,8 @@ export const TicketEdit: FC<TicketEditProps> = ({ id }) => {
name="state" name="state"
label="State" label="State"
formState={formState} formState={formState}
getOptions={() => updateFormState={updateFormState}
filteredStates?.map((state: any) => ({ getOptions={() => filteredStates}
value: state.id,
label: state.name,
})) ?? []
}
/*
onChange={(e: any) => {
const newState = e.target.value;
setSelectedState(newState);
updateTicket({
stateId: `gid://zammad/Ticket::State/${newState}`,
pendingTime: pendingDate.toISOString(),
});
}}
*/
/> />
</Grid> </Grid>
<Grid <Grid
@ -198,24 +148,18 @@ export const TicketEdit: FC<TicketEditProps> = ({ id }) => {
xs={12} xs={12}
sx={{ display: pendingVisible ? "inherit" : "none" }} sx={{ display: pendingVisible ? "inherit" : "none" }}
> >
{/* <DatePicker <DatePicker
label="Pending Date" label="Pending Date"
value={pendingDate} value={new Date(formState.values.pendingTime)}
onChange={(newValue: any) => { onChange={(newValue: any) => {
updateFormState("pendingDate", newValue.toISOString());
console.log(newValue);
setPendingDate(newValue);
updateTicket({
pendingTime: newValue.toISOString(),
})
}} }}
slotProps={{ textField: { size: "small" } }} slotProps={{ textField: { size: "small" } }}
sx={{ sx={{
width: "100%", width: "100%",
backgroundColor: "white", backgroundColor: "white",
}} }}
/> */} />
</Grid> </Grid>
<Grid item> <Grid item>
<Box sx={{ m: 1, mt: 0 }}>Priority</Box> <Box sx={{ m: 1, mt: 0 }}>Priority</Box>
@ -223,42 +167,39 @@ export const TicketEdit: FC<TicketEditProps> = ({ id }) => {
name="priority" name="priority"
label="Priority" label="Priority"
formState={formState} formState={formState}
updateFormState={updateFormState}
getOptions={() => ticketPriorities} getOptions={() => ticketPriorities}
/*
onChange={(e: any) => {
const newPriority = e.target.value;
setSelectedPriority(newPriority);
updateTicket({
priorityId: `gid://zammad/Ticket::Priority/${newPriority}`,
});
}}
*/
/> />
</Grid> </Grid>
<Grid item> <Grid item>
<Box sx={{ mb: 1 }}>Tags</Box> <Box sx={{ mb: 1 }}>Tags</Box>
<MuiChipsInput <MuiChipsInput
sx={{ backgroundColor: "white", width: "100%" }} sx={{ backgroundColor: "white", width: "100%" }}
value={selectedTags} value={formState.values.tags}
hideClearAll
onChange={(tags: any) => { onChange={(tags: any) => {
/* updateFormState("tags", tags);
setSelectedTags(tags); }}
updateTags(tags); onDeleteChip={(tag: any) => {
*/ const tags = formState.values.tags.filter(
(t: any) => t !== tag,
);
updateFormState("tags", tags);
}} }}
/> />
</Grid> </Grid>
<Grid item container direction="row-reverse"> <Grid item container direction="row-reverse">
<Grid item> <Grid item>
<Button text="Save" kind="primary" type="submit" /> <Button
text="Save"
kind="primary"
onClick={updateTicket}
disabled={!hasChanges}
/>
</Grid> </Grid>
</Grid> </Grid>
</Grid> </Grid>
</form> )}
</Box> </Box>
)
); );
}; };

View file

@ -76,35 +76,59 @@ export const createTicketArticleAction = async (
}; };
export const updateTicketAction = async ( export const updateTicketAction = async (
currentState: any, ticketID: string,
formData: FormData, ticketInfo: Record<string, any>,
) => { ) => {
/* console.log({ ticketID, ticketInfo });
try { try {
const { id, project } = currentState.values; const input = {};
const updatedTicket = { if (ticketInfo.state) {
title: formData.get("title"), input["stateId"] = ticketInfo.state;
}; }
await executeMutation({ if (ticketInfo.pendingTime) {
project, input["pendingTime"] = ticketInfo.pendingTime;
mutation: UpdateAssetMutation, }
if (ticketInfo.priority) {
input["priorityId"] = ticketInfo.priority;
}
if (ticketInfo.group) {
input["groupId"] = ticketInfo.group;
}
if (ticketInfo.owner) {
input["ownerId"] = ticketInfo.owner;
}
const result = await executeGraphQL({
query: updateTicketMutation,
variables: { variables: {
id, ticketId: `gid://zammad/Ticket/${ticketID}`,
input: updatedAsset, input,
}, },
}); });
revalidatePath(`/${project}/assets/${id}`); if (ticketInfo.tags?.length > 0) {
const tagsResult = await executeGraphQL({
query: updateTagsMutation,
variables: {
objectId: `gid://zammad/Ticket/${ticketID}`,
tags: ticketInfo.tags,
},
});
console.log({ tagsResult });
}
console.log({ result });
return { return {
...currentState, result,
values: { ...currentState.values, ...updatedAsset, id, project },
success: true, success: true,
}; };
} catch (e: any) { } catch (e: any) {
return { success: false, message: e?.message ?? "Unknown error" }; console.log({ e });
return {
success: false,
message: e?.message ?? "Unknown error",
};
} }
*/
}; };
export const getTicketAction = async (id: string) => { export const getTicketAction = async (id: string) => {
@ -112,7 +136,7 @@ export const getTicketAction = async (id: string) => {
query: getTicketQuery, query: getTicketQuery,
variables: { ticketId: `gid://zammad/Ticket/${id}` }, variables: { ticketId: `gid://zammad/Ticket/${id}` },
}); });
console.log({ td: ticketData.ticket });
return ticketData?.ticket; return ticketData?.ticket;
}; };
@ -125,38 +149,6 @@ export const getTicketArticlesAction = async (id: string) => {
return ticketData?.ticketArticles; return ticketData?.ticketArticles;
}; };
export const updateTicketTagsAction = async (
currentState: any,
formData: FormData,
) => {
/*
try {
const { id, project } = currentState.values;
const updatedTicket = {
title: formData.get("title"),
};
await executeMutation({
project,
mutation: UpdateAssetMutation,
variables: {
id,
input: updatedAsset,
},
});
revalidatePath(`/${project}/assets/${id}`);
return {
...currentState,
values: { ...currentState.values, ...updatedAsset, id, project },
success: true,
};
} catch (e: any) {
return { success: false, message: e?.message ?? "Unknown error" };
}
*/
};
export const getTicketStatesAction = async () => { export const getTicketStatesAction = async () => {
const states = await executeREST({ const states = await executeREST({
path: "/api/v1/ticket_states", path: "/api/v1/ticket_states",
@ -164,15 +156,15 @@ export const getTicketStatesAction = async () => {
const formattedStates = const formattedStates =
states?.map((state: any) => ({ states?.map((state: any) => ({
value: state.id, value: `gid://zammad/Ticket::State/${state.id}`,
label: state.name, label: state.name,
})) ?? []; })) ?? [];
return formattedStates; return formattedStates;
}; };
export const getTicketTagsAction = async () => { export const getTagsAction = async () => {
const tags = await executeREST({ const { tags } = await executeREST({
path: "/api/v1/tags", path: "/api/v1/tags",
}); });
@ -186,7 +178,7 @@ export const getTicketPrioritiesAction = async () => {
const formattedPriorities = const formattedPriorities =
priorities?.map((priority: any) => ({ priorities?.map((priority: any) => ({
value: priority.id, value: `gid://zammad/Ticket::Priority/${priority.id}`,
label: priority.name, label: priority.name,
})) ?? []; })) ?? [];

View file

@ -12,7 +12,6 @@ export const CSRFProvider: FC<PropsWithChildren> = ({ children }) => {
const response = await fetch("/api/v1/users/me"); const response = await fetch("/api/v1/users/me");
const token = response.headers.get("CSRF-Token"); const token = response.headers.get("CSRF-Token");
update({ csrfToken: token }); update({ csrfToken: token });
console.log({ token });
} }
}, 30000); }, 30000);

View file

@ -6,6 +6,7 @@ const nextConfig = {
"@link-stack/ui", "@link-stack/ui",
"@link-stack/bridge-common", "@link-stack/bridge-common",
"@link-stack/bridge-ui", "@link-stack/bridge-ui",
"mui-chips-input"
], ],
publicRuntimeConfig: { publicRuntimeConfig: {
linkURL: process.env.LINK_URL ?? "http://localhost:3000", linkURL: process.env.LINK_URL ?? "http://localhost:3000",

View file

@ -5,6 +5,7 @@ import { colors } from "../styles/theme";
interface InternalButtonProps { interface InternalButtonProps {
text: string; text: string;
disabled: boolean;
color?: string; color?: string;
kind?: "primary" | "secondary" | "destructive"; kind?: "primary" | "secondary" | "destructive";
type?: string; type?: string;
@ -27,12 +28,14 @@ export const InternalButton: FC<InternalButtonProps> = ({
text, text,
color, color,
kind, kind,
disabled,
type = "button", type = "button",
onClick, onClick,
}) => ( }) => (
<MUIButton <MUIButton
variant="contained" variant="contained"
disableElevation disableElevation
disabled={disabled}
onClick={onClick} onClick={onClick}
type={type} type={type}
href="" href=""
@ -56,6 +59,7 @@ export const InternalButton: FC<InternalButtonProps> = ({
interface ButtonProps { interface ButtonProps {
text: string; text: string;
disabled?: boolean;
color?: string; color?: string;
kind?: "primary" | "secondary" | "destructive"; kind?: "primary" | "secondary" | "destructive";
type?: string; type?: string;
@ -65,6 +69,7 @@ interface ButtonProps {
export const Button: FC<ButtonProps> = ({ export const Button: FC<ButtonProps> = ({
text, text,
disabled = false,
color, color,
type, type,
kind, kind,
@ -75,6 +80,7 @@ export const Button: FC<ButtonProps> = ({
<Link href={href} passHref> <Link href={href} passHref>
<InternalButton <InternalButton
text={text} text={text}
disabled={disabled}
color={color} color={color}
kind={kind} kind={kind}
type={type} type={type}
@ -84,6 +90,7 @@ export const Button: FC<ButtonProps> = ({
) : ( ) : (
<InternalButton <InternalButton
text={text} text={text}
disabled={disabled}
color={color} color={color}
kind={kind} kind={kind}
type={type} type={type}

View file

@ -16,6 +16,7 @@ type TextFieldProps = {
required?: boolean; required?: boolean;
lines?: number; lines?: number;
helperText?: string; helperText?: string;
updateFormState?: (field: string, value: any) => void;
}; };
export const TextField: FC<TextFieldProps> = ({ export const TextField: FC<TextFieldProps> = ({
@ -27,6 +28,7 @@ export const TextField: FC<TextFieldProps> = ({
required = false, required = false,
lines = 1, lines = 1,
helperText, helperText,
updateFormState,
}) => { }) => {
const { darkMediumGray, white } = colors; const { darkMediumGray, white } = colors;
const { roboto } = fonts; const { roboto } = fonts;
@ -42,6 +44,7 @@ export const TextField: FC<TextFieldProps> = ({
rows={lines} rows={lines}
required={required} required={required}
defaultValue={formState.values[name]} defaultValue={formState.values[name]}
onChange={(e: any) => updateFormState?.(name, e.target.value)}
error={Boolean(formState.errors[name])} error={Boolean(formState.errors[name])}
helperText={formState.errors[name] ?? helperText} helperText={formState.errors[name] ?? helperText}
InputProps={{ InputProps={{