2023-10-16 09:20:40 +02:00
|
|
|
"use client";
|
|
|
|
|
|
2024-08-05 23:31:15 +02:00
|
|
|
import { FC, useState, useEffect } from "react";
|
2024-05-09 07:42:44 +02:00
|
|
|
import { useFormState } from "react-dom";
|
2024-08-05 23:31:15 +02:00
|
|
|
import { useRouter } from "next/navigation";
|
2024-05-09 07:42:44 +02:00
|
|
|
import { Grid } from "@mui/material";
|
2024-08-05 23:31:15 +02:00
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
Button,
|
|
|
|
|
TextField,
|
|
|
|
|
Autocomplete,
|
|
|
|
|
Select,
|
|
|
|
|
} from "@link-stack/ui";
|
2024-05-09 07:42:44 +02:00
|
|
|
import { createTicketAction } from "app/_actions/tickets";
|
2024-08-05 23:31:15 +02:00
|
|
|
import { getCustomersAction } from "app/_actions/users";
|
|
|
|
|
import { getGroupsAction } from "app/_actions/groups";
|
2023-10-16 09:20:40 +02:00
|
|
|
|
|
|
|
|
interface TicketCreateDialogProps {
|
|
|
|
|
open: boolean;
|
|
|
|
|
closeDialog: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const TicketCreateDialog: FC<TicketCreateDialogProps> = ({
|
|
|
|
|
open,
|
|
|
|
|
closeDialog,
|
|
|
|
|
}) => {
|
2024-08-05 23:31:15 +02:00
|
|
|
const [customers, setCustomers] = useState([]);
|
|
|
|
|
const [groups, setGroups] = useState([]);
|
2024-05-09 07:42:44 +02:00
|
|
|
const initialState = {
|
|
|
|
|
messages: [],
|
|
|
|
|
errors: [],
|
|
|
|
|
values: {
|
|
|
|
|
customerId: "",
|
|
|
|
|
groupId: "",
|
|
|
|
|
ownerId: "",
|
|
|
|
|
priorityId: "",
|
|
|
|
|
stateId: "",
|
|
|
|
|
tags: [],
|
|
|
|
|
title: "",
|
|
|
|
|
article: {
|
|
|
|
|
body: "",
|
|
|
|
|
type: "note",
|
|
|
|
|
internal: true,
|
|
|
|
|
},
|
2023-11-10 14:17:09 +01:00
|
|
|
},
|
2023-10-16 09:20:40 +02:00
|
|
|
};
|
2024-05-09 07:42:44 +02:00
|
|
|
const [formState, formAction] = useFormState(
|
|
|
|
|
createTicketAction,
|
|
|
|
|
initialState,
|
|
|
|
|
);
|
2024-08-05 23:31:15 +02:00
|
|
|
const [liveFormState, setLiveFormState] = useState(formState);
|
|
|
|
|
const updateFormState = (field: string, value: any) => {
|
|
|
|
|
console.log({ value });
|
|
|
|
|
const newState = { ...liveFormState };
|
|
|
|
|
newState.values[field] = value;
|
|
|
|
|
setLiveFormState(newState);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const fetchUsers = async () => {
|
|
|
|
|
const result = await getCustomersAction();
|
|
|
|
|
setCustomers(result);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fetchUsers();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const fetchGroups = async () => {
|
|
|
|
|
const result = await getGroupsAction();
|
|
|
|
|
console.log({ result });
|
|
|
|
|
setGroups(result);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fetchGroups();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (formState.success) {
|
|
|
|
|
closeDialog();
|
|
|
|
|
}
|
|
|
|
|
}, [formState.success, router]);
|
2023-10-16 09:20:40 +02:00
|
|
|
|
|
|
|
|
return (
|
2024-05-09 07:42:44 +02:00
|
|
|
<Dialog
|
|
|
|
|
title="Create Ticket"
|
|
|
|
|
open={open}
|
|
|
|
|
onClose={closeDialog}
|
|
|
|
|
formAction={formAction}
|
|
|
|
|
buttons={
|
2023-10-16 09:20:40 +02:00
|
|
|
<Grid container justifyContent="space-between">
|
|
|
|
|
<Grid item>
|
|
|
|
|
<Button
|
2024-05-09 07:42:44 +02:00
|
|
|
text="Cancel"
|
|
|
|
|
kind="secondary"
|
2023-10-16 09:20:40 +02:00
|
|
|
onClick={() => {
|
|
|
|
|
closeDialog();
|
|
|
|
|
}}
|
2024-05-09 07:42:44 +02:00
|
|
|
/>
|
2023-10-16 09:20:40 +02:00
|
|
|
</Grid>
|
|
|
|
|
<Grid item>
|
2024-05-09 07:42:44 +02:00
|
|
|
<Button text="Save" type="submit" kind="primary" />
|
2023-10-16 09:20:40 +02:00
|
|
|
</Grid>
|
|
|
|
|
</Grid>
|
2024-05-09 07:42:44 +02:00
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<Grid container direction="column" spacing={3}>
|
|
|
|
|
<Grid item>
|
2024-08-05 23:31:15 +02:00
|
|
|
<Select
|
|
|
|
|
name="groupId"
|
|
|
|
|
label="Group"
|
|
|
|
|
getOptions={() => groups as any}
|
|
|
|
|
formState={liveFormState}
|
|
|
|
|
updateFormState={updateFormState}
|
|
|
|
|
/>
|
|
|
|
|
</Grid>
|
|
|
|
|
<Grid item>
|
|
|
|
|
<Select
|
2024-05-09 07:42:44 +02:00
|
|
|
name="customerId"
|
|
|
|
|
label="Customer"
|
2024-08-05 23:31:15 +02:00
|
|
|
getOptions={() => customers as any}
|
|
|
|
|
formState={liveFormState}
|
|
|
|
|
updateFormState={updateFormState}
|
2024-05-09 07:42:44 +02:00
|
|
|
/>
|
|
|
|
|
</Grid>
|
|
|
|
|
<Grid item>
|
|
|
|
|
<TextField name="title" label="Title" formState={formState} />
|
|
|
|
|
</Grid>
|
|
|
|
|
<Grid item>
|
|
|
|
|
<TextField
|
|
|
|
|
name="details"
|
|
|
|
|
label="Details"
|
|
|
|
|
lines={10}
|
|
|
|
|
formState={formState}
|
|
|
|
|
/>
|
|
|
|
|
</Grid>
|
|
|
|
|
</Grid>
|
2023-10-16 09:20:40 +02:00
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
};
|