Files
tm_panel/src/components/forms/notifications/useCreateNotificationFormPresenter.ts
T
AmirReza Jamali da82399d07 feat(tables): integrate EmptyListWrapper component for improved empty state handling
- Add EmptyListWrapper to various tables (Admins, Companies, Customers, Feedback, Notification History, Tenders) to provide a consistent empty state message when no data is available.
- Update phone number and status display logic in the Profile page to handle null values gracefully.
- Introduce worktree setup configuration in worktrees.json for easier project setup.
2026-02-10 18:19:49 +03:30

102 lines
2.9 KiB
TypeScript

"use client";
import {
NotificationChannels,
NotificationPriorities,
NotificationTargetGroups,
NotificationTypes,
} from "@/constants/enums";
import {
useCompanyFullList,
useGetAllCustomers,
useGetUsersQuery,
} from "@/hooks/queries";
import { useCreateNotificationQuery } from "@/hooks/queries/useNotificationQueries";
import { TCreateNotificationCredentials } from "@/lib/api/types/NotificationHistory";
import { ILabelValue } from "@/types/shared";
import { getEnumAsArray } from "@/utils/shared";
import { useRouter } from "next/navigation";
import { ChangeEvent, useState } from "react";
import { SubmitHandler, useForm } from "react-hook-form";
const useCreateNotificationFormPresenter = () => {
const [recipient, setRecipient] = useState<ILabelValue[] | null>(null);
const {
register,
formState: { errors },
handleSubmit,
watch,
setValue,
reset,
control,
} = useForm<TCreateNotificationCredentials>();
const router = useRouter();
const { data: companies } = useCompanyFullList();
const { data: users } = useGetUsersQuery();
const { data: customers } = useGetAllCustomers();
const { mutate } = useCreateNotificationQuery(() => {
router.back();
reset();
});
const onSubmit: SubmitHandler<TCreateNotificationCredentials> = (data) => {
const credentials = {
...data,
schedule_at: data.schedule_at || 0,
};
mutate({ credentials });
};
const handleCancel = () => {
reset();
router.back();
};
const onSelectTarget = (event: ChangeEvent<HTMLSelectElement>): void => {
const type = event.target.value as NotificationTargetGroups;
const recipientMaps: Partial<
Record<NotificationTargetGroups, Array<ILabelValue> | null>
> = {
[NotificationTargetGroups.SPECIFIC_COMPANY]:
companies?.data.companies?.map((company) => ({
label: company.name,
value: company.id,
})) ?? null,
[NotificationTargetGroups.SPECIFIC_ROLE]: [
{ label: "Admin", value: "admin" },
{ label: "Analyst", value: "analyst" },
],
[NotificationTargetGroups.SPECIFIC_USERS]:
users?.data.users?.map((user) => ({
label: user.full_name ?? "",
value: user.id,
})) ?? null,
[NotificationTargetGroups.SPECIFIC_CUSTOMER]:
customers?.data.customers?.map((customer) => ({
label: customer.full_name,
value: customer.id,
})) ?? null,
};
setRecipient(recipientMaps[type] ?? null);
};
return {
handleSubmit,
watch,
priorities: getEnumAsArray(NotificationPriorities),
handleCancel,
router,
setValue,
reset,
register,
errors,
onSelectTarget,
onSubmit,
control,
recipient,
targets: getEnumAsArray(NotificationTargetGroups),
types: getEnumAsArray(NotificationTypes),
channels: getEnumAsArray(NotificationChannels),
};
};
export default useCreateNotificationFormPresenter;