ActionCategory.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import {actionPromise} from "../reducers/PromiseReducer";
  2. import {gql} from "./PathDB";
  3. import {actionCategoryChange, actionCategoryCreate} from "../reducers/CategoryReducer";
  4. //CategoryFind -- parent: null
  5. export const actionRootCats = () => {
  6. return actionPromise('rootCats', gql(`query rootCats{
  7. CategoryFind(query: "[{\\"parent\\": null}]"){
  8. _id
  9. name
  10. subCategories{
  11. _id,
  12. name,
  13. subCategories{
  14. _id,
  15. name
  16. }
  17. }
  18. }
  19. }`)
  20. )
  21. }
  22. export const actionFullRootCats = () =>
  23. async dispatch => {
  24. let value = await dispatch(actionRootCats())
  25. if (value){
  26. dispatch(actionCategoryCreate(value))
  27. }
  28. }
  29. //CategoryFindOne
  30. const actionCatById = (_id) => {
  31. return actionPromise('catById', gql(`query catById($q: String){
  32. CategoryFindOne(query: $q){
  33. _id goods {
  34. _id createdAt name description price images {
  35. url
  36. }
  37. }
  38. }
  39. }`, {q: JSON.stringify([{_id}])}))
  40. }
  41. export const actionFullCatById = (_id) =>
  42. async dispatch => {
  43. let value = await dispatch(actionCatById(_id))
  44. if (value){
  45. dispatch(actionCategoryChange(value))
  46. }
  47. }
  48. //CategoryCount
  49. export const actionCategoryCount = () => {
  50. return actionPromise('categoryCount', gql(`query categoryCount{
  51. CategoryCount(query: "[{}]")
  52. }`
  53. )
  54. )
  55. }
  56. //CategoryUpsert
  57. export const actionCategoryUpsert = (category) => {
  58. const mainTitleCategory = category[0]?.name || 'No name';
  59. const mainGoodsCategory = []
  60. if (category[0]?.goods && category[0]?.goods?.length > 0){
  61. category[0].goods.forEach(item => {
  62. mainGoodsCategory.push(...item)
  63. })
  64. }
  65. console.log(mainTitleCategory, mainGoodsCategory)
  66. return actionPromise('categoryUpsert', gql(`
  67. mutation categoryUpsert($name: String!){
  68. CategoryUpsert(category: {name: $name}) {
  69. _id
  70. createdAt
  71. name
  72. }
  73. }`,{name: mainTitleCategory}
  74. )
  75. )
  76. }
  77. //CategoryFind
  78. export const actionAllCategory = () => {
  79. return actionPromise('allCategory', gql(`query allCategory{
  80. CategoryFind(query: "[{}]"){
  81. _id
  82. name
  83. subCategories{
  84. _id,
  85. name,
  86. subCategories{
  87. _id,
  88. name
  89. }
  90. }
  91. }
  92. }`)
  93. )
  94. }
  95. export const actionFullAllCategory = () =>
  96. async dispatch => {
  97. let value = await dispatch(actionAllCategory())
  98. if (value){
  99. dispatch(actionCategoryCreate(value))
  100. }
  101. }