import getXSRFToken from "../getXSRF.js";

export default async function updateUser(name, lastname, phone) {

    const csrfToken = await getXSRFToken();

    try {
        const res = await fetch(`http://${import.meta.env.VITE_API_URL}/api/users/update`, {
            method: 'POST',
            credentials: 'include',
            headers: {
                'Accept': 'application/json',
                'X-XSRF-TOKEN': csrfToken,
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                name: name,
                phone: phone,
                lastname: lastname,
            })
        });

        if (!res.ok) {
            throw new Error(`Erreur serveur : ${res.status}`);
        }

        return await res.json();
    } catch (err) {
        console.error('Erreur :', err);
        return null;
    }
}