ActionCategory.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {actionPromise} from "../reducers/PromiseReducer";
  2. import {gql} from "./PathDB";
  3. import {actionCategoryChange, actionCategoryCreate} from "../reducers/CategoryReducer";
  4. //CategoryFind
  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. //CategoryDelete
  78. // export const actionCategoryDelete = (_id, name) => {
  79. // return actionPromise('categoryDelete', gql(`
  80. // mutation categoryDelete($q: CategoryInput){
  81. // CategoryDelete(category: $q) {
  82. // _id
  83. // createdAt
  84. // name
  85. // }
  86. // }`, {q: {_id: _id, name: name}}
  87. // )
  88. // )
  89. // }
  90. export const actionAllCategory = () => {
  91. return actionPromise('allCategory', gql(`query allCategory{
  92. CategoryFind(query: "[{}]"){
  93. _id
  94. name
  95. subCategories{
  96. _id,
  97. name,
  98. subCategories{
  99. _id,
  100. name
  101. }
  102. }
  103. }
  104. }`)
  105. )
  106. }
  107. export const actionFullAllCategory = () =>
  108. async dispatch => {
  109. let value = await dispatch(actionAllCategory())
  110. if (value){
  111. dispatch(actionCategoryCreate(value))
  112. }
  113. }