set-avatar-actions.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import {call, put, select, takeEvery} from "redux-saga/effects";
  2. import {actionPromise, promiseWorker} from "../reducers/promiseReducer";
  3. import {backURL,gql} from "./gql-queries-backurl";
  4. import {actionAboutMe} from "./about-me-actions";
  5. export const actionUploadImage = (file) => {
  6. let fd = new FormData();
  7. fd.append("photo", file);
  8. return actionPromise(
  9. "uploadFile",
  10. fetch(backURL+'/upload', {
  11. method: "POST",
  12. headers: localStorage.authToken
  13. ? { Authorization: "Bearer " + localStorage.authToken }
  14. : {},
  15. body: fd,
  16. }).then((res) => res.json())
  17. );
  18. };
  19. export const actionSetAvatar = (file) =>
  20. ({type:'SET_AVATAR', file})
  21. export function* setAvatarWorker (action) {
  22. const {file} = action
  23. let result = yield call(promiseWorker,actionUploadImage(file));
  24. let { auth } = yield select();
  25. let imageId = result._id;
  26. let userId = auth?.payload?.sub?.id;
  27. yield call(
  28. promiseWorker,actionPromise(
  29. "setAvatar",
  30. gql(
  31. `mutation setAva($userId: String, $imageId: ID){
  32. UserUpsert(user:{_id: $userId, avatar: {_id: $imageId}}){_id}}`,
  33. { userId: userId, imageId: imageId }
  34. )
  35. ));
  36. yield put(actionAboutMe());
  37. };
  38. export function* setAvatarWatcher() {
  39. yield takeEvery ('SET_AVATAR',setAvatarWorker)
  40. }