import axios from 'axios';
axios.defaults.baseURL = process.env.REACT_APP_API_URL;

export const getCategories = async () => {
    const {request, data} = await axios.get(
        '/categories'
    );

    if (request.status === 200) {
        return data;
    }
    return [];
}

export const deleteCategory = async (bearer, id) => {
    const {request} = await axios.delete(
        `/categories/${id}`,
        {
            headers: {
                "Authorization": `Bearer ${bearer}`
            },
        }
    )
    return request.status === 200;
}

export const updateCategory = async (bearer, category) => {
    const {request} = await axios.put(
        `categories/${category.id}`,
        {
            ...category
        },
        {
            headers: {
                "Authorization": `Bearer ${bearer}`
            }
        }
    )
    return request.status === 200;
}

export const createCategory = async (bearer, category) => {
    const {request, data} = await axios.post(
        '/categories',
        {
            ...category
        },
        {
            headers: {
                "Authorization": `Bearer ${bearer}`
            }
        }
    )
    if (request.status === 200) {
        return data;
    }
    return undefined;
}
