/**
 * File: blogThunks.ts
 * Purpose: This file contains asynchronous thunk actions for managing blog-related workflows 
 *          such as blog list, and more.
 *          
 * Description:
 * - Utilizes `redux-thunk` to create async actions that handle API requests and responses.
 * - Handles blog API calls by dispatching appropriate Redux actions based on the API response.
 * - Displays error notifications using the `Toast` component in case of API failures.
*/

import apiClient from "@/src/interceptor/apiClient";
import { API_ENDPOINTS } from "@/src/utils/commonVariables";
import { blogActions } from "../../slices/Blog/blog";
import { BlogAPIPayload, BlogListResponse } from "@/redux/types/blogTypes";

/**
 * Asynchronous thunk action for blog list process.
 * @param payload - The data required for blog list.
 * @param callback - Optional callback function that receives API response data.
 * @returns A thunk function that dispatches actions based on API response.
 */
export const getBlogList =
    (payload: BlogAPIPayload, callback?: (data: BlogListResponse) => void) =>
        async (dispatch: any) => {
            try {
                dispatch(blogActions.requestBlogList());
                const response: any = await apiClient.post(API_ENDPOINTS.LIST_BLOG, payload);
                callback && callback(response?.data);
                if (!payload.search) {
                    dispatch(blogActions.responseBlogBannerList(response?.data));
                }
                dispatch(blogActions.successBlogList(response?.data));
            } catch (error: any) {
                dispatch(blogActions.failureBlogList());
            }
        };

export default getBlogList;


