ActionCategory.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // }