98 lines
2.9 KiB
TypeScript
98 lines
2.9 KiB
TypeScript
import { modals } from "@mantine/modals";
|
||
import {
|
||
ServiceCategorySchema,
|
||
ServiceSchema,
|
||
ServiceService,
|
||
} from "../../../client";
|
||
import { notifications } from "../../../shared/lib/notifications.ts";
|
||
import useServicesList from "./useServicesList.tsx";
|
||
import { Text } from "@mantine/core";
|
||
|
||
const useServicesState = () => {
|
||
const { services, refetch } = useServicesList({ withPlaceholders: true });
|
||
|
||
const onCreateClick = () => {
|
||
modals.openContextModal({
|
||
modal: "createService",
|
||
title: "Создание услуги",
|
||
withCloseButton: false,
|
||
innerProps: {
|
||
onCreate,
|
||
},
|
||
});
|
||
};
|
||
const onCreate = (values: ServiceSchema) => {
|
||
ServiceService.createService({ requestBody: { service: values } }).then(
|
||
async ({ ok, message }) => {
|
||
notifications.guess(ok, { message: message });
|
||
if (!ok) return;
|
||
await refetch();
|
||
},
|
||
);
|
||
};
|
||
|
||
const onCreateCategoryClick = () => {
|
||
modals.openContextModal({
|
||
modal: "createServiceCategory",
|
||
title: "Создание категории",
|
||
withCloseButton: false,
|
||
innerProps: {
|
||
onCreate: onCategoryCreate,
|
||
},
|
||
});
|
||
};
|
||
const onCategoryCreate = (category: ServiceCategorySchema) => {
|
||
ServiceService.createServiceCategory({
|
||
requestBody: { category: category },
|
||
}).then(({ ok, message }) =>
|
||
notifications.guess(ok, { message: message }),
|
||
);
|
||
};
|
||
|
||
const onServiceDelete = (service: ServiceSchema) => {
|
||
modals.openConfirmModal({
|
||
title: "Удаление услуги",
|
||
children: (
|
||
<Text>
|
||
Вы уверены, что хотите удалить услугу "{service.name}"?
|
||
</Text>
|
||
),
|
||
onConfirm: () => {
|
||
ServiceService.deleteService({
|
||
requestBody: { serviceId: service.id },
|
||
}).then(async ({ ok, message }) => {
|
||
notifications.guess(ok, { message: message });
|
||
if (!ok) return;
|
||
await refetch();
|
||
});
|
||
},
|
||
labels: {
|
||
confirm: "Удалить",
|
||
cancel: "Отмена",
|
||
},
|
||
});
|
||
};
|
||
|
||
const onServiceUpdate = (service: ServiceSchema) => {
|
||
ServiceService.updateService({
|
||
requestBody: {
|
||
data: service,
|
||
},
|
||
}).then(async ({ ok, message }) => {
|
||
notifications.guess(ok, { message: message });
|
||
if (!ok) return;
|
||
await refetch();
|
||
});
|
||
};
|
||
|
||
return {
|
||
services,
|
||
onCreateClick,
|
||
onServiceDelete,
|
||
onServiceUpdate,
|
||
onCreateCategoryClick,
|
||
};
|
||
};
|
||
|
||
export default useServicesState;
|