|
@@ -1,7 +1,7 @@
|
|
|
import { createApi } from '@reduxjs/toolkit/query/react'
|
|
|
import { graphqlRequestBaseQuery } from "@rtk-query/graphql-request-base-query"
|
|
|
import { gql } from "graphql-request";
|
|
|
-import { createFullQuery, getFullBackendUrl } from '../utills';
|
|
|
+import { createFullQuery, getFullBackendUrl, repeatQuery } from '../utills';
|
|
|
|
|
|
const getCategorySearchParams = (query, queryExt) => ({ searchStr: query, searchFieldNames: ["name"], queryExt });
|
|
|
export const prepareHeaders = (headers, { getState }) => {
|
|
@@ -12,6 +12,7 @@ export const prepareHeaders = (headers, { getState }) => {
|
|
|
return headers;
|
|
|
}
|
|
|
|
|
|
+let placeHolder = '|--|';
|
|
|
export const categoryApi = createApi({
|
|
|
reducerPath: 'category',
|
|
|
baseQuery: graphqlRequestBaseQuery({
|
|
@@ -21,19 +22,33 @@ export const categoryApi = createApi({
|
|
|
tagTypes: ['Category', 'CategoryCount'],
|
|
|
endpoints: (builder) => ({
|
|
|
getRootCategories: builder.query({
|
|
|
- query: (withChildren = false) => ({
|
|
|
- document: gql`
|
|
|
- query GetCategories{
|
|
|
- CategoryFind(query: "[{\\"parent\\": null}]") {
|
|
|
- _id name ${withChildren ? 'subCategories { _id name } ' : ''} image { _id url }
|
|
|
+ query: (childrenDepth = 0) => {
|
|
|
+ let params = createFullQuery({ queryExt: {parent: null } }, { sort: { name: 1 } });
|
|
|
+ return (
|
|
|
+ {
|
|
|
+ document: gql`
|
|
|
+ query GetCategories($q: String){
|
|
|
+ CategoryFind(query: $q) {
|
|
|
+ _id name image { _id url }
|
|
|
+ ${repeatQuery(childrenDepth, ` subCategories { _id name image { _id url } ${placeHolder} } `, placeHolder)}
|
|
|
+ }
|
|
|
}
|
|
|
+ `,
|
|
|
+ variables: params
|
|
|
}
|
|
|
- `}),
|
|
|
+ )
|
|
|
+ },
|
|
|
providesTags: (result) => {
|
|
|
return result
|
|
|
? [...result.CategoryFind.map(obj => ({ type: 'Category', _id: obj._id })), 'Category']
|
|
|
: ['Category'];
|
|
|
- }
|
|
|
+ },
|
|
|
+ transformResponse: (response) => {
|
|
|
+ return response;
|
|
|
+ },
|
|
|
+ transformErrorResponse: (response, meta) => {
|
|
|
+ return response;
|
|
|
+ },
|
|
|
}),
|
|
|
getCategories: builder.query({
|
|
|
query: ({ withOwner = false, withChildren = false, withParent = false, queryExt = {}, fromPage, pageSize, searchStr = '' }) => {
|