All files / src/store/features/search/api searchApi.ts

0% Statements 0/9
0% Branches 0/2
0% Functions 0/4
0% Lines 0/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                                                                                           
import { SEARCH } from "../../../../constants/index.ts";
import handleOptionType from "../../../../utils/search/handleOptionType.ts";
import { apiSlice } from "../../../app/apiSlice.ts";
 
export const searchApi = apiSlice.injectEndpoints({
  endpoints: (builder) => ({
    search: builder.query<any, { context: string; selected: any[] }>({
      query: ({ context, selected }) => {
        return {
          url: SEARCH,
          method: "POST",
          body: {
            context,
            query: {
              filter: {
                op: "AND",
                filters: selected?.map((el) => ({
                  op: "EQ",
                  lhs: {
                    column_identifier: {
                      name: el.column.name,
                      type: el.column.type,
                    },
                  },
                  rhs: {
                    literal: handleOptionType(el.option),
                  },
                })),
              },
            },
          },
        };
      },
      transformResponse: (response: any, _, args) => {
        return {
          [args.context.toLowerCase()]:
            response?.[args.context.toLowerCase()]?.results ?? [],
          meta: response?.meta,
        };
      },
    }),
  }),
});
 
export const { useSearchQuery } = searchApi;