actionsMedia.jsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { history } from "../App";
  2. import { gql } from "../helpers/gql";
  3. import { uploadFile } from "../helpers/uploadFile";
  4. import { actionAboutMe } from "./actionAboutMe";
  5. import { actionUpdateChat } from "./actionsForChats";
  6. import { actionPromise } from "./actionsPromise";
  7. const actionAddMedia = (typeId, mediaId, type) =>
  8. actionPromise('newMedia', gql(`mutation setMedia($media: MediaInput) {
  9. MediaUpsert(media: $media) {
  10. _id
  11. }
  12. }`, {media: {_id: mediaId, [type]: {_id: typeId}}}))
  13. export const actionUploadFile = (file) =>
  14. actionPromise('file', uploadFile(file))
  15. export const actionUploadFiles = (files) =>
  16. actionPromise('files', Promise.all(files.map(file => uploadFile(file))))
  17. export const actionSetUserAvatar = (file) =>
  18. async (dispatch, getState) => {
  19. let userId = getState().auth.payload.sub.id;
  20. let media = await dispatch(actionUploadFile(file));
  21. console.log(media)
  22. if (media._id){
  23. let dataId = await dispatch(actionAddMedia(userId, media._id, "userAvatar"))
  24. if (dataId){
  25. dispatch(actionAboutMe())
  26. }
  27. }
  28. }
  29. export const actionSetChatAvatar = (file) =>
  30. async (dispatch, getState) => {
  31. const [,route, chatId] = history.location.pathname.split('/');
  32. const chat = getState().chats[chatId];
  33. const media = await dispatch(actionUploadFile(file));
  34. console.log(media)
  35. if(media._id){
  36. const dataId = await dispatch(actionAddMedia(chatId, media._id, "chatAvatars"))
  37. if (dataId){
  38. //it helps to everyone to get changed chat with avatar from socket (something to change for example title)
  39. dispatch(actionUpdateChat({_id: chatId, title: chat.title}));
  40. }
  41. return dataId
  42. }
  43. }
  44. // const actionAddAvatar = (userId, avatarId) =>
  45. // actionPromise('avatar', gql(`mutation setAvatar($userId: ID, $avatarId: ID){
  46. // UserUpsert(user:{_id: $userId, avatar: {_id: $avatarId}}){
  47. // _id, avatar{
  48. // _id
  49. // }
  50. // }
  51. // }`,{userId: userId, avatarId: avatarId}))