authActions.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { history } from '../App'
  2. import { gql } from '../helpers'
  3. import {
  4. actionPromise,
  5. actionAuthLogin,
  6. actionAuthLogout,
  7. actionAboutMe,
  8. actionChatsClear,
  9. } from '../reducers'
  10. import { actionUploadFile } from './mediaActions'
  11. export const actionFullLogout = () => async (dispatch) => {
  12. history.push('/login')
  13. await dispatch(actionAuthLogout())
  14. await dispatch(actionChatsClear())
  15. }
  16. const actionLogin = (login, password) =>
  17. actionPromise(
  18. 'login',
  19. gql(
  20. `query log($login: String, $password: String) {
  21. login(login: $login, password: $password)
  22. }`,
  23. { login, password }
  24. )
  25. )
  26. export const actionFullLogin = (login, password) => async (dispatch) => {
  27. const token = await dispatch(actionLogin(login, password))
  28. if (token) {
  29. history.push('/main')
  30. }
  31. if (token) {
  32. await dispatch(actionAuthLogin(token))
  33. await dispatch(actionAboutMe())
  34. }
  35. }
  36. const actionRegister = (login, password, nick) =>
  37. actionPromise(
  38. 'register',
  39. gql(
  40. `mutation reg($user:UserInput) {
  41. UserUpsert(user:$user) {
  42. _id
  43. }
  44. }
  45. `,
  46. { user: { login, password, nick } }
  47. )
  48. )
  49. export const actionFullRegister =
  50. (login, password, nick) => async (dispatch) => {
  51. const regId = await dispatch(actionRegister(login, password, nick))
  52. if (regId) {
  53. await dispatch(actionFullLogin(login, password))
  54. }
  55. }
  56. const actionUpdateUserAvatar = (userId, avatarId) =>
  57. actionPromise(
  58. 'updateUserAv',
  59. gql(
  60. `mutation updateUserAv($user:UserInput) {
  61. UserUpsert(user:$user) {
  62. _id
  63. login
  64. nick
  65. avatar {
  66. url
  67. }
  68. }
  69. }`,
  70. { user: { _id: userId, avatar: { _id: avatarId } } }
  71. )
  72. )
  73. const actionUpdateUserLogin = (userId, newLogin, newNick) =>
  74. actionPromise(
  75. 'updateUser',
  76. gql(
  77. `mutation updateUser($user:UserInput) {
  78. UserUpsert(user:$user) {
  79. _id
  80. login
  81. nick
  82. }
  83. }`,
  84. { user: { _id: userId, login: newLogin, nick: newNick } }
  85. )
  86. )
  87. export const actionSetUserInfo =
  88. (name, file, newLogin, newNick) => async (dispatch, getState) => {
  89. const { auth } = getState()
  90. const userId = auth.payload?.sub?.id
  91. if (file) {
  92. const fileObj = await dispatch(actionUploadFile(name, file))
  93. await dispatch(actionUpdateUserAvatar(userId, fileObj?._id))
  94. }
  95. await dispatch(actionUpdateUserLogin(userId, newLogin, newNick))
  96. await dispatch(actionAboutMe())
  97. }
  98. const actionChangePass = (_id, password) =>
  99. actionPromise(
  100. 'register',
  101. gql(
  102. `mutation reg($user:UserInput) {
  103. UserUpsert(user:$user) {
  104. _id
  105. }
  106. }
  107. `,
  108. { user: { _id, password } }
  109. )
  110. )
  111. export const actionSetUserPass = (password) => async (dispatch, getState) => {
  112. const { auth } = getState()
  113. const userId = auth.payload?.sub?.id
  114. await dispatch(actionChangePass(userId, password))
  115. }