Skip to content
Snippets Groups Projects
publications.thunks.ts 830 B
Newer Older
import { createAsyncThunk } from '@reduxjs/toolkit';
import { validateDataUsingZodSchema } from '@/utils/validateDataUsingZodSchema';
import { axiosInstance } from '@/services/api/utils/axiosInstance';
import { PublicationsResponse } from '@/types/models';
import { publicationsResponseSchema } from '@/models/publicationsResponseSchema';
import { GetPublicationsParams, apiPath } from '../apiPath';

export const getPublications = createAsyncThunk(
  'publications/getPublications',
  async (params: GetPublicationsParams): Promise<PublicationsResponse | undefined> => {
    const response = await axiosInstance.get<PublicationsResponse>(apiPath.getPublications(params));

    const isDataValid = validateDataUsingZodSchema(response.data, publicationsResponseSchema);

    return isDataValid ? response.data : undefined;
  },
);