All files / src/store/features/playbook/api/options getAssets.ts

0% Statements 0/26
0% Branches 0/2
0% Functions 0/4
0% Lines 0/25

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 47 48 49 50 51 52 53 54                                                                                                           
import { GET_ASSETS } from "../../../../../constants/index.ts";
import { updateCardById } from "../../../../../utils/execution/updateCardById.ts";
import getCurrentTask from "../../../../../utils/playbook/task/getCurrentTask.ts";
import handleAssets from "../../../../../utils/handleAssets.ts";
import { apiSlice } from "../../../../app/apiSlice.ts";
import { setAssets } from "../../playbookSlice.ts";
 
export const getAssetApi = apiSlice.injectEndpoints({
  endpoints: (builder) => ({
    getAssets: builder.query<any, string>({
      query: (id) => {
        const [task] = getCurrentTask(id);
        return {
          url: GET_ASSETS,
          method: "POST",
          body: {
            connector_id: task?.task_connector_sources?.[0]?.id,
            type: task?.ui_requirement.model_type,
          },
        };
      },
      transformResponse: (response: any, _, arg) => {
        const [task] = getCurrentTask(arg);
        const data = response?.assets;
        Iif (data?.length === 0) return [];
        let connector_type = task?.source;
        Iif (connector_type?.includes("_VPC"))
          connector_type = connector_type.replace("_VPC", "");
 
        const assets = handleAssets(data, arg);
        return assets;
      },
      onQueryStarted: async (arg, { dispatch, queryFulfilled }) => {
        try {
          // Wait for the query to complete
          updateCardById("ui_requirement.assetsLoading", true, arg);
          const { data } = await queryFulfilled;
          // Dispatch an action to update the global state
          dispatch(setAssets({ assets: data, id: arg }));
        } catch (error) {
          // Handle any errors
          console.log(error);
        } finally {
          updateCardById("ui_requirement.assetsLoading", false, arg);
        }
      },
    }),
  }),
});
 
export const {
  endpoints: { getAssets },
} = getAssetApi;