import useUsersList from "../../hooks/useUsersList.tsx";
import UsersTable from "../../components/UsersTable/UsersTable.tsx";
import {UserSchema, UserService} from "../../../../client";
import {notifications} from "../../../../shared/lib/notifications.ts";

const UsersTab = () => {
    const {objects: users, refetch} = useUsersList();

    const onChange = (user: UserSchema) => {
        UserService.updateUser({
            requestBody: {
                data: {
                    ...user,
                    positionKey: user.position?.key,
                }
            }
        }).then(async ({ok, message}) => {
            notifications.guess(ok, {message});
            if (!ok) return;
            await refetch();
        })
    }
    const onDelete = async (user: UserSchema) => {
        onChange({...user, isDeleted: true});
    }
    return (
        <UsersTable
            items={users}
            onChange={onChange}
            onDelete={onDelete}
        />
    )
}

export default UsersTab;