V
V
Vika Marmeladka2022-02-10 14:05:46
redux
Vika Marmeladka, 2022-02-10 14:05:46

How to reduce the amount of Thanks code in reudx-toolkit?

There is a huge amount of funk on the project, because that's how the backing apish is arranged. The question is how do I best group these funky? I thought for a long time, but apart from the try catch block to throw all the funky, no more ideas came up. Below are just them and this is only 1/4 of them ...

export const addProductInCart = createAsyncThunk('bascket/addProductInCart', async (product_id: string) => {
    try {
        const response = await ClientAPI({
            url: 'gpnntc_mp.cart.product.add',
            params: {
                product_id,
            },
        })

        return response.data.result
    } catch (err) {}
})

export const setCartProductTopic = createAsyncThunk<'success', { product_id: string; topic: string }>(
    'bascket/setCartProductTopic',
    async ({ product_id, topic }) => {
        try {
            const response = await ClientAPI({
                method: 'POST',
                url: 'gpnntc_mp.cart.product.topic.set',
                data: { product_id, topic },
            })

            return response.data.result
        } catch (err) {}
    }
)

export const setCartProductComment = createAsyncThunk<'success', { product_id: string; comment: string }>(
    'bascket/setCartProductTopic',
    async ({ product_id, comment }) => {
        try {
            const response = await ClientAPI({
                method: 'POST',
                url: 'gpnntc_mp.cart.product.comment.set',
                data: { product_id, comment },
            })

            return response.data.result
        } catch (err) {}
    }
)

export const setCartProductManager = createAsyncThunk<'success', { productId: string; productManagerId: string }>(
    'bascket/setCartProductManager',
    async ({ productId, productManagerId }) => {
        try {
            const response = await ClientAPI({
                method: 'POST',
                url: 'gpnntc_mp.cart.product.manager.set',
                data: {
                    product_id: productId,
                    product_manager_id: productManagerId,
                },
            })

            return response.data.result
        } catch (err) {}
    }
)

export const addCartProductPart = createAsyncThunk<'success', { partId: string; productId: string }>(
    'bascket/setCartProductPart',
    async ({ partId, productId }) => {
        try {
            const response = await ClientAPI({
                method: 'POST',
                url: 'gpnntc_mp.cart.product.part.add',
                data: {
                    product_id: productId,
                    part_id: partId,
                },
            })

            return response.data.result
        } catch (err) {}
    }
)

export const delCartProductPart = createAsyncThunk<'success', { partId: string | undefined; productId: string }>(
    'bascket/delCartProductPart',
    async ({ partId, productId }) => {
        if (partId === undefined) return false

        try {
            const response = await ClientAPI({
                method: 'POST',
                url: 'gpnntc_mp.cart.product.part.del',
                data: {
                    product_id: productId,
                    part_id: partId,
                },
            })

            return response.data.result
        } catch (err) {}
    }
)

export const getInstanceProducts = createAsyncThunk<InstanceProduct[]>('bascket/getInstanceProducts', async () => {
    try {
        const response = await ClientAPI({
            url: 'gpnntc_mp.cart.product.list',
        })

        return response.data.result
    } catch (err) {}
})

export const getInstanceProductByID = createAsyncThunk<InstanceProduct[], { productId: string }>(
    'bascket/getInstanceProductByID',
    async ({ productId }) => {
        try {
            const response = await ClientAPI({
                url: 'gpnntc_mp.cart.product.item',
                params: { product_id: productId },
            })

            return response.data.result
        } catch (err) {}
    }
)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question