goodsReducer.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { createApi } from '@reduxjs/toolkit/query/react'
  2. import { graphqlRequestBaseQuery } from "@rtk-query/graphql-request-base-query"
  3. import { gql } from "graphql-request";
  4. import { createFullQuery } from '../gql';
  5. //import { prepareHeaders } from "./index";
  6. export const prepareHeaders = (headers, { getState }) => {
  7. const token = getState().auth.token;
  8. if (token) {
  9. headers.set("Authorization", `Bearer ${token}`);
  10. }
  11. return headers;
  12. }
  13. const getGoodsSearchParams = (searchStr, queryExt) => (
  14. {
  15. searchStr: searchStr,
  16. searchFieldNames: ["name", "description"],
  17. queryExt
  18. });
  19. export const goodsApi = createApi({
  20. reducerPath: 'goods',
  21. baseQuery: graphqlRequestBaseQuery({
  22. url: '/graphql',
  23. prepareHeaders
  24. }),
  25. tagTypes: ['Good', 'GoodCount'],
  26. endpoints: (builder) => ({
  27. getGoods: builder.query({
  28. query: ({ fromPage, pageSize, searchStr = '', queryExt = {} }) => {
  29. let params = createFullQuery(
  30. getGoodsSearchParams(searchStr, queryExt),
  31. { fromPage, pageSize });
  32. return {
  33. document: gql`
  34. query GoodFind($q: String) {
  35. GoodFind(query: $q) {
  36. _id name price description
  37. images { _id url }
  38. }
  39. }
  40. `,
  41. variables: params
  42. }
  43. },
  44. providesTags: (result, error, arg) => {
  45. return result
  46. ? [...result.GoodFind.map(obj => ({ type: 'Good', _id: obj._id })), 'Good']
  47. : ['Good'];
  48. }
  49. }),
  50. getGoodsCount: builder.query({
  51. query: ({ searchStr = '', queryExt = {} }) => {
  52. let params = createFullQuery(
  53. getGoodsSearchParams(searchStr, queryExt));
  54. return {
  55. document: gql`
  56. query GoodsCount($q: String) { GoodCount(query: $q) }
  57. `,
  58. variables: params
  59. }
  60. },
  61. providesTags: ['GoodCount'],
  62. }),
  63. getGoodById: builder.query({
  64. query: (_id) => {
  65. let params = createFullQuery({ searchStr: _id, searchFieldNames: ["_id"] });
  66. return {
  67. document: gql`
  68. query GoodFindOne($q: String) {
  69. GoodFindOne(query: $q) {
  70. _id name price description categories { _id name }
  71. images { _id url }
  72. }
  73. }
  74. `,
  75. variables: params
  76. }
  77. },
  78. providesTags: (result, error, arg) => {
  79. return result
  80. ? [{ type: 'Good', _id: result.GoodFindOne?._id }, 'Good']
  81. : ['Good'];
  82. }
  83. }),
  84. getGoodsById: builder.query({
  85. query: ({ goods }) => {
  86. let params = createFullQuery({queryExt: { _id: { "$in": goods.map(g => g._id) } } })
  87. return {
  88. document: gql`
  89. query GoodFind($q: String) {
  90. GoodFind(query: $q) {
  91. _id name price description
  92. images { url }
  93. }
  94. }
  95. `,
  96. variables: params
  97. }
  98. },
  99. providesTags: (result, error, arg) => {
  100. return result
  101. ? [...result.GoodFind.map(obj => ({ type: 'Good', _id: obj._id })), 'Good']
  102. : ['Good'];
  103. }
  104. }),
  105. saveGood: builder.mutation({
  106. query: ({ good }) => {
  107. return (
  108. {
  109. document: gql`
  110. mutation GoodUpsert($good: GoodInput) {
  111. GoodUpsert(good: $good) {
  112. _id
  113. }
  114. }
  115. `,
  116. variables: { good: { ...good, images: good?.images.map(img => ({ _id: img._id })) ?? [] } }
  117. }
  118. )
  119. },
  120. invalidatesTags: (result, error, arg) => {
  121. if (!error) {
  122. let goodInv = { type: 'Good', _id: arg.good._id };
  123. return [goodInv, 'GoodCount'];
  124. }
  125. },
  126. }),
  127. }),
  128. })
  129. export const { useGetGoodsQuery, useGetGoodsCountQuery, useGetGoodByIdQuery, useGetGoodsByIdQuery, useSaveGoodMutation } = goodsApi;