auth.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import * as types from '../actionsTypes/actionsTypes'
  2. import axios from 'axios'
  3. const authRequest = payload => ({
  4. type: types.AUTH_REQUEST,
  5. payload
  6. })
  7. const authRequestSuccess = payload => ({
  8. type: types.AUTH_REQUEST_SUCCESS,
  9. payload
  10. })
  11. const authRequestFail = payload => ({
  12. type: types.AUTH_REQUEST_FAIL,
  13. payload
  14. })
  15. export const auth = payload => {
  16. return async dispatch => {
  17. dispatch(authRequest());
  18. try {
  19. const { data } = await axios.post("https://api-clinics.herokuapp.com/api/v1/auth/login", payload);
  20. localStorage.setItem("userId",data.user._id)
  21. dispatch(authRequestSuccess(data));
  22. } catch (error){
  23. dispatch(authRequestFail(error));
  24. }
  25. }
  26. }
  27. const registerRequest = payload => ({
  28. type:types.REGISTRATION_REQUEST,
  29. payload
  30. })
  31. const registerRequestSuccess = payload => ({
  32. type: types.REGISTRATION_REQUEST_SUCCESS,
  33. payload
  34. })
  35. const registrRequestFail = payload => ({
  36. type: types.REGISTRATION_REQUEST_FAIL,
  37. payload
  38. })
  39. export const register = payload => {
  40. return async dispatch => {
  41. dispatch(registerRequest());
  42. try {
  43. const { data } = await axios.post(
  44. "https://api-clinics.herokuapp.com/api/v1/auth/register",
  45. payload
  46. );
  47. dispatch(registerRequestSuccess(data))
  48. } catch(error) {
  49. dispatch(registrRequestFail(error))
  50. }
  51. };
  52. };
  53. const getUserRequest = payload => {
  54. return {
  55. type: types.GET_USER_REQUEST,
  56. payload
  57. }}
  58. const getUserRequestSuccess = payload => ({
  59. type: types.GET_USER_REQUEST_SUCCESS,
  60. payload
  61. })
  62. const getUserRequestFail = payload => ({
  63. type: types.GET_USER_REQUEST_FAIL,
  64. payload
  65. })
  66. export const getUser = () => dispatch => {
  67. dispatch(getUserRequest());
  68. return fetch(`https://api-clinics.herokuapp.com/api/v1/users/` + localStorage.getItem('userId'),{
  69. credentials:"include"
  70. })
  71. .then(res => res.json())
  72. .then(res => dispatch(getUserRequestSuccess(res)))
  73. .catch(err => dispatch(getUserRequestFail(err)));
  74. };
  75. const putUserRequest = payload => ({
  76. type: types.PUT_USER_REQUEST,
  77. payload
  78. });
  79. const putUserRequestSuccess = payload => ({
  80. type: types.PUT_USER_REQUEST_SUCCESS,
  81. payload
  82. });
  83. const putUserRequestFail = payload => ({
  84. type: types.PUT_USER_REQUEST_FAIL,
  85. payload
  86. });
  87. export const putUser = (payload) => dispatch => {
  88. dispatch(putUserRequest());
  89. return fetch(` https://api-clinics.herokuapp.com/api/v1/users/` + localStorage.getItem('userId'), {
  90. method: "PUT",
  91. credentials: "include",
  92. headers: {
  93. "Content-Type": "application/json"
  94. },
  95. body: JSON.stringify(payload.data)
  96. })
  97. .then(res => res.json())
  98. .then(res => dispatch(putUserRequestSuccess(res)))
  99. .catch(err => dispatch(putUserRequestFail(err)));
  100. };
  101. export const changeInputValueUserForm = payload => ({
  102. type: types.CHANGE_INPUT_VALUE_USER_FORM,
  103. payload
  104. });
  105. export const setBasicFormValue = payload => ({
  106. type: types.USER_CHANGE_FORM_INFO,
  107. payload
  108. })
  109. export const userLogout = payload => ({
  110. type: types.USER_LOGOUT,
  111. payload
  112. })