|
@@ -1,7 +1,7 @@
|
|
import { createApi } from '@reduxjs/toolkit/query/react'
|
|
import { createApi } from '@reduxjs/toolkit/query/react'
|
|
import { graphqlRequestBaseQuery } from "@rtk-query/graphql-request-base-query"
|
|
import { graphqlRequestBaseQuery } from "@rtk-query/graphql-request-base-query"
|
|
import { gql } from "graphql-request";
|
|
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 });
|
|
const getCategorySearchParams = (query, queryExt) => ({ searchStr: query, searchFieldNames: ["name"], queryExt });
|
|
export const prepareHeaders = (headers, { getState }) => {
|
|
export const prepareHeaders = (headers, { getState }) => {
|
|
@@ -12,6 +12,7 @@ export const prepareHeaders = (headers, { getState }) => {
|
|
return headers;
|
|
return headers;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+let placeHolder = '|--|';
|
|
export const categoryApi = createApi({
|
|
export const categoryApi = createApi({
|
|
reducerPath: 'category',
|
|
reducerPath: 'category',
|
|
baseQuery: graphqlRequestBaseQuery({
|
|
baseQuery: graphqlRequestBaseQuery({
|
|
@@ -21,19 +22,33 @@ export const categoryApi = createApi({
|
|
tagTypes: ['Category', 'CategoryCount'],
|
|
tagTypes: ['Category', 'CategoryCount'],
|
|
endpoints: (builder) => ({
|
|
endpoints: (builder) => ({
|
|
getRootCategories: builder.query({
|
|
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) => {
|
|
providesTags: (result) => {
|
|
return result
|
|
return result
|
|
? [...result.CategoryFind.map(obj => ({ type: 'Category', _id: obj._id })), 'Category']
|
|
? [...result.CategoryFind.map(obj => ({ type: 'Category', _id: obj._id })), 'Category']
|
|
: ['Category'];
|
|
: ['Category'];
|
|
- }
|
|
|
|
|
|
+ },
|
|
|
|
+ transformResponse: (response) => {
|
|
|
|
+ return response;
|
|
|
|
+ },
|
|
|
|
+ transformErrorResponse: (response, meta) => {
|
|
|
|
+ return response;
|
|
|
|
+ },
|
|
}),
|
|
}),
|
|
getCategories: builder.query({
|
|
getCategories: builder.query({
|
|
query: ({ withOwner = false, withChildren = false, withParent = false, queryExt = {}, fromPage, pageSize, searchStr = '' }) => {
|
|
query: ({ withOwner = false, withChildren = false, withParent = false, queryExt = {}, fromPage, pageSize, searchStr = '' }) => {
|