link-stack/apps/link/app/(main)/overview/[overview]/_components/TicketCreateDialog.tsx

143 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-10-16 09:20:40 +02:00
"use client";
import { FC, useState, useEffect } from "react";
2024-05-09 07:42:44 +02:00
import { useFormState } from "react-dom";
import { useRouter } from "next/navigation";
2024-05-09 07:42:44 +02:00
import { Grid } from "@mui/material";
import {
Dialog,
Button,
TextField,
Autocomplete,
Select,
} from "@link-stack/ui";
2024-05-09 07:42:44 +02:00
import { createTicketAction } from "app/_actions/tickets";
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,
}) => {
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-10-16 09:20:40 +02:00
};
2024-05-09 07:42:44 +02:00
const [formState, formAction] = useFormState(
createTicketAction,
initialState,
);
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>
<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"
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>
);
};