Snai3i-LandingPage-FormBuilder / frontend / src / app / backend / endpoints / forms.ts
forms.ts
Raw
import { TagDescription } from '@reduxjs/toolkit/query';
import api from '..';

const FORMS_API = 'api/forms';

export const formsApi = api.injectEndpoints({
  endpoints: (build) => ({
    create: build.mutation<
      ResponseI<FormElementsType>,
      { name: string; elements: FormElementsType[] }
    >({
      query: (data) => ({
        url: `${FORMS_API}/`,
        method: 'POST',
        body: data,
      }),
      invalidatesTags: ['forms'],
    }),
    update: build.mutation<
      ResponseI<FormElementsType>,
      { formId: string; data: { name: string; elements: FormElementsType[] } }
    >({
      query: (body) => ({
        url: `${FORMS_API}/${body.formId}`,
        method: 'PATCH',
        body: body.data,
      }),
      invalidatesTags: ['forms'],
    }),
    updateIsActive: build.mutation<
      ResponseI<FormElementsType>,
      { formId: string; data: { isActive: boolean } }
    >({
      query: (body) => ({
        url: `${FORMS_API}/${body.formId}`,
        method: 'PATCH',
        body: body.data,
      }),
      invalidatesTags: ['forms'],
    }),
    getForm: build.query<ResponseI<FormType>, string>({
      query: (formId) => ({
        url: `${FORMS_API}/${formId}`,
        method: 'GET',
      }),
      providesTags: (result) =>
        [{ type: 'forms', id: result?.data._id }] as TagDescription<'forms'>[],
    }),
    getFormM: build.mutation<ResponseI<FormType>, string>({
      query: (formId) => ({
        url: `${FORMS_API}/${formId}`,
        method: 'GET',
      }),
      invalidatesTags: (_, formId) =>
        [{ type: 'forms', id: formId }] as TagDescription<'forms'>[],
    }),
    deleteForm: build.mutation<ResponseI<null>, string>({
      query: (formId) => ({
        url: `${FORMS_API}/${formId}`,
        method: 'DELETE',
      }),
      invalidatesTags: (_, formId) =>
        ['forms', { type: 'forms', id: formId }] as TagDescription<'forms'>[],
    }),
    deleteForms: build.mutation<ResponseI<FormType>, string[]>({
      query: (formIds) => ({
        url: `${FORMS_API}/bulk-delete`,
        method: 'PATCH',
        body: { forms: formIds },
      }),
      invalidatesTags: (_, formIds) =>
        [
          'forms',
          ((formIds ?? []) as string[]).map((id) => ({ type: 'forms', id })),
          // formIds.map((id) => ({ type: 'forms', id })),
        ] as TagDescription<'forms'>[],
    }),
    getAllForms: build.query<
      ResponseI<{ forms: FormType[]; total: number }>,
      seachParamsType
    >({
      query: (params) => ({
        url: `${FORMS_API}/`,
        method: 'GET',
        params,
      }),
      providesTags: (result) =>
        [
          'forms',
          ...(result?.data?.forms ?? []).map(({ _id }) => ({
            type: 'forms',
            _id,
          })),
        ] as TagDescription<'forms'>[],
    }),
    createResponse: build.mutation<
      ResponseI<FormResponseType>,
      { formId: string; data: FormResponseI[] }
    >({
      query: (body) => ({
        url: `${FORMS_API}/${body.formId}/response`,
        method: 'POST',
        body: { response: body.data },
      }),
    }),
    getResponses: build.query<
      ResponseI<{ responses: FormResponseType[] }>,
      string
    >({
      query: (formId) => ({
        url: `${FORMS_API}/${formId}/response`,
        method: 'GET',
      }),
    }),
  }),
});

export const {
  useCreateMutation,
  useUpdateMutation,
  useUpdateIsActiveMutation,
  useGetFormQuery,
  useGetFormMMutation,
  useGetAllFormsQuery,
  useDeleteFormMutation,
  useDeleteFormsMutation,
  useCreateResponseMutation,
  useGetResponsesQuery,
} = formsApi;