actionsForChats.js 3.6 KB

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