user.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import * as types from "../actionsTypes/actionsTypes";
  2. const URL = "https://api-clinics.herokuapp.com/api/v1/users/";
  3. export const changeFindUserInput = payload => ({
  4. type:types.CHANGE_INPUT_VALUE_FIND_USER,
  5. payload
  6. });
  7. export const changeInputValueUserForm = payload => ({
  8. type:types.CHANGE_INPUT_VALUE_USER_FORM,
  9. payload
  10. });
  11. const getUsersRequest = payload => ({
  12. type: types.GET_USERS_REQUEST,
  13. payload
  14. });
  15. const getUsersRequestSuccess = payload => ({
  16. type: types.GET_USERS_REQUEST_SUCCESS,
  17. payload
  18. });
  19. const getUsersRequestFail = payload => ({
  20. type: types.GET_USERS_REQUEST_FAIL,
  21. payload
  22. });
  23. export const getUsers = () => dispatch => {
  24. dispatch(getUsersRequest());
  25. return fetch(`${URL}`,{
  26. credentials:"include"
  27. })
  28. .then(res => res.json())
  29. .then(res => {
  30. dispatch(getUsersRequestSuccess(res));
  31. })
  32. .catch(err => dispatch(getUsersRequestFail(err)));
  33. };
  34. const findUserRequest = payload => ({
  35. type: types.FIND_USER_REQUEST,
  36. payload
  37. });
  38. const findUserRequestSuccess = payload => ({
  39. type: types.FIND_USER_REQUEST_SUCCESS,
  40. payload
  41. });
  42. const findUserRequestFail = payload => ({
  43. type: types.FIND_USER_REQUEST_FAIL,
  44. payload
  45. });
  46. export const findUser = (payload) => dispatch => {
  47. dispatch(findUserRequest());
  48. return fetch(`${URL}`+payload,{
  49. credentials:"include"
  50. })
  51. .then(res => res.json())
  52. .then(res => {
  53. dispatch(findUserRequestSuccess(res));
  54. })
  55. .catch(err => dispatch(findUserRequestFail(err)));
  56. };
  57. const deleteUserRequest = payload => ({
  58. type: types.DELETE_USER_REQUEST,
  59. payload
  60. });
  61. const deleteUserRequestSuccess = payload => ({
  62. type: types.DELETE_USER_REQUEST_SUCCESS,
  63. payload
  64. });
  65. const deleteUserRequestFail = payload => ({
  66. type: types.DELETE_USER_REQUEST_FAIL,
  67. payload
  68. });
  69. export const deleteUser = (payload) => dispatch => {
  70. dispatch(deleteUserRequest());
  71. return fetch(`${URL}${payload}`, {
  72. method: "DELETE",
  73. credentials: "include"
  74. })
  75. .then(res => res.json())
  76. .then(res => dispatch(deleteUserRequestSuccess(res)))
  77. .catch(err => dispatch(deleteUserRequestFail(err)));
  78. };
  79. const putUserRequest = payload => ({
  80. type: types.PUT_USER_REQUEST,
  81. payload
  82. });
  83. const putUserRequestSuccess = payload => ({
  84. type: types.PUT_USER_REQUEST_SUCCESS,
  85. payload
  86. });
  87. const changeDataInCurrentUser = payload => ({
  88. type: types.CHANGE_DATA_IN_CURRENT_USER,
  89. payload
  90. })
  91. const putUserRequestFail = payload => ({
  92. type: types.PUT_USER_REQUEST_FAIL,
  93. payload
  94. });
  95. export const putUser = (payload) => dispatch => {
  96. dispatch(putUserRequest());
  97. return fetch(`${URL}${payload.path}`, {
  98. method: "PUT",
  99. credentials: "include",
  100. headers: {
  101. "Content-Type": "application/json"
  102. },
  103. body: JSON.stringify(payload.data)
  104. })
  105. .then(res => res.json())
  106. .then(res => dispatch(putUserRequestSuccess(res)))
  107. .then(dispatch(changeDataInCurrentUser(payload.data)))
  108. .catch(err => dispatch(putUserRequestFail(err)));
  109. };