actionsForChats.jsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { actionPromise } from "./actionsPromise";
  2. import { gql } from "../helpers/gql";
  3. import { actionSetChatAvatar } from "./actionsMedia";
  4. import { actionAboutMe } from "./actionAboutMe";
  5. import { createBrowserHistory } from 'history'
  6. import { history } from "../App";
  7. export const actionAddChats = (data) => ({type: 'CHATS', data})
  8. export const actionAddChat = (chat) => ({type: 'CHATS', data: [chat]})
  9. export const actionLeftChat = (data) => {
  10. const [,route, histId] = history.location.pathname.split('/');
  11. if(histId === data._id){
  12. history.push('/')
  13. }
  14. return ({type: 'LEFTCHAT', data})
  15. }
  16. export const actionGetAllChats = (userId) => (
  17. actionPromise('getAllChats', gql(`query getAll($q: String){
  18. ChatFind (query: $q){
  19. _id
  20. title
  21. avatar {
  22. _id
  23. url
  24. }
  25. members {
  26. _id
  27. login
  28. avatar {
  29. _id
  30. url
  31. }
  32. }
  33. lastModified
  34. }
  35. }`, {
  36. q: JSON.stringify([ { 'members._id': userId } ])
  37. }
  38. ))
  39. )
  40. export const actionUpdateChat = (chat) =>
  41. actionPromise('newChat', gql(`mutation createChat($chat: ChatInput) {
  42. ChatUpsert(chat: $chat) {
  43. _id
  44. title
  45. members {
  46. login
  47. _id
  48. }
  49. }
  50. }`, {chat: chat}))
  51. export const actionUpsertChat = (id, title, members, avatar) =>
  52. async (dispatch, getState) => {
  53. const chat = Object.fromEntries(Object.entries({_id: id, title, members}).filter(([_, v]) => v != null));
  54. const {_id} = await dispatch(actionUpdateChat(chat));
  55. console.log(_id)
  56. if(_id && avatar){
  57. dispatch(actionSetChatAvatar(avatar));
  58. }
  59. }
  60. export const actionGetOneChat = (_id) =>
  61. async dispatch => {
  62. let chat = await dispatch(
  63. actionPromise('oneChat', gql(`query findChatById($chatId: String) {
  64. ChatFindOne(query: $chatId) {
  65. _id
  66. title
  67. members{
  68. _id
  69. nick
  70. avatar{
  71. url
  72. }
  73. }
  74. owner{
  75. _id
  76. }
  77. avatar{
  78. _id
  79. url
  80. }
  81. lastModified
  82. lastMessage {
  83. text
  84. createdAt
  85. owner{
  86. nick
  87. _id
  88. }
  89. }
  90. }
  91. }`, {chatId: JSON.stringify([{_id}])}))
  92. )
  93. if(chat){
  94. dispatch(actionAddChat(chat))
  95. }
  96. }
  97. // export const actionUpdateChat = (id, title, members) =>
  98. // async (dispatch, getState) => {
  99. // //remove all null
  100. // let chat = Object.fromEntries(Object.entries({_id: id, title: title, members: members}).filter(([_, v]) => v != null));
  101. // let newChat = await dispatch(actionUpsertChat(chat));
  102. // console.log(newChat);
  103. // }
  104. export const actionRemoveChat = (chatId) =>
  105. async (dispatch, getState) => {
  106. const state = getState();
  107. const curUserId = state.auth?.payload?.sub?.id;
  108. const chats = state.chats;
  109. console.log(chatId in chats);
  110. // console.log(chatId);
  111. }