import PositionsTable from "../../components/PositionsTable/PositionsTable.tsx";
import usePositionsList from "../../hooks/usePositionsList.tsx";
import { PositionSchema, PositionService } from "../../../../client";
import { notifications } from "../../../../shared/lib/notifications.ts";

const RolesAndPositionsTab = () => {
    const { objects: positions, refetch } = usePositionsList();
    const onCreate = (position: PositionSchema) => {
        PositionService.createPosition({
            requestBody: {
                data: position,
            },
        }).then(async ({ ok, message }) => {
            notifications.guess(ok, { message });
            if (!ok) return;
            await refetch();
        });
    };
    const onDelete = (position: PositionSchema) => {
        PositionService.deletePosition({
            requestBody: {
                positionKey: position.key,
            },
        }).then(async ({ ok, message }) => {
            notifications.guess(ok, { message });
            if (!ok) return;
            await refetch();
        });
    };

    return (
        <PositionsTable
            items={positions}
            onCreate={onCreate}
            onDelete={onDelete}
        />
    );
};
export default RolesAndPositionsTab;
