authActions.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. history.push('/main')
  28. const token = await dispatch(actionLogin(login, password))
  29. if (token) {
  30. await dispatch(actionAuthLogin(token))
  31. await dispatch(actionAboutMe())
  32. }
  33. }
  34. const actionRegister = (login, password, nick) =>
  35. actionPromise(
  36. 'register',
  37. gql(
  38. `mutation reg($user:UserInput) {
  39. UserUpsert(user:$user) {
  40. _id
  41. }
  42. }
  43. `,
  44. { user: { login, password, nick } }
  45. )
  46. )
  47. export const actionFullRegister =
  48. (login, password, nick) => async (dispatch) => {
  49. const regId = await dispatch(actionRegister(login, password, nick))
  50. if (regId) {
  51. await dispatch(actionFullLogin(login, password))
  52. }
  53. }
  54. const actionUpdateUserAvatar = (userId, avatarId) =>
  55. actionPromise(
  56. 'updateUserAv',
  57. gql(
  58. `mutation updateUserAv($user:UserInput) {
  59. UserUpsert(user:$user) {
  60. _id
  61. login
  62. nick
  63. avatar {
  64. url
  65. }
  66. }
  67. }`,
  68. { user: { _id: userId, avatar: { _id: avatarId } } }
  69. )
  70. )
  71. const actionUpdateUserLogin = (userId, newLogin, newNick) =>
  72. actionPromise(
  73. 'updateUser',
  74. gql(
  75. `mutation updateUser($user:UserInput) {
  76. UserUpsert(user:$user) {
  77. _id
  78. login
  79. nick
  80. }
  81. }`,
  82. { user: { _id: userId, login: newLogin, nick: newNick } }
  83. )
  84. )
  85. export const actionSetUserInfo =
  86. (name, file, newLogin, newNick) => async (dispatch, getState) => {
  87. const { auth } = getState()
  88. const userId = auth.payload?.sub?.id
  89. if (file) {
  90. const fileObj = await dispatch(actionUploadFile(name, file))
  91. await dispatch(actionUpdateUserAvatar(userId, fileObj?._id))
  92. }
  93. await dispatch(actionUpdateUserLogin(userId, newLogin, newNick))
  94. await dispatch(actionAboutMe())
  95. }
  96. const actionChangePass = (_id, password) =>
  97. actionPromise(
  98. 'register',
  99. gql(
  100. `mutation reg($user:UserInput) {
  101. UserUpsert(user:$user) {
  102. _id
  103. }
  104. }
  105. `,
  106. { user: { _id, password } }
  107. )
  108. )
  109. export const actionSetUserPass = (password) => async (dispatch, getState) => {
  110. const { auth } = getState()
  111. const userId = auth.payload?.sub?.id
  112. await dispatch(actionChangePass(userId, password))
  113. }